Java Inheritance Types

Java Inheritance Types
Slide Note
Embed
Share

The different types of inheritance supported by Java, such as Single Inheritance, Multilevel Inheritance, and Hierarchical Inheritance. See examples and diagrams illustrating how classes inherit properties and behavior from parent and grandparent classes.

  • Java Inheritance
  • Single Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Class Diagrams

Uploaded on Apr 29, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Java Inheritance Types Below are the different types of inheritance which are supported by Java. 1.Single Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance

  2. Single Inheritance In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single- parent class. Sometimes, it is also known as simple inheritance. In the below figure, A is a parent class and B is a child class. The class B inherits all the properties of the class A .

  3. class Employee { float salary=34534*12; } public class Executive extends Employee { float bonus=3000*6; public static void main(String args[]) { Executive obj=new Executive(); System.out.println("Total salary credited: "+obj.salary); System.out.println("Bonus of six months: "+obj.bonus); } }

  4. 2. Multilevel Inheritance In Multilevel Inheritance, a derived class will be inheriting a base class, and as well as the derived class also acts as the base class for other classes. In the below image, class A serves as a base class for the derived class B, which in turn serves as a base class for the derived class C. In Java, a class cannot directly access the grandparent s members.

  5. // Importing required libraries import java.io.*; import java.lang.*; import java.util.*; // Parent class One class One { // Method to print "Geeks" public void print_geek() { System.out.println("Geeks"); } } // Child class Two inherits from class One class Two extends One { // Method to print "for" public void print_for() { System.out.println("for"); } }

  6. // Child class Three inherits from class Two class Three extends Two { // Method to print "Geeks" public void print_lastgeek() { System.out.println("Geeks"); } } // Driver class public class Main { public static void main(String[] args) { // Creating an object of class Three Three g = new Three(); // Calling method from class One g.print_geek(); // Calling method from class Two g.print_for(); // Calling method from class Three g.print_lastgeek(); }

  7. 3. Hierarchical Inheritance If a number of classes are derived from a single base class, it is called hierarchical inheritance. Or In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one subclass. In the below image, class A serves as a base class for the derived classes B, C, and D.

  8. // Java program to illustrate the // concept of Hierarchical inheritance class A { public void print_A() { System.out.println("Class A"); } } class B extends A { public void print_B() { System.out.println("Class B"); } } class C extends A { public void print_C() { System.out.println("Class C"); } } class D extends A { public void print_D() { System.out.println("Class D"); } }

  9. // Driver Class public class Test { public static void main(String[] args) { B obj_B = new B(); obj_B.print_A(); obj_B.print_B(); C obj_C = new C(); obj_C.print_A(); obj_C.print_C(); D obj_D = new D(); obj_D.print_A(); obj_D.print_D(); } }

  10. 4.Multiple Inheritance (Through Interfaces) Multiple inheritances, one class can have more than one superclass and inherit features from all parent classes. Please note that Java does not support multiple inheritances with classes. In Java, we can achieve multiple inheritances only through Interfaces. In the image below, Class C is derived from interfaces A and B.

  11. import java.io.*; import java.lang.*; import java.util.*; interface One { public void print_geek(); } interface Two { public void print_for(); } interface Three extends One, Two { public void print_geek(); } class Child implements Three { @Override public void print_geek() { System.out.println("Geeks"); }

  12. public void print_for() { System.out.println("for"); } } // Drived class public class Main { public static void main(String[] args) { Child c = new Child(); c.print_geek(); c.print_for(); c.print_geek(); } }

  13. 5. Hybrid Inheritance Hybrid means consist of more than one. Hybrid inheritance is the combination of two or more types of inheritance.

  14. //parent class class GrandFather { public void show() { System.out.println("I am grandfather."); } } //inherits GrandFather properties class Father extends GrandFather { public void show() { System.out.println("I am father."); } }

  15. //inherits Father properties class Son extends Father { public void show() { System.out.println("I am son."); } }

  16. //inherits Father properties public class Daughter extends Father { public void show() { System.out.println("I am a daughter."); } public static void main(String args[]) { Daughter obj = new Daughter(); obj.show(); } }

More Related Content