JavaGet all about inheritance in Java with all 5 types

Get all about inheritance in Java with all 5 types

what is inheritance in OOPS ?

Inheritance is a mechanism in which the child class acquires parent class properties.

Why we use inheritance in our project or code ?

Inheritance supports the idea of “reusability”, i.e. When we already have a code in other class that we required in our class, So we can derive new class fro existing class. By doing this we can reuse the methods and variable of the existing class.

Can you show how parent / super class features are inherited in the child / sub-class

Yes sure you can understand by below example

package com.onurdesk.oop.inheritance;

class Car {
	void run() {
		System.out.println("Car is running ");
	}
}

class Audi extends Car {

}

public class CarTesting {

	public static void main(String[] args) {
		
		Audi audi = new Audi();
		audi.run();

	}

}

OUTPUT

Ok So, Which keyword is use for achieving inheritance ?

  • extends
  • implements

Classes are use extends keyword for inherit the properties from other class.

Class always use implements keyword for use of interface into class.

Types of Inheritance in Java

  • Single Inheritance 
  • Multilevel Inheritance 
  • Hierarchical Inheritance 

Single Inheritance

In single inheritance, the features of one superclass are inherited by subclasses. Class A behaves as the base class for the derived class B in the picture above.

package com.onurdesk.oop.inheritance;

class A {
	void print_onyour() {
		System.out.println("OnUr");
	}
}

class B extends A {
	void print_desk() {
		System.out.println("Desk");
	}
}

public class Main {

	public static void main(String[] args) {

		B b = new B();
		b.print_onyour();
		b.print_desk();

	}

}
OUTPUT: 
OnUr
Desk

Multilevel Inheritance 

In Multilevel Inheritance, a derived class may inherit a base class and also act as the base class to another class as well as the derived class. Class A serves as the base class of derived class B in the picture below, which in turn serves as the base class of derived class C. In Java, a class is unable to contact representatives of the grandparent directly.

package com.onurdesk.oop.inheritance;

class A {
	void print_onyour() {
		System.out.println("OnUr");
	}
}

class B extends A {
	void print_desk() {
		System.out.println("Desk");
	}
}

class C extends B{
	void print_tagline() {
		System.out.println("For all your desire");
	}
}
public class Main {
	public static void main(String[] args) {
		C b = new C();
		b.print_onyour();
		b.print_desk();
		b.print_tagline();
	}
}

OUTPUT: 
OnUr
Desk
For all your desire

Hierarchical Inheritance

One class serves as a superclass (base class) with more than one subclass of Hierarchical Inheritance. Class A serves as a base class for the derived classes B , C and D in the picture below.

package com.onurdesk.oop.inheritance;

class A {
	void print_onyour() {
		System.out.println("OnUr");
	}
}

class B extends A {
	void print_desk() {
		System.out.println("Desk");
	}
}
class C extends A{
	void print_tagline() {
		System.out.println("For all your desire");
	}
}
public class Main {
	public static void main(String[] args) {
		C c = new C();
		c.print_onyour();
		c.print_tagline();
		B b = new B();
		b.print_desk();
	}
}

Some of the Key points of inheritance

  • Default superclass: Each class has one and only one direct superclass (single inheritance), except for the Object class, which has no superclass. Each class is implicitly a subclass of the Object class, in the absence of any such explicit superclass.  
  • Superclass can only be one: Any number of subclasses will have a superclass. A subset can only have one superclass, though. This is because of classes; Java does not allow multiple inheritances. Multiple inheritances are supported by java, but with interfaces. 
  • Inheriting Constructors: A subclass inherits from its superclass all the representatives (fields, methods, and nested classes). Constructors are not members, so subclasses do not inherit them, but the superclass constructor may be invoked from the subclass. 
  •  Private member inheritance: A subset does not inherit the parent class’s private members. If the superclass, however, has public or protected methods (such as getters and setters) for accessing its private fields, the subclass may still use these. 

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

Latest article

More article