In this tutorial, we will learn about Java inheritance and its types with example.
One of the key features of OOP is inheritance, which allows us to create a new class from an existing one.
- The newly created class is referred to as a subclass (child or derived class), and the existing class from which the child class is derived is referred to as a superclass (parent or base class).
- In Java, inheritance is a mechanism that allows one object to inherit all of the properties and behaviors of a parent object. It is an essential component of OOPs (Object Oriented programming systems).
In Java, inheritance means that you can create new classes that are based on existing classes. When you inherit from an existing class, you can reuse the parent class’s methods and fields. You can also add new methods and fields to your existing class.
The IS-A relationship, also known as a parent-child relationship, is represented by inheritance. |
- In Java, the extends keyword is used to perform inheritance. As an example:
class Sample{ // methods and fields } // use of extends keyword // to perform inheritance class Basic extends Sample { // methods and fields of Sample // methods and fields of Basic }
- In the preceding example, the Basic class is created by inheriting the Sample class’s methods and fields.
- Basic is the subclass in this case, and Sample is the superclass.
Contents
Why we use inheritance in java?
In Java, inheritance means that you can create new classes that are based on existing classes. When you inherit from an existing class, you can reuse the parent class’s methods and fields. You can also add new methods and fields to your existing class.
- For Overriding Methods.
- For reusability of code.
Terminologies used in inheritance
- Class: A class is a collection of objects with similar properties. It’s a blueprint or template from which objects are made.
- Subclasses/Child Classes: A subclass is a class that inherits from another. A derived class, extended class, or child class is another name for it.
- Superclass / Parentclass: A superclass (also known as a parent class) is the class from which a subclass inherits its features. It’s also known as a parent class or a base class.
- Reusability: As the name implies, reusability is a mechanism that allows you to reuse existing class fields and methods when creating a new class. The fields and methods defined in the previous class can be reused.
Syntax:
class Subclass-name extends Superclass-name { //methods and fields } |
- When you use the extends keyword, it means you’re creating a new class that inherits from an existing one. The word “extends” means “to increase functionality.”
- In Java, an inherited class is referred to as a parent or superclass, and the new class is referred to as a child or subclass.
Types of inheritance in java
In Java, there are three types of inheritance based on class:
- Single,
- Multilevel,
- Hierarchical inheritance.
- Multiple and hybrid inheritance are only supported through interfaces in Java programming. Interfaces will be covered later.
1. Single Inheritance
A single subclass extends from a single superclass in single inheritance. |
A derived class can inherit properties and behavior from a single parent class through single inheritance. It allows a derived class to inherit the properties and behavior of a base class, allowing for code reusability as well as the addition of new features to existing code.
class Sample{ void eat(){System.out.println("eating");} } class Basic extends Sample{ void drink(){System.out.println("drinking");} } class Inheritance{ public static void main(String args[]){ Basic b=new Basic(); b.drink(); b.eat(); }}
Output:
drinking eating |
2. Multilevel Inheritance
A subclass extends from a superclass and then acts as a superclass for another class in multilevel inheritance. |
A class that extends to another class that is already extended from another class in Java is called Multi-Level Inheritance. For example, if class A extends a class B, and class B extends from another class C, this scenario is known as Multi-level Inheritance.
class Sample{ void eat(){System.out.println("eating");} } class Basic extends Sample{ void drink(){System.out.println("drinking");} } class Demo extends Basic{ void demo1(){System.out.println("DevelopersDome");} } class Inheritance{ public static void main(String args[]){ Demo d=new Demo(); d.demo1(); d.drink(); d.eat(); }}
Output:
DevelopersDome drinking eating |
3. Hierarchical Inheritance
Multiple subclasses extend from a single superclass in hierarchical inheritance. |
In Java, hierarchical inheritance is one of the inheritance types. Multiple child classes inherit a single class in Hierarchical Inheritance, or a single class is inherited by multiple child classes.
class Sample{ void eat(){System.out.println("eating");} } class Basic extends Sample{ void drink(){System.out.println("drinking");} } class Demo extends Sample{ void demo1(){System.out.println("DevelopersDome");} } class Inheritance{ public static void main(String args[]){ Demo d=new Demo(); d.demo1(); d.eat(); }}
Output:
DevelopersDome eating |
4. Multiple Inheritance
A single subclass extends from multiple superclasses in multiple inheritance. |
Multiple Inheritance is an object-oriented concept that allows a class to inherit properties from multiple parent classes.
Why multiple inheritance is not supported in Java?
- Multiple inheritance is not supported in Java to reduce complexity and simplify the language.
- Consider the following scenario: A, B, and C are three classes. The A and B classes are inherited by the C class. If A and B classes have the same method and you call it from a child class object, it will be unclear whether you are calling the method of A or B.
- Because compile-time errors are preferable to runtime errors, Java generates a compile-time error if you inherit two classes. So, whether you use the same method or a different one, a compile time error will occur.
5. Hybrid Inheritance
Hybrid inheritance is one that combines two or more types of inheritance. |
In Java, a hybrid inheritance is a combination of inheritances. More than one type of inheritance is observed in this type of inheritance. For example, if we have classes A and B that extend class C, and then another class D that extends class A, we have Hybrid Inheritance.
Method Overriding in Java Inheritance
In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a specific implementation of a method declared by one of its parent classes.
Example: Method Overriding in Java Inheritance
class Sample { // method in the superclass public void eat() { System.out.println("I can eat"); } } // Basic inherits Sample class Basic extends Sample { // overriding the eat() method @Override public void eat() { System.out.println("I eat basic food"); } // new method in subclass public void drink() { System.out.println("I can drink"); } } class Main { public static void main(String[] args) { // create an object of the subclass Basic x = new Basic(); // call the eat() method x.eat(); x.drink(); } }
Output:
I eat basic food I can drink |
- The eat() method is present in both the superclass Sample and the subclass Basic in the preceding example.
- We’ve created a x Basic object here.
- When we use the object x to call eat(), the method inside Basic is called. This is due to the fact that the derived class’s method overrides the base class’s method.
Super Keyword in Java Inheritance
In Java, the super keyword is a reference variable that refers to an immediate parent class object. When you create a subclass instance, you’re also creating an instance of the parent class, which is referred to by the super reference variable.
The super keyword is used to call the parent class’s method from the child class’s method. |
Example: Super Keyword in Java Inheritance
class Sample { // method in the superclass public void eat() { System.out.println("I can eat"); } } // Basic inherits Sample class Basic extends Sample { // overriding the eat() method @Override public void eat() { // call method of superclass super.eat(); System.out.println("I eat basic food"); } // new method in subclass public void drink() { System.out.println("I can drink"); } } class Main { public static void main(String[] args) { // create an object of the subclass Basic x = new Basic(); // call the eat() method x.eat(); x.drink(); } }
Output:
I can eat I eat basic food I can drink |
The eat() method is present in both the base class Sample and the derived class Basic in the example above. Take note of the statement:
super.eat(); |
- The super keyword is used here to invoke the superclass eat() method.
- The super keyword can also be used to call the superclass constructor from the subclass constructor.
multiple inheritance in java, types of inheritance in java, multilevel inheritance in java, hierarchical inheritance in java, inheritance program in java, why multiple inheritance are not supported in java, single inheritance in java, hybrid inheritance in ,all classes in are inherited from which class
You may like:
Java this Keyword with Example
Java final Keyword with Example
Hope this article will guide you to recognize all about the Java Inheritance with Example and Types of inheritance that you needed and still if you have any problem or queries regarding this, post them in the comments section and we will be glad to assist you.
Pingback: Java Method Overriding with Example - Developers Dome
Pingback: Java Interface with Example - Developers Dome
Pingback: Not Equal Example in Java - Developers Dome
Pingback: Java Polymorphism with Example - Developers Dome
Pingback: Encapsulation in Java with Example - Developers Dome
Pingback: Java Nested and Inner Class with Example - Developers Dome
Pingback: Java Anonymous Class with Example - Developers Dome
Pingback: Singleton Class in Java with Implementation and Example
Pingback: Java enum Constructor with Example - Developers Dome
Pingback: Java enum with Example - Developers Dome
Pingback: Try-with-resources in java with Example - Developers Dome