DEV Community

VIDHYA VARSHINI
VIDHYA VARSHINI

Posted on

Inheritance in Java

What is inheritance: It is the mechanism by which object of one class act as object of another class. Here, one class can inherit the features (fields and methods) of another class. This is one of the fundamental concept in OOPs. A class that inherits from another class can reuse the methods and fields of that class.

To inherit from a class, we use the extend keyword.

Syntax:

class Parent {
    void display() {
        System.out.println(" parent class.");
    }
}

class Child extends Parent {
    void show() {
        System.out.println(" child class.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Ex: Here, there are two different classes : ClassRoom class and Student class.

public class ClassRoom{
public static void main (String[] args){
ClassRoom cr = new ClassRoom();
cr.learnJava();
}
public void learnJava(){
System.out.println("learning java");

}
}
Enter fullscreen mode Exit fullscreen mode
public class Student extends ClassRoom{
public static void main(String[] args){
//ClassRoom cr = new ClassRoom();
//cr.learnJava();
Student ss = new Student();
ss.play();
ss.learnJava();
}

public void play(){
System.out.println("playing");
}
}

Enter fullscreen mode Exit fullscreen mode

Here, Student is the child class or subclass and ClassRoom is the parent class or base class (super class).

Output:

playing
learning java
Enter fullscreen mode Exit fullscreen mode

In Inheritance, after the extend keyword, only one class name should be given.

Why more than one class name cannot be given after extend keyword

Java doesn't allow more than one class after extends because multiple parent classes can cause confusion when they have the same methods. Java avoids multiple inheritance to keep the language simple, clear, and easy to maintain.

Relation between constructor and inheritance:

  1. Constructors are not inherited - A child class doesn't get the parent's constructor, it only gets the parent's properties and methods.

  2. Parent constructor runs first - If we create an object for a child class, Java automatically calls the parent constructor before the child constructor because the parent’s part of the object must be created first.

  3. super() is used to call the parent constructor - If we don't write anything, Java automatically adds super() inside the child constructor.

  4. If parent has only parameterized constructor - Here, the child must call it using super().

Ex:

class Parent {
    Parent() {
        System.out.println("Parent constructor");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        Child c = new Child();
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:

Parent constructor 
Child constructor
Enter fullscreen mode Exit fullscreen mode

If I give three constructors and using one object, whether it can be called?

If the three constructors are given and one object is created, only one constructor can be called based on the arguments that matches the object creation. Here, Java picks the constructor based on the arguments which is passed.

Top comments (0)