KEMBAR78
Types of inheritance in java | PPTX
Types of inheritance in java
Prof. Neeraj Bhargava
Kapil Chauhan
Department of Computer Science
School of Engineering & Systems Sciences
MDS University, Ajmer
Introduction
 On the basis of class, there can be three types of
inheritance in java: single, multilevel and hierarchical.
 In java programming, multiple and hybrid inheritance
is supported through interface only.
Types of Inheritance
Cont..
Single Inheritance Example
 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();
 }}
Multilevel Inheritance Example
 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();
 }}
Hierarchical Inheritance Example
 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();
 }}
Assignment
 Explain Types of Inheritance in java with example.

Types of inheritance in java

  • 1.
    Types of inheritancein java Prof. Neeraj Bhargava Kapil Chauhan Department of Computer Science School of Engineering & Systems Sciences MDS University, Ajmer
  • 2.
    Introduction  On thebasis of class, there can be three types of inheritance in java: single, multilevel and hierarchical.  In java programming, multiple and hybrid inheritance is supported through interface only.
  • 3.
  • 4.
  • 5.
    Single Inheritance Example 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();  }}
  • 6.
    Multilevel Inheritance Example 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();  }}
  • 7.
    Hierarchical Inheritance Example 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();  }}
  • 8.
    Assignment  Explain Typesof Inheritance in java with example.