Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does creating an instance of a child class automatically create its super class object?

If I create an object of sub class then will the super class object also be created from which the sub class is inherited? If not then how by creating a sub class of Thread class (in multi-threading) calls the Thread class constructor and creates a Thread object?

like image 629
Mrutyunjaya Thakur Avatar asked May 11 '13 14:05

Mrutyunjaya Thakur


People also ask

Is parent object created when child object is created?

The constructor is called, therefore an object of the parent class is created. That object is used later to build an object of the child class. The reason for this is that an object of the child class is an object of the parent class with more things.

Is super class instantiated when subclass is instantiated?

When a subclass is instantiated, instantiate all the superclasses are instantiated at the same time. Here, the initialization of superclass attributes is ensured by the call of the superclass constructors, as described in Inheritance and Constructors.

What happens when you create a new instance of a class?

When you create an object, you are creating an instance of a class, therefore "instantiating" a class. The new operator requires a single, postfix argument: a call to a constructor. The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object.

What will happen when we create a object for child class?

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.


1 Answers

An instance of the subclass is an instance of the superclass. Only one object is created, but as part of that creation, constructor calls are chained together all the way up to java.lang.Object. So for example:

public class Superclass {

    // Note: I wouldn't normally use public variables.
    // It's just for the sake of the example.
    public int superclassField = 10;

    public Superclass() {
        System.out.println("Superclass constructor");
    }
}

public class Subclass extends Superclass {

    public int subclassField = 20;

    public Subclass() {
        super(); // Implicit if you leave it out. Chains to superclass constructor
        System.out.println("Subclass constructor");
    }
}

...

Subclass x = new Subclass();
System.out.println(x instanceof Subclass);
System.out.println(x instanceof Superclass);
System.out.println(x.superclassField);
System.out.println(x.subclassField);

The output of this is:

Superclass constructor
Subclass constructor
true
true
10
20

... because:

  • The first thing any constructor does is call either another constructor in the same class, or a superclass constructor. So we see "Superclass constructor" before "Subclass constructor" in the output.
  • The object we've created is (obviously) an instance of Subclass
  • The object we've created is also an instance of Superclass
  • The single object we've created has both fields (superclassField and subclassField). This would be true even if the fields were private (which they usually would be) - the code in Subclass wouldn't be able to access a private field declared in Superclass, but the field would still be there - and still accessible to the code within Superclass.

The fact that we've got a single object with all the state (both superclass and subclass) and all the behaviour (any methods declared within Superclass can still be used on an instance of Subclass, although some may be overridden with more specializd behaviour) is crucial to understanding Java's approach to polymorphism.

like image 154
Jon Skeet Avatar answered Oct 19 '22 06:10

Jon Skeet