Inheritance in Java Learn with Examples
Java is considered one of the finest programming languages worldwide for its simplicity, security and resilience. Among its several functions and mechanisms, Inheritance in Java is one of the most important concepts in the programming language.
Because of its compatibility and scalability, Java is used prominently around the world, with nearly 72% of people or organisations working with it in some measure, according to Statista.
In this blog, we will learn more about Inheritance in Java, its types with examples, and its applications in detail.
Table of Contents
1) The basics of Inheritance in Java
2) Key terms related to Java Inheritance
3) Types of Inheritance in Java
4) Applications of Java Inheritance
5) Disadvantages of Inheritance in Java
6) Conclusion
The basics of Inheritance in Java
Inheritance in Java is based on the concept of classes and objects. A class is a framework for creating objects, while an object is a unit that represents an abstract entity in a class. Parent class or superclass in Inheritance refers to a new class created based on an existing class. The new class is called the child class or subclass. The properties of the parent class are inherited by the child class, and the child class can also add new properties and behaviours of its own.
In Java, Inheritance is implemented using the "extends" keyword. To create a child class that inherits from a parent class, we simply use the "extends" keyword followed by the name of the parent class. Here is an example:
class Animal
{
public void eat()
{
System.out.println("The animal is eating.");
}
}
class Dog extends Animal
{
public void bark()
{
System.out.println("The dog is barking.");
}
}
public class Main
{
public static void main(String[] args)
{
Dog dog = new Dog();
dog.eat(); // The animal is eating.
dog.bark(); // The dog is barking.
}
}
In this example, the Dog class extends the Animal class, which means it inherits the eat() method from the Animal class. The Dog class also has its method, bark(). When we create a new instance of the Dog class and call its eat() and bark() methods, we see that the eat() method is inherited from the Animal class, and the bark() method is specific to the Dog class.
Key terms related to Java Inheritance
Here are some key terms you need to know before getting started with Java Inheritance:
1) Inheritance: Inheritance is a pillar of OOP (Object-Oriented Programming) that allows one class to inherit properties and behaviour from another class.
2) Superclass/Base class/Parent class: The class inherited from is known as the superclass, base class, or parent class. It is the class that provides the common properties and behaviours that are shared by its subclasses.
3) Subclass/Derived class/Child class: The class that inherits from a superclass is known as the subclass, derived class, or child class. The class adds new properties and behaviours to those already defined in the superclass.
4) extends keyword: The extends keyword is used to establish the inheritance relationship between a subclass and a superclass. It is placed after the subclass name, followed by the superclass's name.
5) super keyword: The super keyword is used to call the constructor or method of the superclass from within the subclass. It is typically used to access the superclass's properties or methods overridden by the subclass.
6) Access modifiers: Access modifiers are keywords that specify the level of access to class members (fields and methods) from other classes. Java has four access modifiers: public, private, protected, and default.
7) Final keyword: The final keyword prevents a class or method from being subclassed or overridden. A final class cannot be subclassed, and a final method cannot be overridden.
8) Abstract Class: A class that cannot be instantiated and is meant to be subclassed is called an Abstract Class. It may contain abstract methods meant to be implemented by the subclass.
9) Interface: A collection of abstract methods that define a contract for the classes that implement it is called an Interface. A class can implement multiple Interfaces.
10) Multiple Inheritance: Multiple inheritances is the ability to inherit from multiple classes. While Java supports multiple Inheritances of interfaces, it does not support multiple Inheritances of classes.
Types of Inheritance in Java
There are four main types of Java inheritance. Here are the types of inheritance in Java explained, along with examples:
Single Inheritance
Single Inheritance is the most straightforward type of Inheritance in Java, where a child class extends only one parent class.
For example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
public class Main
{
public static void main(String args[])
{
Dog d = new Dog();
d.bark();
d.eat();
}
}
Here, the Dog class inherits the eat() method from the Animal class.
Multi-level Inheritance
Multi-level inheritance is a type of inheritance where a child class extends another child class, which in turn extends a parent class.
Here is an example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class Pug extends Dog
{
void play()
{
System.out.println("playing");
}
}
public class Main
{
public static void main(String args[])
{
Pug p = new Pug();
p.play();
p.bark();
p.eat();
}
}
Here, the Pug class inherits the bark() method from the Dog class, which in turn inherits the eat() method from the Animal class.
Hierarchical Inheritance
In Hierarchical Inheritance multiple child classes extend the same parent class.
Here is an example:
class Animal
{
void eat()
{
System.out.println("eating");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing");
}
}
public class Main
{
public static void main(String args[])
{
Dog d = new Dog();
d.bark();
d.eat();
Cat c = new Cat();
c.meow();
c.eat();
}
}
Here, the Dog and Cat classes inherit the eat() method from the Animal class.
Applications of Java Inheritance
Inheritance is a pivotal part of Object-Oriented Programming (OOPs) that allows one class to inherit the properties and methods of another class. Java Inheritance enables developers to create new classes built upon existing classes, thereby reusing existing code and reducing redundancy.
Here are some of the main applications of Java Inheritance:
1) Code reusability: Inheritance allows developers to reuse code from existing classes by creating new classes that inherit the properties and methods of those classes. This reduces the amount of code that needs to be written and maintained.
2) Polymorphism: In Java, polymorphism is achieved through Inheritance. Polymorphism allows objects of different classes to be treated as objects of the same class. This is particularly useful when writing generic code that can work with different types of objects.
3) Method overriding: Java Inheritance allows developers to override methods from the parent class in the child class. This is useful when the child class needs to implement the same method as the parent class but with different behaviour.
4) Specialisation: Java Inheritance also allows developers to create specialised classes that inherit the properties and methods of a more general class. This can be useful when creating different types of objects that share their functionality.
5) Abstraction: Java Inheritance enables developers to create abstract classes that define a set of methods that must be implemented by any class that inherits from it. This enables developers to create a common interface for a group of related classes.
Disadvantages of Inheritance in Java
The primary disadvantages of Inheritance in Java are:
1) Tight Coupling: Inheritance creates a strong relationship between the base class and its derived classes. This can lead to tight coupling, meaning changes to the base class can significantly impact its derived classes. This makes it difficult to modify the base class without affecting the derived classes.
2) Inflexibility: Inheritance can lead to inflexibility in the code. Once a class is defined as a base class, its structure and functionality cannot be easily changed. This can be a problem when requirements change, or the base class needs to be modified to support new features.
3) Code Duplication: In some cases, Inheritance can lead to code duplication. If multiple classes inherit from the same base class, they may all have similar code, which can lead to redundancy and maintenance issues.
4) Fragility: Inheritance can make the code more fragile and prone to errors. If the base class is modified in a way that affects the derived classes, it can lead to bugs and unexpected behaviour.
5) Complexity: The code can be made complex and difficult to understand with Inheritance. It can be difficult to trace the flow of execution through multiple levels of Inheritance, especially if the base class is abstract and the derived classes have different implementations. This can make it harder to debug and maintain the code.
It is important to use Inheritance judiciously and consider alternative design patterns like composition to avoid these disadvantages.
Conclusion
Java is and will remain one of the most used programming languages worldwide because of its constant evolution, simplicity and resilience to change. Inheritance in Java is one of the most important pillars of OOPs, a key concept that aspirants are expected to learn, understand and master.
We hope this blog was satisfactory in comprehensively explaining the concept, applications and disadvantages and providing a guide to starting up basic Java Inheritance programming.
Learn and master the important concept of GUI programming with Java Swing Development Training Sign up soon!