Java:Method called from subclass in superclass constructor

Paul
class AA{
    int x;
    protected AA(){init (1008);}
    protected void init(int x)
    {
        this.x = x;
    }
}

class BB extends  AA{
    public BB() {
        init(super.x * 2);
    }
    public void init(int x)
    {
        super.x = x+1;
    }
}
public class Main  {

    public static void main(String[] args) {
        BB tst = new BB();
        System.out.println(tst.x);
    }
}

I know that this code will print 2019. Yet I do not understand why the superclass constructor,when called, will use the init method from de subclass instead the one from the superclass.

T.J. Crowder

Yet I do not understand why the superclass constructor,when called, will use the init method from de subclass instead the one from the superclass.

Because that's the one associated with the object being constructed. this within the superclass constructor is a reference to the subclass object being constructed, so just like any other call to init using that reference, it uses the subclass's init.

This may help, note the lines with comments on the end — the comments say what those lines output:

class AA{
    int x;
    protected AA() {
        System.out.println(this.getClass().getName()); // "BB"
        System.out.println(this instanceof BB);        // true
        init(1008);
    }
    protected void init(int x)
    {
        this.x = x;
    }
}
class BB extends  AA{
    public BB() {
        init(super.x * 2);
    }
    public void init(int x)
    {
        super.x = x+1;
    }
}
public class Main  {

    public static void main(String[] args) {
        BB tst = new BB();
        System.out.println(tst.x);
    }
}

It's because subclasses can override methods that calling non-final, non-private methods from a constructor is usually best avoided.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling superclass from a subclass constructor in Java

From Dev

In Java, what does 'this' indicate in a superclass method called on a subclass that inherits that method?

From Dev

Java polymorphism - How to determine whether superclass vs subclass method will be called and superclass variable vs subclass variable?

From Dev

Superclass overriding Subclass with default values from constructor in Java

From Java

A superclass method is called instead of the subclass method

From Dev

Calling constructor of subclass from constructor of superclass

From Java

Calling a subclass method from superclass

From Dev

Constructor method of subclass for a superclass type object?

From Dev

How do you call a subclass method from a superclass in Java?

From Java

java - Calling a subclass method from dynamically casted superclass

From Dev

Calling a subclass method from an ArrayList of type superclass in Java

From Java

Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

From Java

Passing superclass object as parameter to subclass constructor (java)

From Dev

Java superclass variable and subclass copy constructor issue

From Java

java, initialize SubClass from SuperClass

From Java

Why is "this" from superclass calling method from subclass?

From Dev

Access Django SubClass Method from SuperClass

From Dev

Calling a Subclass method from an array of Superclass Objects

From Dev

c++ calling method from subclass in superclass

From Dev

Accessing subclass method from arraylist of superclass

From Dev

Accessing subclass method from superclass C#

From Java

Java: returning subclass in superclass method signature

From Dev

Superclass reference not able to call subclass method in Java

From Dev

Java Inheritance: Calling a subclass method in a superclass

From Java

Superclass method being called even though object is of subclass

From Dev

Getting an instance of the subclass extending a superclass when a method is called

From Dev

Partial mocking a method of a subclass makes it bypass the superclass constructor

From Dev

Why is superclass method calling overrided subclass method when the superclass method is the one specified to be called?

From Dev

Superclass method with subclass data

Related Related

  1. 1

    Calling superclass from a subclass constructor in Java

  2. 2

    In Java, what does 'this' indicate in a superclass method called on a subclass that inherits that method?

  3. 3

    Java polymorphism - How to determine whether superclass vs subclass method will be called and superclass variable vs subclass variable?

  4. 4

    Superclass overriding Subclass with default values from constructor in Java

  5. 5

    A superclass method is called instead of the subclass method

  6. 6

    Calling constructor of subclass from constructor of superclass

  7. 7

    Calling a subclass method from superclass

  8. 8

    Constructor method of subclass for a superclass type object?

  9. 9

    How do you call a subclass method from a superclass in Java?

  10. 10

    java - Calling a subclass method from dynamically casted superclass

  11. 11

    Calling a subclass method from an ArrayList of type superclass in Java

  12. 12

    Is there any way to initialize member variables of a subclass in Java before the superclass' constructor is called?

  13. 13

    Passing superclass object as parameter to subclass constructor (java)

  14. 14

    Java superclass variable and subclass copy constructor issue

  15. 15

    java, initialize SubClass from SuperClass

  16. 16

    Why is "this" from superclass calling method from subclass?

  17. 17

    Access Django SubClass Method from SuperClass

  18. 18

    Calling a Subclass method from an array of Superclass Objects

  19. 19

    c++ calling method from subclass in superclass

  20. 20

    Accessing subclass method from arraylist of superclass

  21. 21

    Accessing subclass method from superclass C#

  22. 22

    Java: returning subclass in superclass method signature

  23. 23

    Superclass reference not able to call subclass method in Java

  24. 24

    Java Inheritance: Calling a subclass method in a superclass

  25. 25

    Superclass method being called even though object is of subclass

  26. 26

    Getting an instance of the subclass extending a superclass when a method is called

  27. 27

    Partial mocking a method of a subclass makes it bypass the superclass constructor

  28. 28

    Why is superclass method calling overrided subclass method when the superclass method is the one specified to be called?

  29. 29

    Superclass method with subclass data

HotTag

Archive