- Collection Framework in JavaCollections Class in JavaList Interface in Java with ExamplesArrayList in JavaVector Class in JavaLinkedList in JavaQueue Interface In JavaPriorityQueue in JavaSet in JavaHashSet in JavaLinkedHashSet in JavaMap in the JavaHashMap in JavaHashtable in JavaLinkedHashMap in Java
Inheritance in Java
Inheritance is one of the fundamental concepts of object-oriented programming (OOP) in Java. It allows a class to inherit properties and behaviour's from another class, known as its superclass or parent class. In this blog post, we will discuss the concept of inheritance in Java and provide an example to demonstrate its usage.
Example of Inheritance in Java
Let's consider a simple example of two classes - Animal
and Dog
- to demonstrate inheritance. The Animal
class is the superclass and the Dog
class is the subclass. The Dog
class inherits the properties and behaviors of the Animal
class.
public class Animal {
private String name;
private int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("The animal is eating.");
}
public void sleep() {
System.out.println("The animal is sleeping.");
}
}
public class Dog extends Animal {
private String breed;
public Dog(String name, int age, String breed) {
super(name, age);
this.breed = breed;
}
public void bark() {
System.out.println("The dog is barking.");
}
}
In this example, the Animal
class has two private fields - name
and age
- and two public methods - eat()
and sleep()
. The Dog
class is a subclass of the Animal
class and inherits its properties and behaviors. The Dog
class has an additional private field - breed
- and a public method - bark()
.
We have used the super
keyword in the constructor of the Dog
class to call the constructor of the superclass and initialize the name
and age
fields. This ensures that the Dog
class has access to the properties of the Animal
class. We have also added an additional behavior to the Dog
class by defining the bark()
method.
Now, let's consider a scenario where we create an object of the Dog
class and call its methods:
Dog dog = new Dog("Rufus", 3, "Labrador");
dog.eat(); // output: The animal is eating.
dog.sleep(); // output: The animal is sleeping.
dog.bark(); // output: The dog is barking.
In this way, the Dog
class has inherited the behaviors of the Animal
class and added its own unique behavior.
Benefits of Inheritance
Inheritance provides several benefits, including:
Code reuse: Inheritance allows us to reuse code from existing classes, reducing the amount of code we need to write and improving code efficiency.
Polymorphism: Inheritance allows us to create objects that are instances of a subclass but can be treated as instances of the superclass. This allows us to write more generic and flexible code.
Modularity: Inheritance allows us to create a hierarchy of classes, making the code more modular and easier to maintain.
Types of Inheritance in Java
Single Inheritance
Single inheritance is the most common type of inheritance in Java. In this type of inheritance, a child class extends a single parent class. The child class inherits all the properties and methods of the parent class and can also define its own properties and methods. For example:
class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
public class TestDog {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.bark();
}
}
Multilevel Inheritance
Multilevel inheritance involves a chain of inheritance in which a derived class becomes the base class for another derived class. In this type of inheritance, each class inherits the properties and methods of its parent class. For example:
class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
class Puppy extends Dog {
public void play() {
System.out.println("Puppy is playing.");
}
}
public class TestPuppy {
public static void main(String args[]) {
Puppy p = new Puppy();
p.eat();
p.bark();
p.play();
}
}
In the above example, the Puppy
class extends the Dog
class, which in turn extends the Animal
class. This means that Puppy
inherits the properties and methods of both Dog
and Animal
.
Hierarchical Inheritance
Hierarchical inheritance involves a single base class and multiple derived classes. In this type of inheritance, each derived class inherits the properties and methods of the base class. For example:
class Animal {
public void eat() {
System.out.println("Animal is eating.");
}
}
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking.");
}
}
class Cat extends Animal {
public void meow() {
System.out.println("Cat is meowing.");
}
}
public class TestAnimals {
public static void main(String args[]) {
Dog d = new Dog();
Cat c = new Cat();
d.eat();
d.bark();
c.eat();
c.meow();
}
}
In the above example, both Dog
and Cat
classes inherit the eat()
method from the Animal
class.
Multiple Inheritance
Multiple inheritance is a type of inheritance where a class can inherit properties and behaviour's from multiple parent classes. Java does not support multiple inheritance directly, but it can be achieved through interfaces. An interface is a collection of abstract methods that can be implemented by a class.
Let's see an example of multiple inheritance in Java using interfaces.
interface Animal {
public void eat();
}
interface Plant {
public void photosynthesize();
}
class Dog implements Animal, Plant {
public void eat() {
System.out.println("Dog is eating.");
}
public void photosynthesize() {
System.out.println("Dog is not a plant, so it cannot photosynthesize.");
}
public void bark() {
System.out.println("Dog is barking.");
}
}
public class TestDog {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.photosynthesize();
d.bark();
}
}
In the above example, we have defined two interfaces Animal
and Plant
. The Animal
interface has one abstract method eat()
, and the Plant
interface has one abstract method photosynthesize()
.
The Dog
class implements both the Animal
and Plant
interfaces and provides implementations for the abstract methods. Additionally, it defines its own method bark()
.
In the main()
method, we create an object of the Dog
class and call its eat()
, photosynthesize()
, and bark()
methods. The output of the program will be:
Dog is eating.
Dog is not a plant, so it cannot photosynthesize.
Dog is barking.
Hybrid inheritance
Hybrid inheritance is a combination of two or more types of inheritance. It is achieved by combining multiple inheritance and hierarchical inheritance. In Java, hybrid inheritance can be achieved using a combination of inheritance and interfaces.
Let's see an example of hybrid inheritance in Java.
interface Animal {
public void eat();
}
interface Plant {
public void photosynthesize();
}
class LivingThing {
public void breathe() {
System.out.println("All living things breathe.");
}
}
class Dog extends LivingThing implements Animal, Plant {
public void eat() {
System.out.println("Dog is eating.");
}
public void photosynthesize() {
System.out.println("Dog is not a plant, so it cannot photosynthesize.");
}
public void bark() {
System.out.println("Dog is barking.");
}
}
public class TestDog {
public static void main(String args[]) {
Dog d = new Dog();
d.eat();
d.photosynthesize();
d.bark();
d.breathe();
}
}
In the above example, we have defined three classes Animal, Plant, and LivingThing. The Animal and Plant interfaces have one abstract method each, and the LivingThing class has one method breathe().
The Dog class extends the LivingThing class and implements both the Animal and Plant interfaces. It provides implementations for the abstract methods and defines its own method bark().
In the main() method, we create an object of the Dog class and call its eat(), photosynthesize(), bark(), and breathe() methods. The output of the program will be:
Dog is eating.
Dog is not a plant, so it cannot photosynthesize.
Dog is barking.
All living things breathe.
Thus, we have achieved hybrid inheritance in Java using a combination of inheritance and interfaces.
Conclusion
Inheritance is an important concept in Java programming that allows us to create more efficient and flexible code. By allowing a subclass to inherit properties and behaviour's from its superclass, we can reuse existing code and create more modular and maintainable programs.