SlideShare a Scribd company logo
1 of 18
Naeem Mia
Lecturer, Dept. of CSE
International University of Business Agriculture and
Technology
 What is inheritance?
 Inheritance necessity
 Terminologies
 Syntax
 Types of inheritance
 Java access modifiers and access control
 Examples
 References
2/17/2024 2
naeem_mia
 Inheritance in Java is a mechanism in which one
object acquires all the properties and behaviors of a
parent object. It is an important part of OOPs (Object
Oriented programming system).
 Inheritance represents the IS-A relationship which is
also known as a parent-child relationship.
Animal
Cat Dog
Animal has some common
features
Dog has the features
of animal
Cat has the features of
animal
2/17/2024 3
naeem_mia
 Code Reusability: The code written in the Superclass
is common to all subclasses. Child classes can directly
use the parent class code.
 Method Overriding: Method Overriding is
achievable only through Inheritance. It is one of the
ways by which Java achieves Run Time Polymorphism.
 Abstraction: The concept of abstract where we do not
have to provide all details is achieved through
inheritance. Abstraction only shows the functionality
to the user.
2/17/2024 4
naeem_mia
 Class: Class is a set of objects which shares common characteristics/ behavior and
common properties/ attributes. Class is not a real-world entity. It is just a template or
blueprint or prototype from which objects are created.
 Super Class/Parent Class: The class whose features are inherited is known as a
superclass(or a base class or a parent class).
 Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a
derived class, extended class, or child class). The subclass can add its own fields and
methods in addition to the superclass 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.
2/17/2024 5
naeem_mia
 class derived-class extends base-class
{
//methods and fields
}
The extends keyword indicates that you are making a
new class that derives from an existing class. The
meaning of "extends" is to increase the functionality.
2/17/2024 6
naeem_mia
As displayed in the above figure, Programmer is the subclass and Employee is
the superclass. The relationship between the two classes is Programmer IS-
A Employee. It means that Programmer is a type of Employee.
2/17/2024 7
naeem_mia
2/17/2024 8
naeem_mia
 In java programming, multiple and hybrid inheritance is supported
through interface only. We will learn about interfaces later.
2/17/2024 9
naeem_mia
2/17/2024 10
naeem_mia
2/17/2024 11
naeem_mia
 class Animal{
 void eat(){System.out.println("eating...");}
 }
 class Dog extends Animal{
 void bark(){System.out.println("barking...");}
 }
 class TestInheritance{
 public static void main(String args[]){
 Dog d=new Dog();
 d.bark();
 d.eat();
 }
 }
Output:
barking...
eating...
2/17/2024 12
naeem_mia
 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...
2/17/2024 13
naeem_mia
 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();
 //c.bark();//C.T.Error
 }
 }
Output:
meowing...
eating...
2/17/2024 14
naeem_mia
 class A{
 void msg(){System.out.println("Hello");}
 }
 class B{
 void msg(){System.out.println("Welcome");}
 }
 class C extends A,B{//suppose if it were

 public static void main(String args[]){
 C obj=new C();
 obj.msg();//Now which msg() method would be invoked?
 }
 }
Output:
Compile Time Error
2/17/2024 15
naeem_mia
2/17/2024 16
naeem_mia
 https://www.javatpoint.com/inheritance-in-java
 http://www.btechsmartclass.com/java/java-
inheritance-basics.html
 https://www.geeksforgeeks.org/inheritance-in-java/
 https://en.wikipedia.org/wiki/Inheritance
2/17/2024 17
naeem_mia
2/17/2024 18
naeem_mia

More Related Content

Similar to Inheritance in Java beginner to advance with examples.pptx

SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 

Similar to Inheritance in Java beginner to advance with examples.pptx (20)

Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING INHERTANCE , NARROW AND WIDENING
INHERTANCE , NARROW AND WIDENING
 
INHERITANCE.pptx
INHERITANCE.pptxINHERITANCE.pptx
INHERITANCE.pptx
 
Inheritance
InheritanceInheritance
Inheritance
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
 
Chapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).pptChapter 5 (OOP Principles).ppt
Chapter 5 (OOP Principles).ppt
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
Java OOPS Concept
Java OOPS ConceptJava OOPS Concept
Java OOPS Concept
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
OOP in Java Presentation.pptx
OOP in Java Presentation.pptxOOP in Java Presentation.pptx
OOP in Java Presentation.pptx
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdfphp_final_sy_semIV_notes_vision.pdf
php_final_sy_semIV_notes_vision.pdf
 
