SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
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

PPTX
Inheritance in java
PPTX
Inheritance in JAVA PPT
PPTX
Inheritance in OOPS
PPTX
Control structures in java
PPTX
Interface in java
PPTX
Java(Polymorphism)
PPTX
Inheritance ppt
PPTX
oops concept in java | object oriented programming in java
Inheritance in java
Inheritance in JAVA PPT
Inheritance in OOPS
Control structures in java
Interface in java
Java(Polymorphism)
Inheritance ppt
oops concept in java | object oriented programming in java

What's hot (20)

PPTX
Classes, objects in JAVA
PPTX
Constructor in java
PPT
Basic concept of OOP's
PPTX
Methods in java
PPTX
Type casting in java
PPTX
Method overloading
PDF
Arrays in Java
PPT
Function overloading(c++)
PPT
Abstract class in java
PPTX
Java package
PDF
Function overloading ppt
PPTX
Super keyword in java
PPT
Method overriding
PPTX
Characteristics of OOPS
PPTX
Polymorphism in c++(ppt)
PPTX
Java exception handling
PDF
Constructor and Destructor
PPTX
Inheritance in c++
PPT
Class and object in C++
Classes, objects in JAVA
Constructor in java
Basic concept of OOP's
Methods in java
Type casting in java
Method overloading
Arrays in Java
Function overloading(c++)
Abstract class in java
Java package
Function overloading ppt
Super keyword in java
Method overriding
Characteristics of OOPS
Polymorphism in c++(ppt)
Java exception handling
Constructor and Destructor
Inheritance in c++
Class and object in C++
Ad

Similar to Inheritance in java (20)

PPTX
Inheritance,single,multiple.access rulepptx
PDF
java inheritance that is used in oop cls
PDF
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
PPTX
Inheritance In Java
PPTX
Inheritance
DOCX
Ganesh groups
PPTX
Inheritance in Java - An Introduction & types
PPTX
INHERTANCE , NARROW AND WIDENING
PPTX
Non Text Magic Studio Magic Design for Presentations L&P (2).pptx
PPT
web program-Inheritance,pack&except in Java.ppt
PPTX
Inheritance
PDF
Inheritance in Java.pdf
PPTX
Inheritance and Interfaces
PPTX
Programming Lesson by Slidesgo.pptx
PPTX
inheritance and interface in oops with java .pptx
PDF
Inheritance
PPTX
PDF
Inheritance used in java
PPTX
Inheritance in java
PPTX
Inheritance Super and MRO _
Inheritance,single,multiple.access rulepptx
java inheritance that is used in oop cls
efrecdcxcfxfr125002231fewdsdsfxcdfe25.pdf
Inheritance In Java
Inheritance
Ganesh groups
Inheritance in Java - An Introduction & types
INHERTANCE , NARROW AND WIDENING
Non Text Magic Studio Magic Design for Presentations L&P (2).pptx
web program-Inheritance,pack&except in Java.ppt
Inheritance
Inheritance in Java.pdf
Inheritance and Interfaces
Programming Lesson by Slidesgo.pptx
inheritance and interface in oops with java .pptx
Inheritance
Inheritance used in java
Inheritance in java
Inheritance Super and MRO _
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Business Ethics Teaching Materials for college
PDF
RMMM.pdf make it easy to upload and study
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Pre independence Education in Inndia.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
master seminar digital applications in india
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Anesthesia in Laparoscopic Surgery in India
human mycosis Human fungal infections are called human mycosis..pptx
Week 4 Term 3 Study Techniques revisited.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Business Ethics Teaching Materials for college
RMMM.pdf make it easy to upload and study
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
Pre independence Education in Inndia.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
Supply Chain Operations Speaking Notes -ICLT Program
master seminar digital applications in india
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Basic Mud Logging Guide for educational purpose
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O7-L3 Supply Chain Operations - ICLT Program

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