SlideShare a Scribd company logo
1 of 31
INHERITANCE
AUTHOR:- AHSAN RAJA
INHERITANCE
A mechanism wherein a new class is derived from an existing class.
In Java, classes may inherit or acquire the properties and methods of other classes.
A class derived from another class is called a subclass, whereas the class from which a subclass is derived is ca
a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses
class Super{
.....
….
}
class Sub extends Super{
.....
…..
}
 Permits sharing and accessing properties form one to another class.
 To establish this relation java uses “EXTEND” keyword.
 Example:
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
ADVANTAGES OF INHERITANCE
 Minimize the amount of duplicate code in an application by sharing common code amongst
several subclasses.
 It also make application code more flexible to change because classes that inherit from a
common superclass can
be used interchangeably.
 Reusability -- facility to use public methods of base class without rewriting the same.
 Data hiding -- base class can decide to keep some data private so that it cannot be altered by
the derived class.
 Overriding--With inheritance, we will be able to override the methods of the base class so that
meaningful
implementation of the base class method can be designed in the derived class.
IS-A & HAS-A Relationship
• If a class is child of another class then we have is a relation
• If an object contain an instance of a class this is called has a relation.
HAS-A RELATIONSHIP
package relationships;
class Car {
private String colour;
private int maxSpeed;
public void carInfo(){
System.out.println("Car Colour= "+colour + " Max Speed= " + maxSpeed);
}
public void setColor(String colour) {
this.colour = colour;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
IS-A RELATIONSHIP
class Audi extends Car
{
//Audi extends Car and thus inherits all methods from Car (except final and
static)
public void AudiStartDemo(){
Engine AudiEngine = new Engine();
AudiEngine.start();
}
}
SPECIALIZATION AND GENERALIZATION
o The process of extracting common characteristics from two or more
classes and combining them into a generalized superclass, is called
Generalization. The common characteristics can be attributes or
methods. Generalization is represented by a triangle followed by a line.
o Specialization is the reverse process of Generalization means creating
new sub classes from an existing class.
 There is no limit to the number of subclasses a class can
have.
 There is no limit to the depth of the class tree.
 Subclasses are more specific and have more functionality.
 Super classes capture generic functionality common across
many types of objects.
SUB CLASS CONSTRUCTOR
a subclass constructor is used to construct the inheritance instance
variable of both the subclass and super class
the subclass constructor uses the keyword super to invoke the
constructor method of the super class.
TYPES OF INHERITANCE
A. SINGLE INHERITANCE
B. MULTILEVEL INHERITANCE
C. MULTIPLE INHERITANCE
D. HIERARCHICAL INHERITANCE
E. HYBRID INHERITANCE
TYPE OF INHERITANCE
 Single Inheritance
When a class extends another class(Only one class) then we call it
as Single inheritance.
public class ClassA
{
public void dispA(){
System.out.println("disp() method of ClassA");
}
}
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB");
}
public static void main(String args[]) {
ClassB b = new ClassB();
b.dispA();
b.dispB();
}
}
 Multilevel Inheritance
In Multilevel Inheritance a derived class will be inheriting a parent class and as well
as the derived class act as the parent class to other class.
public class ClassA {
public void dispA() {
System.out.println("disp() method of ClassA");
} }
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB");
} }
public class ClassC extends ClassB {
public void dispC() {
System.out.println("disp() method of ClassC");
}
public static void main(String args[]) {
ClassC c = new ClassC();
c.dispA();
c.dispB();
c.dispC();
}
}
 Hierarchical Inheritance
In this inheritance multiple classes inherits from a single class i.e. there is one super
class and multiple sub classes.
public class ClassA {
public void dispA() {
System.out.println("disp() method of ClassA"); } }
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB"); } }
public class ClassC extends ClassA {
public void dispC() {
System.out.println("disp() method of ClassC"); } }
public class ClassD extends ClassA {
public void dispD() {
System.out.println("disp() method of ClassD"); } }
public class HierarchicalInheritanceTest {
public static void main(String args[]) {
ClassB b = new ClassB();
b.dispB();
b.dispA();
ClassC c = new ClassC();
c.dispC();
c.dispA();
ClassD d = new ClassD();
d.dispD();
d.dispA(); } }
Access Control
Access control keywords define which classes can access classes, methods, and
members
Modifiers Class Package Subclass World
Public
Protected
Private
SUPER KEYWORD
 It is used inside a sub-class method definition to call a method defined in
