Why Is Constructor of Super Class Invoked When We Declare the Object of Sub Class? (Java)

Why is constructor of super class invoked when we declare the object of sub class?

Because it will ensure that when a constructor is invoked, it can rely on all the fields in its superclass being initialised.

see 3.4.4 in here

Does creating an instance of a subclass automatically run the superclass's constructor?

Obviously, the constructor for Animal is running each time I create a Cat, but why?

Because Cat is sub class of Animal class, When you create an instance of sub class, by default the default constructor of the super class is called.

Internally this is something like this:

public Cat() {
super(); // call to Animal class constructor.
counter1 += 1;
System.out.println("Cats counter is currently " + counter1);
}

The inheritance represents is-a relationship, Cat is an Animal.


Currently static int counter = 0; the counter variable is declared as static, which means that it will belong to the class not the instances of the class. So all the instance of the class will share the counter variable. That is why the counter is keeps on increasing as you create instance of sub class and super class.

When you make is non-static, it will be included into the state of the class as the instance member. Which mean, every time super class Animal constructor is called, its value is incremented to 1 (default is 0, you have also mentioned that), either directly or in-directly via sub-class constructor.

Why not constructor of super class invoked when we declare the object of sub class?

In python you should call the parent's initializer (that's how the __init__ method is actually called, - "constructor" is something else) explicitly.

You can do it like you did in the commented out line. Better yet, you should use the super function, that figures out, which parent to access for you. It only works with new-style classes though (basically, that means, that the root of your class hierarchy must inherit from object).

why cant subclass objects hold super class constructor

Nothing surprising here:

System.out.println(a1.getType1());

You are calling a method that is defined in the super class; and overridden in the a subclass. You create an instance of the subclass; and the method that gets executed ... is the overridden version.

The fact that Beta contains another method which is not at all used in your example anyway doesn't come into play here at all. And even if getType1() would be calling acc() - that would still work. That is the essence of of polymorphism!

And just to be precise: none of the methods you have in your classes is a constructor!

What is the purpose of calling the constructor of the object class in Java?

First of all, it is NOT necessary to write this:

 public class Gen extends Object {

If a class doesn't explicitly have some other class as its direct superclass, then it implicitly extends Object. There is not need to tell the compiler that.


Yes ... all classes (apart from Object) constructors will call a constructor of their superclass. Even if the superclass is Object.

But it doesn't matter to the programmer. As the spec says, if the constructor to be called is a no-args constructor, then you don't need an explicit super call. The compiler injects a missing super() call for you if you don't include one.

Yes ... the Object constructor has an empty body in all Java implementations I have come across.

But I don't think that the JLS mandates that.

The JLS section on how objects are created states that the constructor will be called.

But it doesn't say that the compilers can't optimize away the call to the Object() constructor. And that is what they do. (The bytecode compiler is required to emit instructions for the call by JVM spec, but the JIT compiler can and will optimize it away.)

Why do they specify it in these terms?

  • Primarily because the spec is easier to understand and the language is easier to use if there are fewer special cases in the Java syntax and semantics.

    "You can't have a super() class if the superclass is Object" would be a special case. But this complexity is not needed to make the language work, and it certainly doesn't help programmers if you force them to leave out a super() call in this context.

  • In addition, the current way permits a Java implementation to have a non-empty Object() constructor, if there was a good reason for doing that. (But I doubt that that was serious consideration when they designed Java.)


Either way, this is the way that Java has been since before Java 1.0, and they won't change it now. The current way doesn't actually cause any problems, or add any appreciable overheads.

Why superclass constructor must be called with super() and not with its class name?

A good programming language is one that lets the author be expressive about their intent with as few syntactical variations as possible in which to achieve that. The more variations there are, the more rules a programmer has to carry around in their head, and the more complex a compiler has to be to support all those variations.

Suppose I eschew Java naming conventions and use a capital letter in the name of a static method:

class Duck extends Animal {
int size;

public Duck(int size) {
Animal(); //super or static method?
this.size = size;
}

private static void Animal() {
System.out.println("I'm a static method");
}
}

Is the Duck constructor calling the Animal constructor or the static method? We could introduce a new rule which covers this case but it would be introducing more complexity.

What you are suggesting is two ways to achieve exactly the same thing. There is no reason to support that when super fulfills that purpose perfectly well on its own.

Why don't they also allow us to use parent() or superconstructor() or superclass()? The onus is on you to provide a compelling reason why it should be supported.



Related Topics



Leave a reply



Submit