SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

Inheritance
InheritanceInheritance
Inheritance
Selvin Josy Bai Somu
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
OOP java
OOP javaOOP java
OOP java
xball977
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
Tamanna Akter
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Manish Sahu
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Arnab Bhaumik
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Githushan Gengaparam
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Vineeta Garg
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
Simplilearn
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
MOHIT AGARWAL
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Manish Sahu
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Polymorphism in c++(ppt)
Polymorphism in c++(ppt)Polymorphism in c++(ppt)
Polymorphism in c++(ppt)
Sanjit Shaw
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
Amit Trivedi
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
Md.Al-imran Roton
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 

Similar to Inheritance ppt (20)

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
HarshitaAshwani
 
Object Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. InheritanceObject Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. Inheritance
AndiNurkholis1
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
Arati Gadgil
 
Java
JavaJava
Java
Haripritha
 
Inheritance in Java - An Introduction & types
Inheritance in Java - An Introduction & typesInheritance in Java - An Introduction & types
Inheritance in Java - An Introduction & types
VijethaChandran
 
Inheritance,single,multiple.access rulepptx
Inheritance,single,multiple.access rulepptxInheritance,single,multiple.access rulepptx
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
Inheritance
InheritanceInheritance
Inheritance
disha singh
 
JAVA UNIT 2 BCA students' notes IPU university
JAVA UNIT 2 BCA students' notes IPU universityJAVA UNIT 2 BCA students' notes IPU university
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
ManpreetSingh1387
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
TharuniDiddekunta
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
Ayan974999
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
COMSATS Institute of Information Technology
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
Debasish Pratihari
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
kumari36
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
Pooja Bhojwani
 
Inheritance
InheritanceInheritance
Inheritance
ssusera6878f
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Object Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. InheritanceObject Oriented Programming - 7.1. Inheritance
Object Oriented Programming - 7.1. Inheritance
AndiNurkholis1
 
Inheritance in Java - An Introduction & types
Inheritance in Java - An Introduction & typesInheritance in Java - An Introduction & types
Inheritance in Java - An Introduction & types
VijethaChandran
 
Inheritance,single,multiple.access rulepptx
Inheritance,single,multiple.access rulepptxInheritance,single,multiple.access rulepptx
Inheritance,single,multiple.access rulepptx
ArunPatrick2
 
JAVA UNIT 2 BCA students' notes IPU university
JAVA UNIT 2 BCA students' notes IPU universityJAVA UNIT 2 BCA students' notes IPU university
JAVA UNIT 2 BCA students' notes IPU university
n32310997
 
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptxInheritance in java.pptx_20241025_101324_0000.pptx.pptx
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx
saurabhthege
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
ManpreetSingh1387
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
Ayan974999
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
Terry Yoast
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
kumari36
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...Inheritance in Java is a mechanism in which one object acquires all the prope...
Inheritance in Java is a mechanism in which one object acquires all the prope...
Kavitha S
 
Ad

Recently uploaded (20)

GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Handling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptxHandling Multiple Choice Responses: Fortune Effiong.pptx
Handling Multiple Choice Responses: Fortune Effiong.pptx
AuthorAIDNationalRes
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar RabbiPresentation on Tourism Product Development By Md Shaifullar Rabbi
Presentation on Tourism Product Development By Md Shaifullar Rabbi
Md Shaifullar Rabbi
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Ad

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