the super class. Private methods of the super-class cannot be called.
Only public and protected methods can be called by the super keyword.
 It is also used by class constructors to invoke constructors of its parent
class.
USE OF SUPER KEYWORD
it calls the superclass constructor
inheritance syntax :- super( parameter list);
access the member of the super class
syntax:- super. member variable;
class Super_class{
int num = 20;
public void display(){
System.out.println("This is the display method of superclass");
} }
public class Sub_class extends Super_class {
int num = 10;
public void display(){
System.out.println("This is the display method of subclass");
}
public void my_method(){
Sub_class sub = new Sub_class();
sub.display();
super.display();
System.out.println("value of the variable named num in sub class:"+ sub.num);
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]){
Sub_class obj = new Sub_class();
obj.my_method();
} }
Invoking Superclass constructor
If a class is inheriting the properties of another class, the subclass automatically
acquires the default constructor of the super class. But if we want to call a
parametrized constructor of the super class, you need to use the super keyword
class Superclass{
int age;
Superclass(int age){
this.age = age; }
public void getAge(){
System.out.println("The value of the variable named age in super class is: "
+age); } }
public class Subclass extends Superclass {
Subclass(int age){
super(age); }
public static void main(String args[]){
Subclass s = new Subclass(24);
s.getAge();
} }
Method overriding
Declaring a method in subclass which is already present in parent class is known as
method overriding.
class Human{
public void eat() {
System.out.println("Human is eating"); }
}
class Boy extends Human{
public void eat(){ // @overriding
System.out.println("Boy is eating"); }
public static void main( String args[]) {
Boy obj = new Boy();
obj.eat();
}
}
Rules of method overriding in Java
 Argument list: The argument list of overriding method must be same as that of the
method in parent class. The data types of the arguments and their sequence should
be maintained as it is in the overriding method.
 Access Modifier: The Access Modifier of the overriding method (method of
subclass) cannot be more restrictive than the overridden method of parent class.
 Private, Static and final methods cannot be overridden as they are local to the
class. However static methods can be re-declared in the sub class, in this case the
sub-class method would act differently and will have nothing to do with the same
static method of parent class.
 Must be IS-A relationship (inheritance).
