Java inheritance confusion, superclass and subclass member variable having same name

Jeet Parekh

On running this code

class c1 { int ci = 1; }

class c2 extends c1 { int ci = 2; }

class inheritanceTest {
    public static void main(String args[]) {
        c1 c1reftoc2 = new c2();
        System.out.println(c1reftoc2.ci);
    }
}

Output is 1. So i guess that a subclass object with superclass reference variable, uses superclass variable in case of variables of same name in both classes.

But then, running this program from a SCJP question,

class Foo {
    public int a = 3;
    public void addFive() {
        a += 5;
        System.out.print("f ");
    }
}

class Bar extends Foo {
    public int a = 8;
    public void addFive() {
        System.out.println(this.a);
        this.a += 5;
        System.out.print("b " );
        System.out.println(this.a);
    }
}

class SCJPQC3 {
    public static void main(String[] args) {
        Foo f = new Bar();
        f.addFive();
        System.out.println(f.a);
    }
}

output is

8
b 13
3

The addFive() method called in main() would be that of class Bar because of overriding.

this.a in addFive() prints 8. Whereas f.a in main() gives 3. Instances being referred by this. and f. are same, yet they give different results. Using this.a gives 8 as output which contradicts my previous understanding.

So my question is, does this. refer to a class or an instance? I know that this. would refer to the object through which a method is called, but this particular program confused me.

rgettman

The keyword this here refers to the current object instance, on which the method was called. Because of polymorphishm, Bar's addFive method is called. That method refers to the a in scope there, which is Bar's a. (Bar's a hides Foo's a.) That is why 8 is printed initially, and why it gets changed to 13. However, when System.out.println(f.a); is called at the end of main, the compiler only sees f as a Foo variable, so it prints 3 -- Foo's a, which was never changed from 3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Variable with same name in subclass and superclass

From Dev

Inheritance :hidden variable of superclass in subclass

From Dev

Java inheritance; passing a subclass to an abstract method of a superclass

From Dev

Confusion over method parameter and instance variable having same name

From Dev

Inheritance - UIAlertView in superclass and subclass

From Dev

Is a subclass always in the same package as a superclass in Java?

From Dev

Java superclass variable and subclass copy constructor issue

From Dev

javascript object inheritance - subclass and superclass

From Dev

Subclass Reference by Superclass variable?

From Dev

Multiple Inheritance: same variable name

From Dev

New to Java: SuperClass and SubClass

From Dev

java polymorphism creating a new subclass object using a superclass variable

From Dev

Getting name of subclass from superclass?

From Dev

Getting name of subclass from superclass?

From Dev

Hibernate Inheritance - Getting superclass instance and casting into subclass

From Dev

Hibernate Inheritance - Getting superclass instance and casting into subclass

From Dev

how to subclass properties in superclass by using inheritance?

From Dev

Superclass method obtain subclass variable

From Dev

Parameterizing superclass with static member class from subclass

From Dev

Static variable and parameter having the same name

From Dev

Java superclass catch and subclass catch

From Dev

Java superclass calls subclass method

From Dev

initialise superclass array in subclass (java)

From Dev

Java superclass catch and subclass catch

From Dev

Can a member function of a superclass access a member function of a subclass? And how?

From Dev

Inheritance calling superclass method java

From Dev

can i use the name of subclass in my superclass?

From Dev

Lookup for EJB subclass by superclass EJB name

From Dev

java Having multiple constructors for superclass

Related Related

  1. 1

    Variable with same name in subclass and superclass

  2. 2

    Inheritance :hidden variable of superclass in subclass

  3. 3

    Java inheritance; passing a subclass to an abstract method of a superclass

  4. 4

    Confusion over method parameter and instance variable having same name

  5. 5

    Inheritance - UIAlertView in superclass and subclass

  6. 6

    Is a subclass always in the same package as a superclass in Java?

  7. 7

    Java superclass variable and subclass copy constructor issue

  8. 8

    javascript object inheritance - subclass and superclass

  9. 9

    Subclass Reference by Superclass variable?

  10. 10

    Multiple Inheritance: same variable name

  11. 11

    New to Java: SuperClass and SubClass

  12. 12

    java polymorphism creating a new subclass object using a superclass variable

  13. 13

    Getting name of subclass from superclass?

  14. 14

    Getting name of subclass from superclass?

  15. 15

    Hibernate Inheritance - Getting superclass instance and casting into subclass

  16. 16

    Hibernate Inheritance - Getting superclass instance and casting into subclass

  17. 17

    how to subclass properties in superclass by using inheritance?

  18. 18

    Superclass method obtain subclass variable

  19. 19

    Parameterizing superclass with static member class from subclass

  20. 20

    Static variable and parameter having the same name

  21. 21

    Java superclass catch and subclass catch

  22. 22

    Java superclass calls subclass method

  23. 23

    initialise superclass array in subclass (java)

  24. 24

    Java superclass catch and subclass catch

  25. 25

    Can a member function of a superclass access a member function of a subclass? And how?

  26. 26

    Inheritance calling superclass method java

  27. 27

    can i use the name of subclass in my superclass?

  28. 28

    Lookup for EJB subclass by superclass EJB name

  29. 29

    java Having multiple constructors for superclass

HotTag

Archive