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

abc bca

Example code:

public class A {

    public int number;

    public A(int number) {
        this.number = number;
    }

    public int getNumber() {
        return number;
    }

}



public class B extends A{

    public int number;

    public B(int number) {
        super(number);
    }


    public int getNumber() {
        return number;
    }

}


public class C {

    public static void main(String args[]) {

        A test1 = new B(2);
        B test2 = new B(2);

        System.out.println(test1.number) // prints 2
        System.out.println(test2.number) // prints 0
        System.out.println(test1.getNumber()) //prints 0
        System.out.println(test2.getNumber()) // prints 0
    }

}

As shown above test1.number is not equal to test1.getNumber().

So when I make test1 an object of type A test1.number is referring to the int number in class A.

But when I call test1.getNumber() it's calling getNumber() in class B?

Why does that happen?

Powerlord

All methods in Java are virtual. I'll get to what that means in a second.

So, on this line:

A test1 = new B(2);

You've assigned a new instance of class B to a variable declared as holding an A.

When you use test1.number, you're using the number variable declared in class A, which given that the constructor for B calls super(number) will be 2.

However, when you call test1.getNumber(), that's where the virtual kicks in. Virtual means that it will always call a method in the constructed class rather than the type of variable that you've declared it as. In other words, rather than calling A's getNumber like you thought it would, it actually call's B's getNumber instead. B's constructor doesn't assign a value to B's number variable, so you get 0.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Superclass method obtain subclass variable

From Dev

Subclass Reference by Superclass variable?

From Dev

Java:Method called from subclass in superclass constructor

From Java

Polymorphism - how to check if a superclass is a subclass?

From Dev

Java superclass variable and subclass copy constructor issue

From Dev

How to modify superclass variable from subclass?

From Dev

How to access subclass variable from superclass?

From Dev

Inheritance :hidden variable of superclass in subclass

From Java

Variable with same name in subclass and superclass

From Dev

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

From Java

A superclass method is called instead of the subclass method

From Java

How can do a Polymorphism with subclass in a superclass?

From Dev

Superclass method with subclass data

From Dev

How to access overridden variable of subclass using instance of superclass

From Java

Why is an instance variable of the superclass not overridden by a subclass?

From Dev

Alter superclass' variable from subclass on Python

From Dev

C++ - Casting a variable of {superclass} to {subclass}

From Dev

Accessibility to variable of superclass when initializing the subclass

From Java

Difference between creating an object of subclass in SuperClass Vs Subclass

From Dev

New to Java: SuperClass and SubClass

From Java

How do you call subclass method in superclass, in Java?

From Dev

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

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

How to make Superclass Method returns instance of SubClass

From Dev

How to access a protected method of superclass in a subclass?

From Dev

How to use subclass parameters in superclass method?

From Dev

How to have a method in superclass that validates a field in the subclass

Related Related

  1. 1

    Superclass method obtain subclass variable

  2. 2

    Subclass Reference by Superclass variable?

  3. 3

    Java:Method called from subclass in superclass constructor

  4. 4

    Polymorphism - how to check if a superclass is a subclass?

  5. 5

    Java superclass variable and subclass copy constructor issue

  6. 6

    How to modify superclass variable from subclass?

  7. 7

    How to access subclass variable from superclass?

  8. 8

    Inheritance :hidden variable of superclass in subclass

  9. 9

    Variable with same name in subclass and superclass

  10. 10

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

  11. 11

    A superclass method is called instead of the subclass method

  12. 12

    How can do a Polymorphism with subclass in a superclass?

  13. 13

    Superclass method with subclass data

  14. 14

    How to access overridden variable of subclass using instance of superclass

  15. 15

    Why is an instance variable of the superclass not overridden by a subclass?

  16. 16

    Alter superclass' variable from subclass on Python

  17. 17

    C++ - Casting a variable of {superclass} to {subclass}

  18. 18

    Accessibility to variable of superclass when initializing the subclass

  19. 19

    Difference between creating an object of subclass in SuperClass Vs Subclass

  20. 20

    New to Java: SuperClass and SubClass

  21. 21

    How do you call subclass method in superclass, in Java?

  22. 22

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

  23. 23

    Java: returning subclass in superclass method signature

  24. 24

    Superclass reference not able to call subclass method in Java

  25. 25

    Java Inheritance: Calling a subclass method in a superclass

  26. 26

    How to make Superclass Method returns instance of SubClass

  27. 27

    How to access a protected method of superclass in a subclass?

  28. 28

    How to use subclass parameters in superclass method?

  29. 29

    How to have a method in superclass that validates a field in the subclass

HotTag

Archive