Java Object Creation of Inherited Class Last Updated : 18 Apr, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In Java, being an object-oriented language, objects inside a class is created with help of constructors. When it comes down to inheritance in Java we are basically dealing with deriving a class from another class. Now let us understand inheritance a step deeper so when a particular class inherits a class we do have a keyword that we use in order to refer to parent constructor via super keyword. It is used as a suffix followed by the "." operator in constructor/s in a class. This property of parent class(super class) also gets initiated well before child class(sub class) inherits and uses them. Note: It is mandatory that when an object is created, the constructor is for sure called but it is not mandatory when a constructor is called object creation is mandatory. We already have seen above when we created an object of a subclass, the constructor of the same class is called, if not made the default constructor is called while we called superclass constructor without creating an object of class inside subclass via keyword. In inheritance, subclass acquires super class properties. An important point to note is, when a subclass object is created, a separate object of a superclass object will not be created. Only a subclass object is created that has superclass variables. This situation is different from a normal assumption that a constructor call means an object of the class is created, so we can't blindly say that whenever a class constructor is executed, the object of that class is created or not. Example: Java // Java program to demonstrate that Both Super Class // and Subclass Constructors Refer to Same Object // Importing required classes import java.util.*; // Class 1 // Super Class class Fruit { // Method inside super class public Fruit() { // Print statement System.out.println("Super class constructor"); // Displaying object hashcode of super class System.out.println("Super class object hashcode :" + this.hashCode()); System.out.println(this.getClass().getName()); } } // Class 2 // Sub class extending above super class class Apple extends Fruit { // Method inside sub class public Apple() { // Print statement System.out.println("Subclass constructor invoked"); // Displaying object hashcode of sub class System.out.println("Sub class object hashcode :" + this.hashCode()); System.out.println(this.hashCode() + " " + super.hashCode()); System.out.println(this.getClass().getName() + " " + super.getClass().getName()); } } // Class 3 // Main class public class GFG { // Main driver method public static void main(String[] args) { // Creating an instance of above class // inside main() method Apple myApple = new Apple(); } } OutputSuper class constructor Super class object hashcode :1149319664 Apple Subclass constructor invoked Sub class object hashcode :1149319664 1149319664 1149319664 Apple Apple Output Explanation: As we can see that both superclass(Fruit) object hashcode and subclass(Apple) object hashcode are same, so only one object is created. This object is of class Apple(subclass) as when we try to print the name of the class in which object is created, it is printing Apple which is a subclass. Comment More infoAdvertise with us Next Article Java Multiple Inheritance K kartik Improve Article Tags : Java java-inheritance Practice Tags : Java Similar Reads Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Object-Oriented Design (OOD) - System Design A crucial method for system design is object-oriented design (OOD), which places an intense focus on scalability, modularity, and reusability. OOD resembles real-world systems by encapsulating data and behavior into objects, which facilitates understanding programs and maintenance. By utilizing conc 12 min read Dynamic Method Dispatch or Runtime Polymorphism in Java Prerequisite: Overriding in java, Inheritance Method overriding is one of the ways in which Java supports Runtime Polymorphism. Dynamic method dispatch is the mechanism by which a call to an overridden method is resolved at run time, rather than compile time. When an overridden method is called thro 5 min read Method Overloading in Java In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference may include:The number of parametersThe types of parametersThe order of parametersMethod overloading in Java is also known as Compile-time Polymorphism, Static 10 min read Overriding in Java Overriding in Java occurs when a subclass or child class implements a method that is already defined in the superclass or base class. When a subclass provides its own version of a method that is already defined in its superclass, we call it method overriding. The subclass method must match the paren 15 min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Java Object Creation of Inherited Class In Java, being an object-oriented language, objects inside a class is created with help of constructors. When it comes down to inheritance in Java we are basically dealing with deriving a class from another class. Now let us understand inheritance a step deeper so when a particular class inherits a 3 min read Java Multiple Inheritance Multiple Inheritance is a feature of an object-oriented concept, where a class can inherit properties of more than one parent class. The problem occurs when there exist methods with the same signature in both the superclasses and subclass. On calling the method, the compiler cannot determine which c 4 min read Super Keyword in Java The super keyword in Java is a reference variable that is used to refer to the parent class when we are working with objects. You need to know the basics of Inheritance and Polymorphism to understand the Java super keyword. The Keyword "super" came into the picture with the concept of Inheritance. I 7 min read Using final with Inheritance in Java Prerequisite - Overriding in java, Inheritance final is a keyword in java used for restricting some functionalities. We can declare variables, methods, and classes with the final keyword. Using final with inheritance During inheritance, we must declare methods with the final keyword for which we are 4 min read Like