SlideShare a Scribd company logo
2
Most read
3
Most read
10
Most read
INHERITANCE &
METHOD
OVERRIDING
ADVANCED JAVA
PROGRAMMING
NADAR SARASWATHI COLLEGE OF ARTS AND
SCIENCE,THENI.
B.NIVEGEETHA(I-MSC(CS))
INHERITANCE
 Inheritance is an important pillar of OOP(Object Oriented
Programming). It is the mechanism in java by which one class is
allow to inherit the features(fields and methods) of another class.
Important terminology:
 Super Class: The class whose features are inherited is known as
super class(or a base class or a parent class).
 Sub Class: The class that inherits the other class is known as sub
class(or a derived class, extended class, or child class). The
subclass can add its own fields and methods in addition to the
super class fields and methods.
 Reusability: Inheritance supports the concept of “reusability”,
i.e. when we want to create a new class and there is already a
class that includes some of the code that we want, we can derive
our new class from the existing class. By doing this, we are
reusing the fields and methods of the existing class.
SINGLE INHERITANCE
 Types of Inheritance in Java
 Below are the different types of inheritance which is supported
by Java.
 Single Inheritance : In single inheritance, subclasses inherit
the features of one super class. In image below, the class A
serves as a base class for the derived class B.
MULTILEVEL
INHERITANCE
 Multilevel Inheritance : In Multilevel Inheritance, a derived
class will be inheriting a base class and as well as the derived
class also act as the base class to other class. In below image, the
class A serves as a base class for the derived class B, which in
turn serves as a base class for the derived class C. In Java, a class
cannot directly access the grandparent’s members.
HIERARCHICAL
INHERITANCE
 Hierarchical Inheritance : In Hierarchical Inheritance, one
class serves as a super class (base class) for more than one sub
class. In below image, the class A serves as a base class for the
derived class B,C and D.
MULTIPLE INHERITANCE
 Multiple Inheritance (Through Interfaces) : In Multiple
inheritance ,one class can have more than one super class and
inherit features from all parent classes. Please note that Java
does not support multiple inheritance with classes. In java, we
can achieve multiple inheritance only through Interfaces. In
image below, Class C is derived from interface A and B.
HYBRID INHERITANCE
 Hybrid Inheritance(Through Interfaces) : It is a mix of two or
more of the above types of inheritance. Since java doesn’t
support multiple inheritance with classes, the hybrid inheritance
is also not possible with classes. In java, we can achieve hybrid
inheritance only through Interfaces.
IMPORTANT FACTS
 Default super class: Except Object class, which has no super class,
every class has one and only one direct super class (single
inheritance). In the absence of any other explicit super class, every
class is implicitly a subclass of Object class.
 Super class can only be one: A super class can have any number of
subclasses. But a subclass can have only one super class. This is
because Java does not support multiple inheritance with classes.
Although with interfaces, multiple inheritance is supported by java.
 Inheriting Constructors: A subclass inherits all the members
(fields, methods, and nested classes) from its super class.
Constructors are not members, so they are not inherited by
subclasses, but the constructor of the super class can be invoked
from the subclass.
 Private member inheritance: A subclass does not inherit the
