DEV Community

Cover image for Behind the Scenes of Java OOP: Constructors to Run Time Polymorphism
Hariharan S J
Hariharan S J

Posted on

Behind the Scenes of Java OOP: Constructors to Run Time Polymorphism

1.Introduction

Have you ever wondered what actually happens when you create an object in Java?
Why does the parent constructor execute before the child constructor?
Why can a child class access parent methods, but a parent class cannot directly call a child constructor?
And why is method overriding called Run Time Polymorphism?

Most beginners learn Java syntax, but very few truly understand what happens behind the scenes when inheritance, constructors, super() keyword, and method overriding work together.

In this blog, we are going to break down these core OOP concepts in the simplest possible way using real Java examples based on Hotel.java and Vadakkambatti.java.

By the end of this blog, you will clearly understand:

  • How constructors work internally

  • How inheritance actually behaves

  • Why super() is important

  • How method overriding works

  • Why overriding is called Run Time Polymorphism

  • Whether return types can be changed during method overriding

If you are learning Java or preparing for interviews, these concepts are extremely important because they are frequently asked in technical discussions and coding interviews.

Let’s dive into the world of Java OOP concepts step by step

2.What is a Constructor in Java?

A constructor is a special block in Java that is automatically called when an object is created.

Important Points About Constructors

  • Constructor name should be the same as the class name

  • Constructors do not have a return type

  • Constructors are automatically executed during object creation

  • Constructors are mainly used to initialize object values

Example

public class Hotel {

    String foodName;
    int price;

    public Hotel(String foodName, int price){

        this.foodName = foodName;
        this.price = price;
    }