ANOTHER EXAMPLEOF OVERRIDING
class Animal{
public void move(){
system.out.println("animals can move"); } }
class Dog extends Animal{
public void move(){
system.out.println("dogs can walk and run"); } }
public class TestDog{
static void main(string args[])
{ Animal a = new Animal(); // animal reference and object
Animal b = new Dog(); // animal reference but dog object
a.move();// runs the method in animal class
b.move();//runs the method in dog class } }
PROGRAMS OF Hierachical INHERRITANCE
package office;
import java.util.Scanner;
class parent
{ int code;
String name;
String address;
int phonenum;
void enterdata()
{
Scanner dc=new Scanner(System.in);//use to
take string values
Scanner in=new Scanner(System.in);//use
to take integer value
System.out.println("Enter Code=");
code=in.nextInt();
System.out.println("Enter Name=");
name=dc.nextLine();
System.out.println("Enter Address=");
address=dc.nextLine();
System.out.println("Enter Phone-
Number=");
phonenum=in.nextInt();
}
void display()
{
System.out.println("Code="+code);
System.out.println("Name="+name);
System.out.println("Address="+address);
System.out.println("phonenum="+phonenum);
}
}
class Typest extends parent
{
int pay;
void enterT()
{
enterdata();
Scanner in=new Scanner(System.in);
System.out.println("Enterdata");
pay=in.nextInt();
}
}
class Officer extends parent
{
int pay;
void enterO()
{
enterdata();
Scanner in=new Scanner(System.in);
System.out.println("Enterdata");
pay=in.nextInt();
display();
System.out.println("Basic pay="+pay);
}
public class Office {
public static void main(String[] args) {
Typest T=new Typest();
T.enterT();
Officer O=new Officer();
O.enterO();
}
}
Static keyword in java
The static keyword in java is used for memory management mainly.
 the static can be;
Variable
Function/method
It makes your program memory efficient
Static :
Static keyword does not require obj it can be called without obj also the value of static
keyword remains same.
Final :
Final keyword stops overriding of any method or variable …
 Class Student8{
Int rollno;
String name;
Static String college=“ITS”;
Student8(int r, String n){
rollno=r;
name=n;}
Void display (){
System.out.println(rollno+” “+name” “+college);}
public static void main(String[ ] args){
student8 s1=new student8(228,”mahnoor”);
student8 s2=new student8(051,”sana”);
s1.display();
s2.display();
}
Why java main method is static ?
Because object is not required to call static method if it were non-
static method, JVM create object first then call main() method
that will lead the problem of extra memory allocation.
FINAL KEYWORD IN JAVA
IT is used in java to restrict the user. It can be use in many
contexts.
Variable
Method
Class
JAVA FINAL KEYWORD
 Stop change value
 Stop method overriding
 Stop inheritance
class Bike{
final void run(){
System.out.println(“running”);}
class Honda extends Bike{
void run(){
System.out.println(“running safetly”);}
Public static void main(String[ ] args){
Honda honda= new Honda();
Honda.run();
}
}
Inheritance Slides

More Related Content

What's hot

itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in javaAtul Sehdev
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-javaDeepak Singh
 
Seminar on java
Seminar on javaSeminar on java
Seminar on javashathika
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Java Inheritance
Java InheritanceJava Inheritance
Java InheritanceVINOTH R
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
Unit3 java
Unit3 javaUnit3 java
Unit3 javamrecedu
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 

What's hot (20)

itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
 
Inheritance
InheritanceInheritance
Inheritance
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Inheritance polymorphism-in-java
Inheritance polymorphism-in-javaInheritance polymorphism-in-java
Inheritance polymorphism-in-java
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Unit3 java
Unit3 javaUnit3 java
Unit3 java
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 

Viewers also liked

Christopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
Christopher Odhiambo Joseph Operating Without Cultural And Art Education PolicyChristopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
Christopher Odhiambo Joseph Operating Without Cultural And Art Education PolicyWAAE
 
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)Talmidei Yeshua HaMashiach
 
Universidad central del ecuador investigación científica
Universidad central del ecuador  investigación científicaUniversidad central del ecuador  investigación científica
Universidad central del ecuador investigación científicaCristian Rea
 
Morocco in Five Thumbnails
Morocco in Five ThumbnailsMorocco in Five Thumbnails
Morocco in Five ThumbnailsAnn Marie Meehan
 
Aula a constituição da disciplina escolar ciências
Aula a constituição da disciplina escolar ciênciasAula a constituição da disciplina escolar ciências
Aula a constituição da disciplina escolar ciênciasLeonardo Kaplan
 
Inventories and the Cost of Goods Sold
Inventories and the Cost of Goods SoldInventories and the Cost of Goods Sold
Inventories and the Cost of Goods SoldMuhammad Unaib Aslam
 

Viewers also liked (12)

Genesis rodiguez
Genesis rodiguezGenesis rodiguez
Genesis rodiguez
 
Christopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
Christopher Odhiambo Joseph Operating Without Cultural And Art Education PolicyChristopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
Christopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
 
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
 
MembershipCertificate
MembershipCertificateMembershipCertificate
MembershipCertificate
 
Jayfox
JayfoxJayfox
Jayfox
 
Bedouin question with prophet
Bedouin question with prophetBedouin question with prophet
Bedouin question with prophet
 
Universidad central del ecuador investigación científica
Universidad central del ecuador  investigación científicaUniversidad central del ecuador  investigación científica
Universidad central del ecuador investigación científica
 
Morocco in Five Thumbnails
Morocco in Five ThumbnailsMorocco in Five Thumbnails
Morocco in Five Thumbnails
 
Account Receivable QuestionsZ
Account Receivable QuestionsZAccount Receivable QuestionsZ
Account Receivable QuestionsZ
 
Aula a constituição da disciplina escolar ciências
Aula a constituição da disciplina escolar ciênciasAula a constituição da disciplina escolar ciências
Aula a constituição da disciplina escolar ciências
 
Inventories and the Cost of Goods Sold
Inventories and the Cost of Goods SoldInventories and the Cost of Goods Sold
Inventories and the Cost of Goods Sold
 
Los artifices de la sociologia
Los artifices de la sociologiaLos artifices de la sociologia
Los artifices de la sociologia
 

Similar to Inheritance Slides

Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritanceKalai Selvi
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism JavaM. Raihan
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear ASHNA nadhm
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritancemanish kumar
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3Narayana Swamy
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritanceteach4uin
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesSakkaravarthiS1
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2sotlsoc
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppthenokmetaferia1
 

Similar to Inheritance Slides (20)

Java
JavaJava
Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Presentation 3.pdf
Presentation 3.pdfPresentation 3.pdf
Presentation 3.pdf
 
Chap-3 Inheritance.pptx
Chap-3 Inheritance.pptxChap-3 Inheritance.pptx
Chap-3 Inheritance.pptx
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear Inheritance and Polymorphism in java simple and clear
Inheritance and Polymorphism in java simple and clear
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
 
Oop inheritance chapter 3
Oop inheritance chapter 3Oop inheritance chapter 3
Oop inheritance chapter 3
 
Core java oop
Core java oopCore java oop
Core java oop
 
L7 inheritance
L7 inheritanceL7 inheritance
L7 inheritance
 
‫Chapter3 inheritance
‫Chapter3 inheritance‫Chapter3 inheritance
‫Chapter3 inheritance
 
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and InterfacesUNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
UNIT-2.pptx CS3391 Inheritance , types, packages and Interfaces
 
Chapter 8.2
Chapter 8.2Chapter 8.2
Chapter 8.2
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 

Inheritance Slides

  • 2. INHERITANCE A mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is ca a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses class Super{ ..... …. } class Sub extends Super{ ..... ….. }
  • 3.  Permits sharing and accessing properties form one to another class.  To establish this relation java uses “EXTEND” keyword.  Example: class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
  • 4. ADVANTAGES OF INHERITANCE  Minimize the amount of duplicate code in an application by sharing common code amongst several subclasses.  It also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably.  Reusability -- facility to use public methods of base class without rewriting the same.  Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class.  Overriding--With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.
  • 5. IS-A & HAS-A Relationship • If a class is child of another class then we have is a relation • If an object contain an instance of a class this is called has a relation.
  • 6. HAS-A RELATIONSHIP package relationships; class Car { private String colour; private int maxSpeed; public void carInfo(){ System.out.println("Car Colour= "+colour + " Max Speed= " + maxSpeed); } public void setColor(String colour) { this.colour = colour; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } }
  • 7. IS-A RELATIONSHIP class Audi extends Car { //Audi extends Car and thus inherits all methods from Car (except final and static) public void AudiStartDemo(){ Engine AudiEngine = new Engine(); AudiEngine.start(); } }
  • 8. SPECIALIZATION AND GENERALIZATION o The process of extracting common characteristics from two or more classes and combining them into a generalized superclass, is called Generalization. The common characteristics can be attributes or methods. Generalization is represented by a triangle followed by a line. o Specialization is the reverse process of Generalization means creating new sub classes from an existing class.
  • 9.  There is no limit to the number of subclasses a class can have.  There is no limit to the depth of the class tree.  Subclasses are more specific and have more functionality.  Super classes capture generic functionality common across many types of objects.
  • 10. SUB CLASS CONSTRUCTOR a subclass constructor is used to construct the inheritance instance variable of both the subclass and super class the subclass constructor uses the keyword super to invoke the constructor method of the super class.
  • 11. TYPES OF INHERITANCE A. SINGLE INHERITANCE B. MULTILEVEL INHERITANCE C. MULTIPLE INHERITANCE D. HIERARCHICAL INHERITANCE E. HYBRID INHERITANCE
  • 12. TYPE OF INHERITANCE  Single Inheritance When a class extends another class(Only one class) then we call it as Single inheritance.
  • 13. public class ClassA { public void dispA(){ System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } public static void main(String args[]) { ClassB b = new ClassB(); b.dispA(); b.dispB(); } }
  • 14.  Multilevel Inheritance In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class.
  • 15. public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassB { public void dispC() { System.out.println("disp() method of ClassC"); } public static void main(String args[]) { ClassC c = new ClassC(); c.dispA(); c.dispB(); c.dispC(); } }
  • 16.  Hierarchical Inheritance In this inheritance multiple classes inherits from a single class i.e. there is one super class and multiple sub classes.
  • 17. public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassA { public void dispC() { System.out.println("disp() method of ClassC"); } } public class ClassD extends ClassA { public void dispD() { System.out.println("disp() method of ClassD"); } } public class HierarchicalInheritanceTest { public static void main(String args[]) { ClassB b = new ClassB(); b.dispB(); b.dispA(); ClassC c = new ClassC(); c.dispC(); c.dispA(); ClassD d = new ClassD(); d.dispD(); d.dispA(); } }
  • 18. Access Control Access control keywords define which classes can access classes, methods, and members Modifiers Class Package Subclass World Public Protected Private
  • 19. SUPER KEYWORD  It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.  It is also used by class constructors to invoke constructors of its parent class.
  • 20. USE OF SUPER KEYWORD it calls the superclass constructor inheritance syntax :- super( parameter list); access the member of the super class syntax:- super. member variable;
  • 21. class Super_class{ int num = 20; public void display(){ System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; public void display(){ System.out.println("This is the display method of subclass"); } public void my_method(){ Sub_class sub = new Sub_class(); sub.display(); super.display(); System.out.println("value of the variable named num in sub class:"+ sub.num); System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]){ Sub_class obj = new Sub_class(); obj.my_method(); } }
  • 22. Invoking Superclass constructor If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the super class. But if we want to call a parametrized constructor of the super class, you need to use the super keyword class Superclass{ int age; Superclass(int age){ this.age = age; } public void getAge(){ System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age){ super(age); } public static void main(String args[]){ Subclass s = new Subclass(24); s.getAge(); } }
  • 23. Method overriding Declaring a method in subclass which is already present in parent class is known as method overriding. class Human{ public void eat() { System.out.println("Human is eating"); } } class Boy extends Human{ public void eat(){ // @overriding System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); obj.eat(); } }
  • 24. Rules of method overriding in Java  Argument list: The argument list of overriding method must be same as that of the method in parent class. The data types of the arguments and their sequence should be maintained as it is in the overriding method.  Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class.  Private, Static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.  Must be IS-A relationship (inheritance).
  • 25. ANOTHER EXAMPLEOF OVERRIDING class Animal{ public void move(){ system.out.println("animals can move"); } } class Dog extends Animal{ public void move(){ system.out.println("dogs can walk and run"); } } public class TestDog{ static void main(string args[]) { Animal a = new Animal(); // animal reference and object Animal b = new Dog(); // animal reference but dog object a.move();// runs the method in animal class b.move();//runs the method in dog class } }
  • 26. PROGRAMS OF Hierachical INHERRITANCE package office; import java.util.Scanner; class parent { int code; String name; String address; int phonenum; void enterdata() { Scanner dc=new Scanner(System.in);//use to take string values Scanner in=new Scanner(System.in);//use to take integer value System.out.println("Enter Code="); code=in.nextInt(); System.out.println("Enter Name="); name=dc.nextLine(); System.out.println("Enter Address="); address=dc.nextLine(); System.out.println("Enter Phone- Number="); phonenum=in.nextInt(); } void display() { System.out.println("Code="+code); System.out.println("Name="+name); System.out.println("Address="+address); System.out.println("phonenum="+phonenum); } } class Typest extends parent { int pay; void enterT() { enterdata(); Scanner in=new Scanner(System.in); System.out.println("Enterdata"); pay=in.nextInt(); } } class Officer extends parent { int pay; void enterO() { enterdata(); Scanner in=new Scanner(System.in); System.out.println("Enterdata"); pay=in.nextInt(); display(); System.out.println("Basic pay="+pay); } public class Office { public static void main(String[] args) { Typest T=new Typest(); T.enterT(); Officer O=new Officer(); O.enterO(); } }
  • 27. Static keyword in java The static keyword in java is used for memory management mainly.  the static can be; Variable Function/method It makes your program memory efficient Static : Static keyword does not require obj it can be called without obj also the value of static keyword remains same. Final : Final keyword stops overriding of any method or variable …
  • 28.  Class Student8{ Int rollno; String name; Static String college=“ITS”; Student8(int r, String n){ rollno=r; name=n;} Void display (){ System.out.println(rollno+” “+name” “+college);} public static void main(String[ ] args){ student8 s1=new student8(228,”mahnoor”); student8 s2=new student8(051,”sana”); s1.display(); s2.display(); }
  • 29. Why java main method is static ? Because object is not required to call static method if it were non- static method, JVM create object first then call main() method that will lead the problem of extra memory allocation. FINAL KEYWORD IN JAVA IT is used in java to restrict the user. It can be use in many contexts. Variable Method Class
  • 30. JAVA FINAL KEYWORD  Stop change value  Stop method overriding  Stop inheritance class Bike{ final void run(){ System.out.println(“running”);} class Honda extends Bike{ void run(){ System.out.println(“running safetly”);} Public static void main(String[ ] args){ Honda honda= new Honda(); Honda.run(); } }