
What is Inheritance in Java:
Inheritance in java is a mechanism by which one class is allowed to inherit the features (fields and methods) of another class.
The class that inherits the feature of other class is known as subclass / child class and the class whose feature is being inherited is known as super class / parent class. The child class get’s the access of fields and methods of parent class after inheriting the parent class.
In other words inheritance allows the child class object to acquire the properties and behavior of Parent class. The child class automatically gains access to all the fields and methods defined in the parent class without needing to redefine them and it can also have its own additional fields and methods.
Real Life example of inheritance:
Inheritance in Java is like a parent passing down their skills and talents to their child. Inheritance is similar to how a parent shares their skills, talents, and knowledge with their child. The child gains access to these abilities from their parent, just like a child class in Java inherits the fields and methods from its parent class.
Additionally, the child can have its own unique skills, just as a child class can have its own additional fields and methods. This process allows the child to acquire a combination of both the parent’s traits and their own individual qualities.
- Let’s take an example here :
// Superclass (Parent Class)
class Parent {
// Common skill shared by all instances of Parent class
public void sharedSkill() {
System.out.println("Parent's shared skill: Cooking");
}
}
// Subclass (Child Class)
class Child extends Parent {
// Unique skill specific to the Child class
public void uniqueSkill() {
System.out.println("Child's unique skill: Painting");
}
}
public class Main {
public static void main(String[] args) {
// Creating an instance of the Child class
Child child = new Child();
// Calling the inherited method from the Parent class
child.sharedSkill();
// Calling the unique method of the Child class
child.uniqueSkill();
}
}
Output :
Parent's shared skill: Cooking
Child's unique skill: Painting
In the given example, we have a Parent class and a Child class. The Child class inherits from the Parent class using the extends
keyword.
This means that the Child class automatically gains access to the sharedSkill()
method defined in the Parent class. It is similar to how a child inherits certain skills and talents from their parent.
Additionally, the Child class has its own unique method called uniqueSkill()
, which represents a skill specific to the Child class only. This showcases how a child can have their individual qualities and abilities.
In the main
method, we create an instance of the Child class and demonstrate how it can access both the inherited sharedSkill()
method (representing the parent’s shared skill) and the uniqueSkill()
method (representing the child’s unique skill).
This example shows how inheritance allows child classes to inherit both their parent’s traits and add their own unique features, promoting code reuse and a well-organized class structure.
Types of inheritance in java
There are different types of inheritance which are supported by java are:
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritance(Supported by using interfaces, not by classes)
- Hybrid inheritance(combination of two or more types of inheritance)
Advantage of using inheritance in Java:
- Code Reusability: Inheritance helps us avoid repeating code. We can create a parent class with shared features, and other classes can inherit those features. This way, we don’t have to write the same code again and again, making our work faster and more organized.
- Organized Code: Inheritance helps us create a hierarchy of classes, making our code more organized and easier to understand.
- Easy Maintenance: Inheritance promotes a modular approach to coding. When updates or improvements are needed, changes can be made in the parent class, and those changes automatically reflect in all the child classes
- Reduced Code Duplication: With inheritance, common features are defined in a single place (the parent class), reducing code duplication across the application
- polymorphism: Using inheritance we can achieve runtime polymorphism (Method Overriding)
Key Points of inheritance:
- Private Members: Private members (fields or methods) are not inherited because the scope of a private member is only limited to the class level in which it is defined. Only the public and protected member are inherited.
- Constructors: Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass by using Super keyword
- Initialization Blocks: Initialization blocks (static and instance) are not inherited. Subclasses will not inherit the initialization blocks defined in the superclass.
- Static Members: static members are not inherited in Java to avoid ambiguity . To access static members from a superclass, subclasses can use the superclass’s name directly
- Final members: final members are not inherited in Java to protect encapsulation, prevent overriding, and maintain class integrity.