What is Inheritance in Java with Example

What is Inheritance in Java with Example

Inheritance in Java is a fundamental concept in OOPs (Object-oriented programming system) that allows one class to inherit properties and behaviors from another class.

Why use Inheritance in Java?

  • Inheritance facilitates Code Reusability.
  • Inheritance helps you hierarchically organize your classes.
  • Method Overriding which helps you to achieve runtime polymorphism.

Types of Inheritance in Java

  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java

Single Inheritance in Java

In single inheritance, a class inherits properties and behaviors from a single-parent class. It forms a linear relationship, where one class is derived from another class.
1class Parent {
2    // Parent class members
3}
4
5class Child extends Parent {
6    // Child class members
7}

Extends keyword in Java

Extends keyword is used to create a special relationship between classes. It's like saying one class is a more specialized version of another.
Imagine you have a class called "Animal," and you want to create a more specific class called "Dog." You can use the "extends" keyword like this:
1class Dog extends Animal {
2    // Dog-specific code goes here
3}
In this example, you're telling Java that the Dog class extends the Animal class. This means that "Dog" inherits all the properties methods and variables of an Animal class.

Multiple Inheritance in Java

  • Multiple inheritance is a feature where a class can inherit from multiple parent classes.
  • While some programming languages support multiple inheritance.
  • Java does not directly allow it to avoid ambiguity in case of method or attribute conflicts.

Multilevel Inheritance in Java

In multilevel inheritance, a class inherits from another class, which, in turn, is derived from another class. This creates a chain of inheritance.
1class Grandparent {
2    // Grandparent class members
3}
4
5class Parent extends Grandparent {
6    // Parent class members
7}
8
9class Child extends Parent {
10    // Child class members
11}
12

Hierarchical Inheritance in Java

Hierarchical inheritance involves one class being inherited by multiple subclasses. Each subclass shares common features with the parent class. for instance:
1class Animal {
2    // Animal class members
3}
4
5class Dog extends Animal {
6    // Dog class members
7}
8
9class Cat extends Animal {
10    // Cat class members
11}
12

Super Keyword in Java

  • The super keyword is used to refer to the superclass or parent class in Java.
  • It is primarily used to call the constructor of the parent class.
  • By using the super keyword, we can override the parent class's methods while still utilizing their behavior.
1class Parent {
2    void display() {
3        System.out.println("Hello, I am the parent class!");
4    }
5}
6
7class Child extends Parent {
8    void display() {
9        super.display(); // Calling parent class method
10        System.out.println("And I am the child class!");
11    }
12}
13
the output will be :
1Hello, I am the parent class!
2And I am the child class!

Method Overriding in Java

  • Method overriding in Java allows a subclass to provide a specific implementation for a method that is already defined in its superclass.
  • When a method in a subclass has the same name, return type, and parameters as a method in its superclass, it's said to override the superclass method.
  • It supports polymorphism.
1class Animal {
2    void sound() {
3        System.out.println("Animal makes a sound");
4    }
5}
6
7class Dog extends Animal {
8    void sound() {
9        System.out.println("Dog barks");
10    }
11}
12
13class Cat extends Animal {
14    void sound() {
15        System.out.println("Cat meows");
16    }
17}
18

Covariant Return Type

In Java 5 and later versions, covariant return types were introduced, allowing the overriding method to have a different return type from the overridden method, as long as it is a subtype of the original return type. This feature enhances the flexibility of method overriding.
1class Fruit {
2    Fruit getFruit() {
3        return new Fruit();
4    }
5}
6
7class Apple extends Fruit {
8    Apple getFruit() {
9        return new Apple();
10    }
11}
12

Abstract Class in Java

  • An abstract class is a class that cannot be presented on its own and may contain abstract methods, which are declared but not implemented in the abstract class.
  • It serves as a blueprint for its subclasses, which must implement abstract methods.
1abstract class Shape {
2    abstract void draw();
3}
4
5class Circle extends Shape {
6    void draw() {
7        System.out.println("Drawing a circle");
8    }
9}
10
11class Square extends Shape {
12    void draw() {
13        System.out.println("Drawing a square");
14    }
15}

Differences Between Abstract Classes and Interfaces in Java


Although both abstract classes and interfaces support abstraction and declaration without implementation, they have some important differences.

1. Multiple inheritance: An abstract class can only have one direct superclass, while an interface can extend more than one interface.

2. Constructors: Abstract classes have constructors, but interfaces do not.

3. Access modifiers: Abstract classes can have access modifiers for methods, but in interfaces, all methods are implicitly public.
4..Variables: Abstract classes can have mutable instances, but not interfaces.

5. Method body: Abstract classes can have abstract methods and concrete methods, while interfaces can only have abstract methods (before Java 8).

Conclusion

In conclusion, this comprehensive guide covered various essential topics related to Java programming, such as inheritance, abstract classes, and much more. Understanding these concepts is crucial for any Java developer to create efficient, robust, and well-structured applications.