0

When a class extends another class, the subclass constructor calls the super class constructor.

In this process there are 2 objects created, sub class and super class. My Question is what happens to this super class object through out the life cycle of subclass object?

Will it be garbage collected as the reference is of sub class?

class A
{
    public A()
    {
        System.out.println("super class constructor");
    }
}

class B extends A
{
    public B()
    {
        //  super();  default added 
        System.out.println("sub class constructor");
    }
}

class Test
{
    public static void main(String[] args)
    {
        B ob=new B();
    }
}
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • It's just one object. This one object is an instance of both `A` and `B`, so it needs to run `A`'s initialisation and `B`'s initialisation. – khelwood Jun 27 '17 at 11:11
  • *In this process there are 2 objects created, sub class and super class*: no, not at all. A single object is being created. – JB Nizet Jun 27 '17 at 11:11
  • 1
    Don't forget that `A` also has a super-class, `java.lang.Object` (in fact `Object` is a super-class to all reference types). – Elliott Frisch Jun 27 '17 at 11:13
  • And hint: your tagging is bogus. You have a simple Java question; there is absolutely **no** need to tag this for java8 or java6. – GhostCat Jun 27 '17 at 11:15
  • @erwin No, not in Java. – JB Nizet Jun 27 '17 at 11:41
  • @MadhuKumarTallapalli If an user answered your question please also **accept** his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Jul 27 '17 at 12:01

6 Answers6

3

In this process there are 2 objects created, sub class and super class.

No, a single object is created : the instance of the subclass which the structure relies also on the super class.

Invoking the constructor of the super class is a requirement of the specification of the Java language. It allows to keep a consistence as subclasses should rely on the construction rules of their parent.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
2

Wrong assumption.

Exactly one object is created. Yes, multiple constructor calls are going to happen (see here for details on what exactly happens).

But right from the beginning; until the very end and new returning that freshly created object: only a single object comes into existence!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

Only one object will be created. Therefore the code in both constructors will be executed. A sub-class has the chance to override defined behavior of its parent class.

An example will help. Imagine a class Human and a sub-class Bob:

class Human
class Bob extends Human

When creating Bob with Bob bob = new Bob() you will only create a Bob instance that is also a human. So bob instanceOf Human will be true. As Bob is a human it makes sense that code from his and from Humans constructor is both executed and he also gets all methods from Human.

By using super you do not reference to a parent instance but rather to the code declared there. Imagine Human having a method isAlive and Bob overriding this method (he wants to be alive always). However you want to be able to also explicitly call the isAlive method declared in the Human class though Bob overrode it. You do so by super.isAlive(). By that Java knows ah, he wants the method from Human and not from Bob.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

In this process there are 2 objects created, sub class and super class.

No, only one object is created - that of the subclass. Since subclass-superclass relationship is called "is-a", subclass is also a superclass.

That is why superclass methods can be called on the subclass, as long as the methods are visible. That is also why a constructor of the superclass works with the object of a subclass.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

I think there is only one object, constructed with its constructor and the super-constructor. The object 'ob' will inherit all properties of class B and the super-class A. If the constructor of A initialize variables for example, they will be initialized for the object 'ob' too.

0

There will be only one object. super() is called to inherit the behaviour of the parent. That is why we can get the properties of parent in subclass.