SlideShare a Scribd company logo
1 of 26
INHERITANCE
IN JAVA
Content
•What is Inheritance?
•Basic Example of Inheritance in Java
•Types Of Inheritance
•Inheritance By Animal
What Is Inheritance?
Inheritance in java is a
mechanism in which one
object acquires all the
properties and behaviors
of parent object.
Inheritance in Java can be best
understood in terms of Parent
and Child relationship, also
known as Super class(Parent)
and Sub class(child) in Java
language. Inheritance defines a
relationship between a Super
class and its Sub class. extends
and implements keywords are
used to describe inheritance in
Java
BASIC EXAMPLE OF
INHERITANCE IN JAVA
//Creating a SubClass
class A{
int a=10, b=20;
void showab(){
System.out.println(“a: “+a+ “b: ”+b);
}
}
//Creating a SubClass by extanding A class
class B extends A{
void sum(){
System.out.println(“Sum of a and b is: ”+(a+b));
}
}
//Creating Main Class
class Main{
public static void main(String args[]){
A obj1= new A();
obj1.showab();
B obj2= new B();
obj2.sum();
}
}
Output
Inheritance
• As you can see, the subclass B
include all the members of its
super class, A.
• This is why class B can access
“a” and “b” and call showab().
• Also, include sum(), “a” and
“b” can be referred directly, as
if they were part of B class.
• Even though A is super class
for B, it is also a completely
independent, stand alone
class.
TYPES OF
INHERITANCE
TYPES OF INHERITANCE
Based on number of ways inheriting the feature of
base class into derived class we have some types of
inheritance; they are:
• Single inheritance
• Multilevel inheritance
• Hierarchical inheritance
SINGLE INHERITANCE
When a class extends
another one class only then
we call it a single
inheritance.The below flow
diagram shows that class B
extends only one class
which is A. Here A is a
parent class of B and B
would be a child class of A
MULTIPLE INHERITANCE
Here class C inherits class B
and class B inherits class A
which means B is a parent
class of C andA 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.
Hierarchical Inheritance
In this inheritance multiple classes inherits from a single class i.e
there is one super class and multiple sub classes. As we can see
from the below diagram when a same class is having more than
one sub class (or) more than one sub class has the same parent is
called as Hierarchical Inheritance.
TYPES OF INHERITANCE
INHERITANCE BY
ANIMAL
SAMPLE PROGRAM
public class Animal{
}
public class Amphibians extends Animal{
}
public class Reptiles extends Animal{
}
public class Mammals extends Animal{
}
public class Birds extends Animal{
}
public class Main{
public static void main(String args[]){
// write your code
}
}
INHERITANCE BY
ANIMAL
Main Program
public class Animal{
eat();
reproduce();
sleep();
}
public class Amphibians extends Animal {
swim();
}
public class Reptiles extends Animal{
crawl();
}
public class Mammals extends Animal{
giveMilk();
}
public class Birds extends Animal {
fly();
}
public class Main{
public static void main (String args[] ){
Amphibians obj1=new Amphibians();
Reptiles obj2=new Reptiles();
Mammals obj3=new Mammals();
Birds obj4=new Birds();
}
}
THANKYOU
Any Suggestion Or Question IsWelcome

More Related Content

What's hot

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Paumil Patel
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in javaVishnu Suresh
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaManish Sahu
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vineeta Garg
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming languageMd.Al-imran Roton
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionPritom Chaki
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods javaPadma Kannan
 
Abstraction java
Abstraction javaAbstraction java
Abstraction javaMahinImran
 

What's hot (20)

Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Object Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaionObject Oriented Programing JAVA presentaion
Object Oriented Programing JAVA presentaion
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Abstraction java
Abstraction javaAbstraction java
Abstraction java
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 

Similar to Inheritance In Java

Similar to Inheritance In Java (20)

Inheritance
InheritanceInheritance
Inheritance
 
E -COMMERCE.ppt
E -COMMERCE.pptE -COMMERCE.ppt
E -COMMERCE.ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Java
JavaJava
Java
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java(inheritance)
Java(inheritance)Java(inheritance)
Java(inheritance)
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
Programming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptxProgramming Lesson by Slidesgo.pptx
Programming Lesson by Slidesgo.pptx
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 
All 5 Types of Inheritance in Java | Analytics Jobs
All 5 Types of Inheritance in Java | Analytics JobsAll 5 Types of Inheritance in Java | Analytics Jobs
All 5 Types of Inheritance in Java | Analytics Jobs
 
Inheritance
InheritanceInheritance
Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Recently uploaded

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 

Recently uploaded (20)

(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 

Inheritance In Java

  • 2. Content •What is Inheritance? •Basic Example of Inheritance in Java •Types Of Inheritance •Inheritance By Animal
  • 3. What Is Inheritance? Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.
  • 4. Inheritance in Java can be best understood in terms of Parent and Child relationship, also known as Super class(Parent) and Sub class(child) in Java language. Inheritance defines a relationship between a Super class and its Sub class. extends and implements keywords are used to describe inheritance in Java
  • 6. //Creating a SubClass class A{ int a=10, b=20; void showab(){ System.out.println(“a: “+a+ “b: ”+b); } } //Creating a SubClass by extanding A class class B extends A{ void sum(){ System.out.println(“Sum of a and b is: ”+(a+b)); } } //Creating Main Class class Main{ public static void main(String args[]){ A obj1= new A(); obj1.showab(); B obj2= new B(); obj2.sum(); } }
  • 8. Inheritance • As you can see, the subclass B include all the members of its super class, A. • This is why class B can access “a” and “b” and call showab(). • Also, include sum(), “a” and “b” can be referred directly, as if they were part of B class. • Even though A is super class for B, it is also a completely independent, stand alone class.
  • 9.
  • 10.
  • 12. TYPES OF INHERITANCE Based on number of ways inheriting the feature of base class into derived class we have some types of inheritance; they are: • Single inheritance • Multilevel inheritance • Hierarchical inheritance
  • 13. SINGLE INHERITANCE When a class extends another one class only then we call it a single inheritance.The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be a child class of A
  • 14.
  • 15. MULTIPLE INHERITANCE Here class C inherits class B and class B inherits class A which means B is a parent class of C andA 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.
  • 16.
  • 17. Hierarchical Inheritance In this inheritance multiple classes inherits from a single class i.e there is one super class and multiple sub classes. As we can see from the below diagram when a same class is having more than one sub class (or) more than one sub class has the same parent is called as Hierarchical Inheritance.
  • 18.
  • 21.
  • 22. SAMPLE PROGRAM public class Animal{ } public class Amphibians extends Animal{ } public class Reptiles extends Animal{ } public class Mammals extends Animal{ } public class Birds extends Animal{ } public class Main{ public static void main(String args[]){ // write your code } }
  • 23.
  • 25. public class Animal{ eat(); reproduce(); sleep(); } public class Amphibians extends Animal { swim(); } public class Reptiles extends Animal{ crawl(); } public class Mammals extends Animal{ giveMilk(); } public class Birds extends Animal { fly(); } public class Main{ public static void main (String args[] ){ Amphibians obj1=new Amphibians(); Reptiles obj2=new Reptiles(); Mammals obj3=new Mammals(); Birds obj4=new Birds(); } }
  • 26. THANKYOU Any Suggestion Or Question IsWelcome