php_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdfphp_final_sy_semIV_notes_vision (3).pdf
php_final_sy_semIV_notes_vision (3).pdf
 

Recently uploaded

SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
CaitlinCummins3
 

Recently uploaded (20)

Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
The Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptxThe Ball Poem- John Berryman_20240518_001617_0000.pptx
The Ball Poem- John Berryman_20240518_001617_0000.pptx
 
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
Basic Civil Engineering notes on Transportation Engineering, Modes of Transpo...
 
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING IIII BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
II BIOSENSOR PRINCIPLE APPLICATIONS AND WORKING II
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Improved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio AppImproved Approval Flow in Odoo 17 Studio App
Improved Approval Flow in Odoo 17 Studio App
 
Benefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptxBenefits and Challenges of OER by Shweta Babel.pptx
Benefits and Challenges of OER by Shweta Babel.pptx
 

Inheritance in Java beginner to advance with examples.pptx

  • 1. Naeem Mia Lecturer, Dept. of CSE International University of Business Agriculture and Technology
  • 2.  What is inheritance?  Inheritance necessity  Terminologies  Syntax  Types of inheritance  Java access modifiers and access control  Examples  References 2/17/2024 2 naeem_mia
  • 3.  Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).  Inheritance represents the IS-A relationship which is also known as a parent-child relationship. Animal Cat Dog Animal has some common features Dog has the features of animal Cat has the features of animal 2/17/2024 3 naeem_mia
  • 4.  Code Reusability: The code written in the Superclass is common to all subclasses. Child classes can directly use the parent class code.  Method Overriding: Method Overriding is achievable only through Inheritance. It is one of the ways by which Java achieves Run Time Polymorphism.  Abstraction: The concept of abstract where we do not have to provide all details is achieved through inheritance. Abstraction only shows the functionality to the user. 2/17/2024 4 naeem_mia
  • 5.  Class: Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. Class is not a real-world entity. It is just a template or blueprint or prototype from which objects are created.  Super Class/Parent Class: The class whose features are inherited is known as a superclass(or a base class or a parent class).  Sub Class/Child Class: The class that inherits the other class is known as a subclass(or a derived class, extended class, or child class). The subclass can add its own fields and methods in addition to the superclass 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. 2/17/2024 5 naeem_mia
  • 6.  class derived-class extends base-class { //methods and fields } The extends keyword indicates that you are making a new class that derives from an existing class. The meaning of "extends" is to increase the functionality. 2/17/2024 6 naeem_mia
  • 7. As displayed in the above figure, Programmer is the subclass and Employee is the superclass. The relationship between the two classes is Programmer IS- A Employee. It means that Programmer is a type of Employee. 2/17/2024 7 naeem_mia
  • 9.  In java programming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later. 2/17/2024 9 naeem_mia
  • 12.  class Animal{  void eat(){System.out.println("eating...");}  }  class Dog extends Animal{  void bark(){System.out.println("barking...");}  }  class TestInheritance{  public static void main(String args[]){  Dog d=new Dog();  d.bark();  d.eat();  }  } Output: barking... eating... 2/17/2024 12 naeem_mia
  • 13.  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... 2/17/2024 13 naeem_mia
  • 14.  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();  //c.bark();//C.T.Error  }  } Output: meowing... eating... 2/17/2024 14 naeem_mia
  • 15.  class A{  void msg(){System.out.println("Hello");}  }  class B{  void msg(){System.out.println("Welcome");}  }  class C extends A,B{//suppose if it were   public static void main(String args[]){  C obj=new C();  obj.msg();//Now which msg() method would be invoked?  }  } Output: Compile Time Error 2/17/2024 15 naeem_mia
  • 17.  https://www.javatpoint.com/inheritance-in-java  http://www.btechsmartclass.com/java/java- inheritance-basics.html  https://www.geeksforgeeks.org/inheritance-in-java/  https://en.wikipedia.org/wiki/Inheritance 2/17/2024 17 naeem_mia

Editor's Notes

  1. Filename: TestIheritance.java
  2. Filename: TestInheritance2.java