SlideShare a Scribd company logo
1 of 15
INHERITANCE
In Java
presented by:
Rahul Paul
Inheritance:
In java inheritance is a mechanism which one object can get
all the properties and behaviours of a parent object. In
inheritance there is a relationship between two classes.
That’s why it is-a relationship.
For example: Bike, car, bus this
are vehicle. That’s why the
child of vehicle are bike,
car, bus and vehicle is the
parent of this objects.
Vehicle
Bike, car,
bus
Java inheritance:
• The important of inheritance in java program is code
reusability. We can reuse the function of parent class at
child class.
• In java we use Extends keyword for inheritance.
• Subclass and Superclass in inheritance.
Inheritance Syntax:
class Parent
{
//code
}
class Child extends Parent
{
//code
}
Parent
Child
Simple program
class super {
Public void display()
{
System.out.println(“I am parent class”);
}
}
class sub extends super {
public static void main(String args[])
{
sub message = new sub()
message.display();
}
}
Super
I am from parent class
sub
Parent class
Child classs
Output: I am parent class
TYPES OF INHERITANCE:
 Single inheritance.
 Multiple inheritance.
 Multi-level inheritance.
 Hierarchical inheritance.
 Hybrid inheritance.
Note: In java Multiple inheritance is not supported.
SINGLE INHERITANCE:
Single inheritance is the most easy type to understand. In
single inheritance a class extends another only one class. The
child class inherit only parents class. The subclass inherit the
objects of superclass.
Example: A cat is a animal.
So a cat only can inherit the
objects of animal class.
Animal
(superclass)
Cat
(subclass)
SAMPLE PROGRAM OF SINGLE INHERITANCE :
class message_super {
Public void display()
{
System.out.println(“I am from superclass”);
}
}
class message_sub extends message_super {
public static void main(String args[])
{
Message_sub message = new message_sub()
Message.display();
}
}
Output: I am from superclass
message_super
I am from superclass
message_sub
MULTILEVEL INHERITANCE
When a class extends a class, and that class extends another
class and its object then it call multilevel inheritance.
Example: a class A is parent class.
Class B is inherit the
objects of class A. Another
one class c is child of class B.
class c can inherit the objects
of class C.
A
B
C
Base class
Intermediary
class
Child class
SAMPLE PROGRAM OF MULTILEVEL INHERITANCE :
class a {
int data= 15; }
class b extends a {
}
class c extends b {
public void display() {
System.out.println(“number is:”+data);
}
public static void main(String args[])
{
c num= new c()
num.display();
}
}
Output: number is: 15
a
Data=15
b
c
HIERARCHICAL INHERITANCE:
When one superclass can be inherited by more than one
subclass then it is called hierarchical inheritance.
Example: a class A is parent class.
Class B is inherit the
objects of class A. Another
one class c also can inherit the
objects of class A.
A
B C
Syntax of Hierarchical inheritance:
Class Food
{
//code
}
class Rice extends Food{
//code
}
class Fruit extends Food{
//code
}
Food
Rice Fruit
Inheritance Method overriding:
When a method is already in parent class but also declaring
in child class, then it is known as Method Overriding.
Note: But when it is ‘Final’ method it cannot overriding.
Example: A parent class of animal
has method ‘eat’. Also a child class
tiger has method ‘eat’.
Animal
eat()
Tiger
eat()
Sample program of method overriding:
class animal {
public void display() {
System.out.println(“I am animal”); }
}
class tiger extends animal {
public void display() {
System.out.println(“I am tiger”);
}
public static void main(String args[]) {
tiger t = new tiger();
t.display(); }
}
Output: I am tiger
Thank you

More Related Content

What's hot

class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
pm2214
 

What's hot (20)

Java package
Java packageJava package
Java package
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Data types in java
Data types in javaData types in java
Data types in java
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Java threads
Java threadsJava threads
Java threads
 
Interface
InterfaceInterface
Interface
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
 
