SlideShare a Scribd company logo
By
VINOTH R
St.Joseph’s college(Arts & Science)
Kovur, Chennai
• The mechanism of deriving a new class from an old
class is called inheritance.
• The old class is known as the Base class or Parent
class
• The new class is called the Sub class or Derived class
or Child class.
• The inheritance allows subclasses to inherit all the
variables and methods of their parent classes.
• Syntax
• The extends keyword indicates that making a
new class that derives from an existing class.
class Subclass-name extends Superclass-name
{
//methods and fields
}
As displayed in the figure
• Programmer is the subclass and Employee is the superclass.
• Relationship between two classes is Programmer IS-A
Employee.
• It means that Programmer is a type of Employee.
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);
} }
OUTPUT: Programmer salary is:40000.0
Bonus of programmer is:10000
• On the basis of class, there can be three types of
inheritance in java
1. Single
2. Multilevel
3. Hierarchical
• Multiple and hybrid inheritance is supported through
interface only (Will see in Unit 3).
• One sub class is derived from one super class
• From the below diagram class A serves as a Super class or
parent class
• Class B serves as a Base class or Sub class.
class Animal
{
void eat()
{
System.out.println("eating...");
} }
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
} }
Class Dog inherits the properties of
Class Animal.
class TestInheritance
{
public static void main(String args[])
{
Dog d=new Dog();
d.bark();
d.eat();
}}
OUTPUT:
barking...
eating...
Animal
Void eat()
Dog
Void bark()
Void eat()
• In Multilevel inheritance there is a concept of grandparent
class.
• From the Below diagram then class C inherits class B and
class B inherits class A
• Which means B is a parent class of C and A is a parent class
of B.
• So in this case class C is implicitly inheriting the properties
and method of class A along with B that’s what is called
multilevel inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
}}
class TestInheritance2
{
public static void main(String args[])
{
BabyDog d=new BabyDog();
d.weep();
d.bark();
d.eat();
}}
Output:
weeping...
barking...
eating...
Animal
Void eat()
Dog
Void bark()
Void eat()
BabyDog
void weep()
void bark()
Void eat()
• A class has more than one child classes (sub classes).
• More than one child classes have the same parent
class.
• Class A is a parent class of class B and class C and
class C.
class Animal
{
void eat()
{
System.out.println("eating...");
} }
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
} }
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
} }
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
}}
Output:
meowing...
eating...
Ad

More Related Content

What's hot (20)

Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
Ronak Chhajed
 
Method overloading
Method overloadingMethod overloading
Method overloading
Azaz Maverick
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Lovely Professional University
 
Inheritance in Object Oriented Programming
Inheritance in Object Oriented ProgrammingInheritance in Object Oriented Programming
Inheritance in Object Oriented Programming
Ashita Agrawal
 
Super and final in java
Super and final in javaSuper and final in java
Super and final in java
anshu_atri
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
MahinImran
 
itft-Inheritance in java
itft-Inheritance in javaitft-Inheritance in java
itft-Inheritance in java
Atul Sehdev
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Manish Sahu
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
baabtra.com - No. 1 supplier of quality freshers
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
sivasundari6
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Constructor overloading & method overloading
Constructor overloading & method overloadingConstructor overloading & method overloading
Constructor overloading & method overloading
garishma bhatia
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
Hirra Sultan
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
Nilesh Dalvi
 

Similar to Java Inheritance (20)

Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
Inheritance
InheritanceInheritance
Inheritance
Jancirani Selvam
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
RithwikRanjan
 
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
 
Unit3 part2-inheritance
Unit3 part2-inheritanceUnit3 part2-inheritance
Unit3 part2-inheritance
DevaKumari Vijay
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
kshitijsaini9
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
NAGASURESH MANOHARAN
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
Ahsan Raja
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
chauhankapil
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
Java
JavaJava
Java
Haripritha
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
Rassjb
 
Inheritance1
Inheritance1Inheritance1
Inheritance1
Daman Toor
 
c++Inheritance.pdf
c++Inheritance.pdfc++Inheritance.pdf
c++Inheritance.pdf
RanjanaThakuria1
 
Modules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptxModules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
C++ Presen. tation.pptx
C++ Presen.                   tation.pptxC++ Presen.                   tation.pptx
C++ Presen. tation.pptx
mohitsinha7739289047
 
Java - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptxJava - Inheritance_multiple_inheritance.pptx
Java - Inheritance_multiple_inheritance.pptx
Vikash Dúbēy
 
