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

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 isinheritance?  Inheritance necessity  Terminologies  Syntax  Types of inheritance  Java access modifiers and access control  Examples  References 2/17/2024 2 naeem_mia
  • 3.
     Inheritance inJava 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: Classis 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-classextends 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 inthe 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
  • 8.
  • 9.
     In javaprogramming, multiple and hybrid inheritance is supported through interface only. We will learn about interfaces later. 2/17/2024 9 naeem_mia
  • 10.
  • 11.
  • 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
  • 16.
  • 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
  • 18.

Editor's Notes

  • #13 Filename: TestIheritance.java
  • #14 Filename: TestInheritance2.java