class and objects
class and objectsclass and objects
class and objects
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
Basics of JAVA programming
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 

Similar to Inheritance in java

Similar to Inheritance in java (20)

Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Ganesh groups
Ganesh groupsGanesh groups
Ganesh groups
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 
Inheritance and Interfaces
Inheritance and InterfacesInheritance and Interfaces
Inheritance and Interfaces
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptx
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Java
JavaJava
Java
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance
InheritanceInheritance
Inheritance
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Inheritance
InheritanceInheritance
Inheritance
 
2CPP07 - Inheritance
2CPP07 - Inheritance2CPP07 - Inheritance
2CPP07 - Inheritance
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Recently uploaded

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

Inheritance in java

  • 2. Inheritance: In java inheritance is a mechanism which one object can get all the properties and behaviours of a parent object. In inheritance there is a relationship between two classes. That’s why it is-a relationship. For example: Bike, car, bus this are vehicle. That’s why the child of vehicle are bike, car, bus and vehicle is the parent of this objects. Vehicle Bike, car, bus
  • 3. Java inheritance: • The important of inheritance in java program is code reusability. We can reuse the function of parent class at child class. • In java we use Extends keyword for inheritance. • Subclass and Superclass in inheritance.
  • 4. Inheritance Syntax: class Parent { //code } class Child extends Parent { //code } Parent Child
  • 5. Simple program class super { Public void display() { System.out.println(“I am parent class”); } } class sub extends super { public static void main(String args[]) { sub message = new sub() message.display(); } } Super I am from parent class sub Parent class Child classs Output: I am parent class
  • 6. TYPES OF INHERITANCE:  Single inheritance.  Multiple inheritance.  Multi-level inheritance.  Hierarchical inheritance.  Hybrid inheritance. Note: In java Multiple inheritance is not supported.
  • 7. SINGLE INHERITANCE: Single inheritance is the most easy type to understand. In single inheritance a class extends another only one class. The child class inherit only parents class. The subclass inherit the objects of superclass. Example: A cat is a animal. So a cat only can inherit the objects of animal class. Animal (superclass) Cat (subclass)
  • 8. SAMPLE PROGRAM OF SINGLE INHERITANCE : class message_super { Public void display() { System.out.println(“I am from superclass”); } } class message_sub extends message_super { public static void main(String args[]) { Message_sub message = new message_sub() Message.display(); } } Output: I am from superclass message_super I am from superclass message_sub
  • 9. MULTILEVEL INHERITANCE When a class extends a class, and that class extends another class and its object then it call multilevel inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c is child of class B. class c can inherit the objects of class C. A B C Base class Intermediary class Child class
  • 10. SAMPLE PROGRAM OF MULTILEVEL INHERITANCE : class a { int data= 15; } class b extends a { } class c extends b { public void display() { System.out.println(“number is:”+data); } public static void main(String args[]) { c num= new c() num.display(); } } Output: number is: 15 a Data=15 b c
  • 11. HIERARCHICAL INHERITANCE: When one superclass can be inherited by more than one subclass then it is called hierarchical inheritance. Example: a class A is parent class. Class B is inherit the objects of class A. Another one class c also can inherit the objects of class A. A B C
  • 12. Syntax of Hierarchical inheritance: Class Food { //code } class Rice extends Food{ //code } class Fruit extends Food{ //code } Food Rice Fruit
  • 13. Inheritance Method overriding: When a method is already in parent class but also declaring in child class, then it is known as Method Overriding. Note: But when it is ‘Final’ method it cannot overriding. Example: A parent class of animal has method ‘eat’. Also a child class tiger has method ‘eat’. Animal eat() Tiger eat()
  • 14. Sample program of method overriding: class animal { public void display() { System.out.println(“I am animal”); } } class tiger extends animal { public void display() { System.out.println(“I am tiger”); } public static void main(String args[]) { tiger t = new tiger(); t.display(); } } Output: I am tiger