Unit3 inheritance
Unit3 inheritanceUnit3 inheritance
Unit3 inheritance
Kalai Selvi
 
A457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.pptA457405934_21789_26_2018_Inheritance.ppt
A457405934_21789_26_2018_Inheritance.ppt
RithwikRanjan
 
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
 
Inheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdfInheritance_abstractclass_interface.pdf
Inheritance_abstractclass_interface.pdf
kshitijsaini9
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
Ahsan Raja
 
Inheritance & interface ppt Inheritance
Inheritance & interface ppt  InheritanceInheritance & interface ppt  Inheritance
Inheritance & interface ppt Inheritance
narikamalliy
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
chauhankapil
 
Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1Inheritance & Polymorphism - 1
Inheritance & Polymorphism - 1
PRN USM
 
RajLec10.ppt
RajLec10.pptRajLec10.ppt
RajLec10.ppt
Rassjb
 
Modules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptxModules 333333333³3444444444444444444.pptx
Modules 333333333³3444444444444444444.pptx
radhikacordise
 
Ad

Recently uploaded (20)

Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
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
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
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
 
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
 
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
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
Kasdorf "Accessibility Essentials: A 2025 NISO Training Series, Session 5, Ac...
National Information Standards Organization (NISO)
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
BỘ ĐỀ TUYỂN SINH VÀO LỚP 10 TIẾNG ANH - 25 ĐỀ THI BÁM SÁT CẤU TRÚC MỚI NHẤT, ...
Nguyen Thanh Tu Collection
 
03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.03#UNTAGGED. Generosity in architecture.
03#UNTAGGED. Generosity in architecture.
MCH
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
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
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
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
 
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
 
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
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Ad

Java Inheritance

  • 1. By VINOTH R St.Joseph’s college(Arts & Science) Kovur, Chennai
  • 2. • The mechanism of deriving a new class from an old class is called inheritance. • The old class is known as the Base class or Parent class • The new class is called the Sub class or Derived class or Child class. • The inheritance allows subclasses to inherit all the variables and methods of their parent classes.
  • 3. • Syntax • The extends keyword indicates that making a new class that derives from an existing class. class Subclass-name extends Superclass-name { //methods and fields }
  • 4. As displayed in the figure • Programmer is the subclass and Employee is the superclass. • Relationship between two classes is Programmer IS-A Employee. • It means that Programmer is a type of Employee.
  • 5. 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); } } OUTPUT: Programmer salary is:40000.0 Bonus of programmer is:10000
  • 6. • On the basis of class, there can be three types of inheritance in java 1. Single 2. Multilevel 3. Hierarchical • Multiple and hybrid inheritance is supported through interface only (Will see in Unit 3).
  • 7. • One sub class is derived from one super class • From the below diagram class A serves as a Super class or parent class • Class B serves as a Base class or Sub class.
  • 8. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } Class Dog inherits the properties of Class Animal. class TestInheritance { public static void main(String args[]) { Dog d=new Dog(); d.bark(); d.eat(); }} OUTPUT: barking... eating... Animal Void eat() Dog Void bark() Void eat()
  • 9. • In Multilevel inheritance there is a concept of grandparent class. • From the Below diagram then class C inherits class B and class B inherits class A • Which means B is a parent class of C and A is a parent class of B. • So in this case class C is implicitly inheriting the properties and method of class A along with B that’s what is called multilevel inheritance.
  • 10. class Animal { void eat() { System.out.println("eating..."); }} class Dog extends Animal { void bark() { System.out.println("barking..."); }} class BabyDog extends Dog { void weep() { System.out.println("weeping..."); }} class TestInheritance2 { public static void main(String args[]) { BabyDog d=new BabyDog(); d.weep(); d.bark(); d.eat(); }} Output: weeping... barking... eating... Animal Void eat() Dog Void bark() Void eat() BabyDog void weep() void bark() Void eat()
  • 11. • A class has more than one child classes (sub classes). • More than one child classes have the same parent class. • Class A is a parent class of class B and class C and class C.
  • 12. class Animal { void eat() { System.out.println("eating..."); } } class Dog extends Animal { void bark() { System.out.println("barking..."); } } class Cat extends Animal { void meow() { System.out.println("meowing..."); } } class TestInheritance3 { public static void main(String args[]) { Cat c=new Cat(); c.meow(); c.eat(); }} Output: meowing... eating...