private members of its parent class.
GENERAL SYNTAX OF
INHERITANCE
public class Class extends
ParentClass
{
//new variable or methods here
}
EXAMPLE
public class Shape {
int length;
int breadth;
}
public class Rectangle extends Shape
{
int area;
public void calculateArea()
{
area = length*breadth;
}
public static void main(String args[])
{
Rectangle r = new Rectangle();
//Assigning values to Shape class attributes
r.length = 10;
r.breadth = 20;
//Calculate the area
r.calcualteArea();
System.out.println("The Area of rectangle of length "" +r.length+"" and breadth
""+r.breadth+"" is ""+r.area+""");
}
METHOD OVERRIDING
 Declaring a method in sub class which is already present
in parent class is known as method overriding. Overriding is
done so that a child class can give its own implementation to a
method which is already provided by the parent class. In this
case the method in parent class is called overridden method and
the method in child class is called overriding method.
class Human
{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
}
class Boy extends Human
{
//Overriding method public void eat()
{
System.out.println("Boy is eating");
}
public static void main( String args[])
{
Boy obj = new Boy();
//This will call the child class version of eat()
obj.eat();
}
}
ADVANTAGE OF METHOD
OVERRIDING
 The main advantage of method overriding is that the class can
give its own specific implementation to a inherited
method without even modifying the parent class code.
 This is helpful when a class has several child classes, so if a child
class needs to use the parent class method, it can use it and the
other classes that want to have different implementation can use
overriding feature to make changes without touching the parent
class code.
RULES OF METHOD
OVERRIDING
 Argument list: The argument list of overriding method (method
of child class) must match the Overridden method(the method of
parent class). The data types of the arguments and their sequence
should exactly match.
 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.
 Binding of overridden methods happen at runtime which is
known as dynamic binding
 If a class is extending an abstract class or implementing
an interface then it has to override all the abstract methods
unless the class itself is a abstract class.
END

More Related Content

PPTX
Inheritance in Object Oriented Programming
Ashita Agrawal
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
PPTX
Inheritance in c++
Paumil Patel
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPTX
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Inheritance
Sapna Sharma
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
Inheritance in c++
Paumil Patel
 
Inheritance in OOPS
Ronak Chhajed
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 

What's hot (20)

PDF
Java variable types
Soba Arjun
 
PPTX
Strings in Java
Abhilash Nair
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
class and objects
Payel Guria
 
PPTX
Packages in java
Elizabeth alexander
 
PPT
Function overloading(c++)
Ritika Sharma
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPT
Operator Overloading
Nilesh Dalvi
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PPTX
Method overloading
Lovely Professional University
 
PPTX
Java Tokens
Madishetty Prathibha
 
PPT
1 - Introduction to PL/SQL
rehaniltifat
 
PPT
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
PPTX
This pointer
Kamal Acharya
 
PPTX
INHERITANCE IN JAVA.pptx
NITHISG1
 
Java variable types
Soba Arjun
 
Strings in Java
Abhilash Nair
 
Classes, objects in JAVA
Abhilash Nair
 
Inheritance in java
Tech_MX
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Interface in java
PhD Research Scholar
 
class and objects
Payel Guria
 
Packages in java
Elizabeth alexander
 
Function overloading(c++)
Ritika Sharma
 
Data types in java
HarshitaAshwani
 
Constructor in java
Pavith Gunasekara
 
Operator Overloading
Nilesh Dalvi
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Method overloading
Lovely Professional University
 
1 - Introduction to PL/SQL
rehaniltifat
 
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
This pointer
Kamal Acharya
 
INHERITANCE IN JAVA.pptx
NITHISG1
 
Ad

Similar to Inheritance ppt (20)

PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PPTX
Unit3 inheritance
Kalai Selvi
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
Java inheritance
BHUVIJAYAVELU
 
PPTX
Inheritance Slides
Ahsan Raja
 
PPTX
Chap-3 Inheritance.pptx
chetanpatilcp783
 
PDF
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Inheritance in Java - An Introduction & types
VijethaChandran
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PPT
L7 inheritance
teach4uin
 
PPT
L7 inheritance
teach4uin
 
PPTX
Java
Haripritha
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
PPTX
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
PPTX
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
PDF
Inheritance
Ravi_Kant_Sahu
 
PPTX
Lecture 6 inheritance
manish kumar
 
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Unit3 part2-inheritance
DevaKumari Vijay
 
Unit3 inheritance
Kalai Selvi
 
Ch5 inheritance
HarshithaAllu
 
Java inheritance
BHUVIJAYAVELU
 
Inheritance Slides
Ahsan Raja
 
Chap-3 Inheritance.pptx
chetanpatilcp783
 
Java programming -Object-Oriented Thinking- Inheritance
Jyothishmathi Institute of Technology and Science Karimnagar
 
Inheritance in Java - An Introduction & types
VijethaChandran
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
L7 inheritance
teach4uin
 
L7 inheritance
teach4uin
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
OOPS_Unit2.inheritance and interface objected oriented programming
ssuserf45a65
 
Inheritance
Ravi_Kant_Sahu
 
Lecture 6 inheritance
manish kumar
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Ad

Recently uploaded (20)

PPTX
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
PPTX
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 
TEF & EA Bsc Nursing 5th sem.....BBBpptx
AneetaSharma15
 
UPPER GASTRO INTESTINAL DISORDER.docx
BANDITA PATRA
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Electricity-Magnetic-and-Heating-Effects 4th Chapter/8th-science-curiosity.pd...
Sandeep Swamy
 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
 
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Mithil Fal Desai
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
5.Universal-Franchise-and-Indias-Electoral-System.pdfppt/pdf/8th class social...
Sandeep Swamy
 
Understanding operators in c language.pptx
auteharshil95
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
NOI Hackathon - Summer Edition - GreenThumber.pptx
MartinaBurlando1
 
Presentation on Janskhiya sthirata kosh.
Ms Usha Vadhel
 

Inheritance ppt

  • 1. INHERITANCE & METHOD OVERRIDING ADVANCED JAVA PROGRAMMING NADAR SARASWATHI COLLEGE OF ARTS AND SCIENCE,THENI. B.NIVEGEETHA(I-MSC(CS))
  • 2. INHERITANCE  Inheritance is an important pillar of OOP(Object Oriented Programming). It is the mechanism in java by which one class is allow to inherit the features(fields and methods) of another class. Important terminology:  Super Class: The class whose features are inherited is known as super class(or a base class or a parent class).  Sub Class: The class that inherits the other class is known as sub class(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the super class fields and methods.  Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
  • 3. SINGLE INHERITANCE  Types of Inheritance in Java  Below are the different types of inheritance which is supported by Java.  Single Inheritance : In single inheritance, subclasses inherit the features of one super class. In image below, the class A serves as a base class for the derived class B.
  • 4. MULTILEVEL INHERITANCE  Multilevel Inheritance : In Multilevel Inheritance, a derived class will be inheriting a base class and as well as the derived class also act as the base class to other class. In below image, the class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent’s members.
  • 5. HIERARCHICAL INHERITANCE  Hierarchical Inheritance : In Hierarchical Inheritance, one class serves as a super class (base class) for more than one sub class. In below image, the class A serves as a base class for the derived class B,C and D.
  • 6. MULTIPLE INHERITANCE  Multiple Inheritance (Through Interfaces) : In Multiple inheritance ,one class can have more than one super class and inherit features from all parent classes. Please note that Java does not support multiple inheritance with classes. In java, we can achieve multiple inheritance only through Interfaces. In image below, Class C is derived from interface A and B.
  • 7. HYBRID INHERITANCE  Hybrid Inheritance(Through Interfaces) : It is a mix of two or more of the above types of inheritance. Since java doesn’t support multiple inheritance with classes, the hybrid inheritance is also not possible with classes. In java, we can achieve hybrid inheritance only through Interfaces.
  • 8. IMPORTANT FACTS  Default super class: Except Object class, which has no super class, every class has one and only one direct super class (single inheritance). In the absence of any other explicit super class, every class is implicitly a subclass of Object class.  Super class can only be one: A super class can have any number of subclasses. But a subclass can have only one super class. This is because Java does not support multiple inheritance with classes. Although with interfaces, multiple inheritance is supported by java.  Inheriting Constructors: A subclass inherits all the members (fields, methods, and nested classes) from its super class. Constructors are not members, so they are not inherited by subclasses, but the constructor of the super class can be invoked from the subclass.  Private member inheritance: A subclass does not inherit the private members of its parent class.
  • 9. GENERAL SYNTAX OF INHERITANCE public class Class extends ParentClass { //new variable or methods here }
  • 10. EXAMPLE public class Shape { int length; int breadth; } public class Rectangle extends Shape { int area; public void calculateArea() { area = length*breadth; } public static void main(String args[]) { Rectangle r = new Rectangle(); //Assigning values to Shape class attributes r.length = 10; r.breadth = 20; //Calculate the area r.calcualteArea(); System.out.println("The Area of rectangle of length "" +r.length+"" and breadth ""+r.breadth+"" is ""+r.area+"""); }
  • 11. METHOD OVERRIDING  Declaring a method in sub class which is already present in parent class is known as method overriding. Overriding is done so that a child class can give its own implementation to a method which is already provided by the parent class. In this case the method in parent class is called overridden method and the method in child class is called overriding method.
  • 12. class Human { //Overridden method public void eat() { System.out.println("Human is eating"); } } class Boy extends Human { //Overriding method public void eat() { System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); //This will call the child class version of eat() obj.eat(); } }
  • 13. ADVANTAGE OF METHOD OVERRIDING  The main advantage of method overriding is that the class can give its own specific implementation to a inherited method without even modifying the parent class code.  This is helpful when a class has several child classes, so if a child class needs to use the parent class method, it can use it and the other classes that want to have different implementation can use overriding feature to make changes without touching the parent class code.
  • 14. RULES OF METHOD OVERRIDING  Argument list: The argument list of overriding method (method of child class) must match the Overridden method(the method of parent class). The data types of the arguments and their sequence should exactly match.  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.  Binding of overridden methods happen at runtime which is known as dynamic binding  If a class is extending an abstract class or implementing an interface then it has to override all the abstract methods unless the class itself is a abstract class.
  • 15. END