0

I have created an upcasting demo and i don't understand how this code is working or i can more specifically say why is constructor of base class also called while the dispatching is done with the Derived class.And there is even not a call to the constructor of the base class.I haven't even used super keyword anywhere than how is the constructor of the base class also called.

class Base{
        int value =0;
        Base(){

            addValue();
        }
        void addValue(){

            value+=10;
        }
        int getValue(){
            return value;
        }

}
class Derived extends Base{

    Derived()
    {
        addValue();
    }
    void addValue(){
        value+=20;
    }
}
class Test{
    public static void main(String args[]){
        Base b=new Derived();
        System.out.println(b.getValue());
    }
}
Nnnnn
  • 133
  • 3
  • 15
  • 2
    The super constructor is _always_ called even if you don't call `super()` directly. That's per the specification and has been that way since Java 1.0. However, since your super constructor is calling an overridden method you might run into ordering problems, especially if it references instance members that might not yet have been initialized. – Thomas Aug 01 '18 at 14:52
  • What do you see; what do you expect to see? –  Aug 01 '18 at 14:52
  • "And there is even not a call to the constructor of the base class.I haven't even used super keyword anywhere" superclass constructor is always called. If you will place `super` keyword in your code to decide which constructor you want, compiler will try to place `super()` for you automatically to call non-argument constructor. You will get compilation error if superclass will not have such constructor, or if it will not be accessible. – Pshemo Aug 01 '18 at 14:54
  • Possible duplicate of [Possible to avoid default call to super() in Java?](https://stackoverflow.com/questions/41566202/possible-to-avoid-default-call-to-super-in-java) – sn42 Aug 01 '18 at 14:54
  • Possibly related: [Is it unnecessary to put super() in constructor?](https://stackoverflow.com/q/2054022) – Pshemo Aug 01 '18 at 15:00
  • 1
    BTW because of polymorphism inside superclass constructor you are calling `addValue();` which will *execute* code from `Derived` class. So since you are also calling that method in Derived class constructor it will be executed twice. To avoid such problems it is better to use in constructor only non-polymorphic methods, so either `private` `final` or `static` ones. – Pshemo Aug 01 '18 at 15:03

2 Answers2

1

When you create a new Derived object, its constructor will be called. Since Derived has a superclass Base, its construtor will be called first. In the constructor, the overridden method addValue will be called, which leads to the temporary value of 20. After that, addValue of Derived is called and adds another 20 to value. The final result is 40.

Since, you did not call the constructor of the superclass using super yourself, Java will do that automatically:

Derived() {
    super();
    addValue();
}
Glains
  • 2,773
  • 3
  • 16
  • 30
  • "there is a serious problem with your code, since you must always call the constructor of the superclass using super first:" - note that Java will _always_ implicitly call `super()` at the beginning of a constructor unless there's already a call to `super(...)` or `this(...)` (with or without parameters). Thus code not calling `super() ` explicitly is quite common and not a problem at all - you need to know that this is happening though. – Thomas Aug 01 '18 at 15:12
  • @Thomas Absolutely right, edited my answer. Thanks a lot! – Glains Aug 01 '18 at 16:13
0

A Derived object is-a Base object, plus more. Java, like most languages, needs to construct the whole object, including the parts initialized by the Base constructor.

Look at is this way: What's doing the initial setting of value to 0? Isn't that part of the code for Base too?

Java lets you specify which base-class constructor to use with super if you want to, but if you don't specify one, it'll pick one. Those rules can be complicated, but in this case they're simple: The no-arguments Base constructor is called from the no-arguments Derived constructor.

As a test, set the Base() constructor private and see what the compiler tells you.

Bob Jacobsen
  • 1,150
  • 6
  • 9