Types : Single Inheritance Multilevel Inheritance Hierarchical Inheritance Multiple Inheritance
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. A – Parent class B – child class
Sample program : Write a java program to illustrate the concept of single inheritance import java.io.* ; import java.lang .* ; import java.util .* ; // Parent class class One { void display1() { System.out.println (“MEPCO"); } } class Two extends One { public void display2() { System.out.println (“JAVA"); } } // Driver class class Main { public static void main(String[] args ) { Two g = new Two(); g.display1(); g.display2(); } }
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
Write a simple java program to illustrate the concept of Multilevel inheritance : class Name { public void print_firstname () { System.out.println (“ Mepco "); } } Class Middlename extends Name { public void print_middlename () { System.out.println (“ Schnlek "); } } class L astname extends Middlename { public void print_lastname () { System.out.println (“College"); } } // Driver class public class Main { public static void main(String[] args ) { Lastname g = new Lastname (); Calling method from class One g.firstname (); g. middlename (); g. lastname (); } }
Hierarchical Inheritance 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.
Write a simple java program to illustrate the concept of Hierarchial 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"); } } 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 (); } }
Multiple Inheritance : In 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 .
What is Interfaces ? The interface in Java is a mechanism to achieve abstraction . A n interface could only have abstract methods (methods without a body) and public, static, and final variables by default. It is used to achieve abstraction and multiple inheritances in Java. In other words, interfaces primarily define methods that other classes must implement.
Relationship words :
Syntax : interface { // declare constant fields // declare methods that abstract // by default. } In other words,
Rules to declare “interface” class : That means all the methods in an interface are declared with an empty body and are public and all fields are public, static, and final by default. A class that implements an interface must implement all the methods declared in the interface. To implement the interface, use the implements keyword .
Concept #1: W e can have method body in interface. But we need to make it default method. Let's see an example:
interface Interface1 { default void show() { System.out.println (“Greetings!!!"); } } interface Interface2 extends Interface1 { // Abstract method void display(); } // Interface 3 i nterface Interface3 extends Interface1 { // Abstract method void print(); } // Main class class TestClass implements Interface2, Interface3 { // Overriding the abstract method from Interface2 public void display() { System.out.println (“Hello everyone"); } // Overriding the abstract method from Interface3 public void print() { System.out.println (“WELOME TO JAVA WORLD"); } // Main driver method public static void main(String args []) { TestClass d = new TestClass (); // Now calling the methods from both the interfaces d.show (); // Default method from API d.display (); // Overridden method from Interface1 d.print (); // Overridden method from Interface2 } }
Concept #2: We can have static method in Interface and it is allowed to use it in Java.
Concep t 3 : Static Method in Interface i nterface Drawable { void draw(); static int cube( int x) { return x*x*x; } } class Rectangle implements Drawable { public void draw() { System.out.println ( "drawing rectangle" );} } class A{ public static void main(String args []){ Drawable d= new Rectangle(); d.draw ();
Points to remember : One interface can inherit another by the use of keyword “extends”. When a class implements an interface that inherits another interface, it must provide an implementation for all methods required by the interface inheritance chain. A class implements an interface, but one interface extends another interface. It is used to achieve abstraction. By interface, we can support the functionality of multiple inheritance.
Abstract class Interface 1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods. Since Java 8, it can have default and static methods also. 2) Abstract class doesn't support multiple inheritance . Interface supports multiple inheritance . 3) Abstract class can have final, non-final, static and non-static variables . Interface has only static and final variables . 4) Abstract class can provide the implementation of interface . Interface can't provide the implementation of abstract class . 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface. 6) An abstract class can extend another Java class and implement multiple Java interfaces. An interface can extend another Java interface only. 7) An abstract class can be extended using keyword "extends". An interface can be implemented using keyword "implements". 8) A Java abstract class can have class members like private, protected, etc. Members of a Java interface are public by default. 9) Example: public abstract class Shape{ public abstract void draw(); } Example: public interface Drawable{ void draw(); }
Points Abstract Class Interface Type of Methods Can have both abstract and concrete methods Can have only abstract methods (until Java 7), and from Java 8, can have default and static methods, and from Java 9, can have private methods. Variables Can have final, non-final, static, and non-static variables. Only static and final variables Inheritance Can extend only one class (abstract or not) A class can implement multiple interfaces Constructors Can have constructors Cannot have constructors Implementation Can provide the implementation of interface methods Cannot provide the implementation of abstract class methods
Practice program 1 : Create a Drawable interface has only one method “draw”. Its implementation is provided by Rectangle and Circle classes.
Source code : interface Drawable{ void draw(); } //Implementation: by second user class Rectangle implements Drawable{ public void draw(){ System.out.println ("drawing rectangle");} } class Circle implements Drawable{ public void draw(){ System.out.println ("drawing circle");} } //Using interface: by third user class TestInterface1{ public static void main(String args []){ Drawable d=new Circle(); d.draw (); }}
interface Drawable{ void draw(); default void msg (){ System.out.println ( "default method" );} } class Rectangle implements Drawable{ public void draw() { System.out.println ( "drawing rectangle" );} } class TestInterfaceDefault { public static void main(String args []){ Drawable d= new Rectangle(); d.draw (); d.msg(); }}
Practice program 2 : Let's see another example of java interface which provides the implementation of Bank interface.
interface Bank{ float rateOfInterest (); } class SBI implements Bank{ public float rateOfInterest (){ return 9 .15f;} } class PNB implements Bank{ public float rateOfInterest (){ return 9 .7f;} } class TestInterface2{ public static void main(String[] args ){ Bank b= new SBI(); System.out.println ( "ROI: " + b.rateOfInterest ()); }}