3

Can anyone clarify me. Here instance method is overridden but variable is not. output is: B 10

class A{
    int i=10;
    public void name(){   
        System.out.println("A");
    }
}

class B extends A{
    int i=20;
    public void name(){        
        System.out.println("B");
    }  
}  

public class HelloWorld { 
    public static void main(String[] args){       
        A a = new B();
        a.name();
        System.out.println(a.i);
    }
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
Siddappa Walake
  • 303
  • 5
  • 14
  • 3
    Method calls are bound _dynamically_ (which means, they are resolved at run time) whereas accesses to instance variables are bound _statically_ (which means, they are resolved at compile time). And the compiler always uses the _declared_ type of a variable to resolve its instance variables. – Seelenvirtuose Sep 28 '17 at 10:02
  • @Seelenvirtuose +1 for that simple and nice explanation – Procrastinator Sep 28 '17 at 10:10
  • Yes,you are right.because of,To access a variable of the class or a instance is used the getstatic or getfield,the bytecode is not find the super class,but invoke the method that is override its superclass method will execute invokevirtual,the bytecode will find superclass method. – dabaicai Sep 28 '17 at 10:10

3 Answers3

4

You are absolutely correct. Methods are overridden in Java if the parameter list and function names are identical, and the return types are covariant.

i in the base class is simply shadowed: a.i refers to the i member in the base class, since the type of the reference a is an A, even though it refers to a B instance.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • @SiddappaWalake: yes you can call it the Super class too: I prefer the term Base class as I'm a C++ chap really. – Bathsheba Sep 28 '17 at 09:49
  • But why this is strange Here??. Variables are also instance members right?? – Siddappa Walake Sep 28 '17 at 09:49
  • @SiddappaWalake: Indeed they are, but the behaviour of class fields and class methods differ in this respect. – Bathsheba Sep 28 '17 at 09:50
  • But in oracle documenation they have mentioned like 'A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.' – Siddappa Walake Sep 28 '17 at 09:57
1

You cannot override attribute, you can only override method:

public class A{
    private int i=10;

    public void name(){   
        System.out.println("A");
    }

    public int getI(){
        return i;
    }
}

public class B extends A{
    private int i=20;

    public void name(){        
        System.out.println("B");
    }

    @Override
    public int getI(){
        return i;
    }
}  

public class HelloWorld { 

    public static void main(String[] args){
        A a = new B();
        a.name();
        System.out.println(a.getI());
    }

}

In your example, you define variable a as type A so the i value in B is ignored.

Al-un
  • 3,102
  • 2
  • 21
  • 40
  • Thank you bro. Is this explained any where? – Siddappa Walake Sep 28 '17 at 09:55
  • You can start [here](https://stackoverflow.com/a/4716335/4906586) knowing that it's more than common to have your attributes private and expose it via getters. Afterword, you can override the getters the way you want in subclasses – Al-un Sep 28 '17 at 10:00
1

In Java instance variables cannot be overridden, only methods can be overridden. When we declare a field with same name as declared in super class then this new field hides the existing field. See this Java doc Hiding Fields.

Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29