    void display(){

        System.out.println(foodName);
        System.out.println(price);
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Vadakkambatti {

    public static void main(String[] args){

        Hotel item1 = new Hotel("Chicken Biriyani", 250);

        item1.display();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Chicken Biriyani
250
Enter fullscreen mode Exit fullscreen mode

Here, the constructor initializes the values using this.

3.Understanding Inheritance in Java

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).

Excepting Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass of Object.

Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class, Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back to Object.

The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Syntax

class Child extends Parent
Enter fullscreen mode Exit fullscreen mode

Here:

  • Parent class β†’ Super class

  • Child class β†’ Sub class

4.Understanding super Keyword in Java

The super keyword in Java is used to refer to the immediate parent class object in an inheritance hierarchy. It allows a subclass to explicitly access parent class members when they are hidden or overridden. This keyword helps maintain clarity and control while working with inheritance.

  • Used to call parent class constructors using super().

  • Helps access parent class methods and variables when overridden or hidden.

  • Ensures proper inheritance behavior and code reusability.

Example

Hotel.java

public class Hotel {

    Hotel(){

        System.out.println("Parent Constructor");
    }
}
Enter fullscreen mode Exit fullscreen mode

Vadakkambatti.java

public class Vadakkambatti extends Hotel {

    Vadakkambatti(){

        super();

        System.out.println("Child Constructor");
    }

    public static void main(String[] args){

        Vadakkambatti obj = new Vadakkambatti();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Parent Constructor
Child Constructor
Enter fullscreen mode Exit fullscreen mode

What Happens Here?

When the child object is created:

new Vadakkambatti();
Enter fullscreen mode Exit fullscreen mode

Java automatically calls:

super();
Enter fullscreen mode Exit fullscreen mode

This super() calls the parent class constructor first.

So the execution order becomes:

  • Parent constructor

  • Child constructor

Important Rules About super Keyword

  • super() must be the first statement inside the child constructor

  • It is used to call the parent constructor

  • If super() is not written, Java automatically inserts it

5.Can Parent Class Call Child Constructor?

This is one of the most confusing questions for beginners.

Answer: No

A parent class cannot directly call a child class constructor.

Wrong Example

public class Hotel {

    Hotel(){

        Vadakkambatti(); // Error
    }
}
Enter fullscreen mode Exit fullscreen mode

This produces a compile-time error like:

cannot find symbol
Enter fullscreen mode Exit fullscreen mode

Reason

Constructors cannot be called like normal methods.

Correct way:

new Vadakkambatti();
Enter fullscreen mode Exit fullscreen mode

Constructors only execute during object creation.

6.What is Method Overriding?

Method overriding means redefining a parent class method inside the child class with the same method name and same parameters.

  • Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.

  • When a method in a subclass has the same name, same parameters or signature, and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class.

  • Method overriding is one of the way by which java achieve Run Time Polymorphism.

Usage of Java Method Overriding

  • Method overriding is used to provide the specific implementation of a method which is already provided by its superclass.

  • Method overriding is used for runtime polymorphism.

Example

Hotel.java

public class Hotel {

    void SpecialFood(){

        System.out.println("Chicken Biriyani");
    }
}
Enter fullscreen mode Exit fullscreen mode

Vadakkambatti.java

public class Vadakkambatti extends Hotel {

    void SpecialFood(){

        System.out.println("Grill Chicken");
    }

    public static void main(String[] args){

        Vadakkambatti obj = new Vadakkambatti();

        obj.SpecialFood();
    }
}
Enter fullscreen mode Exit fullscreen mode

Output

Grill Chicken
Enter fullscreen mode Exit fullscreen mode

Here, the child class overrides the parent method and provides its own implementation.

7.Why Method Overriding is Called Run Time Polymorphism?

In method overriding, Java decides which method to execute during run time.

Example

Hotel obj = new Vadakkambatti();

obj.SpecialFood();
Enter fullscreen mode Exit fullscreen mode

Even though the reference type is Hotel, the object type is Vadakkambatti.

So Java checks the actual object during execution and calls the child method.

This decision happens at run time, so method overriding is called:

Run Time Polymorphism
Enter fullscreen mode Exit fullscreen mode

Can We Change Return Type in Method Overriding?

Answer: Yes and No

Return type can be changed only in certain situations.

Same Return Type

class Hotel {

    int price(){

        return 100;
    }
}

class Vadakkambatti extends Hotel {

    int price(){

        return 200;
    }
}
Enter fullscreen mode Exit fullscreen mode

Here both methods return int, so overriding is valid.

Covariant Return Type

A child class return type can replace the parent class return type.

Example

class Food {

}

class Biriyani extends Food {

}

class Hotel {

    Food SpecialFood(){

        return new Food();
    }
}

class Vadakkambatti extends Hotel {

    Biriyani SpecialFood(){

        return new Biriyani();
    }
}
Enter fullscreen mode Exit fullscreen mode

This is called:

Covariant Return Type
Enter fullscreen mode Exit fullscreen mode

Completely Different Return Type

class Hotel {

    int price(){

        return 100;
    }
}

class Vadakkambatti extends Hotel {

    String price(){ // Error

        return "Two Hundred";
    }
}
Enter fullscreen mode Exit fullscreen mode

This produces a compile-time error because int and String are unrelated return types.

Important Rule for Method Overriding

Method overriding requires:

  • The method must have the same name as in the parent class.

  • The method must have the same parameter as in the parent class.

  • Compatible return type

  • Inheritance relationship

8.Final Thoughts

Understanding constructors, inheritance, super keyword, method overriding, and return types is essential for mastering Java OOP concepts.

Key Takeaways

  • Constructors initialize objects

  • Inheritance helps reuse code

  • super() calls parent constructors

  • Parent constructors can be called by child classes

  • Child constructors cannot be directly called by parent classes

  • Method overriding enables run time polymorphism

  • Return type can be changed only if it is compatible

Once these basics become clear, advanced Java concepts become much easier to understand.

Happy Coding.

Reference

(https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html)

(https://www.geeksforgeeks.org/java/super-keyword/)

(https://java-iitd.vlabs.ac.in/exp/method-overriding/theory.html)

Top comments (0)