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);
    }
}
3
  • 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. Commented Sep 28, 2017 at 10:02
  • @Seelenvirtuose +1 for that simple and nice explanation Commented Sep 28, 2017 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. Commented Sep 28, 2017 at 10:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

@SiddappaWalake: yes you can call it the Super class too: I prefer the term Base class as I'm a C++ chap really.
But why this is strange Here??. Variables are also instance members right??
@SiddappaWalake: Indeed they are, but the behaviour of class fields and class methods differ in this respect.
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.'
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.

2 Comments

Thank you bro. Is this explained any where?
You can start here 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
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.