0

In this program

class a
{
     int a=25;
    public void aa()
    {
        System.out.println(a);  
    }

}
class b extends a
{
    int a=2;
    public static void main(String[] args) {
        b x=new b();
        x.aa();

    }
}

why does "x.aa()" prints 25 ans why not 2?,what is the reason behind it?

class a
{
     int a=25;
    public void aa()
    {
        System.out.println(a);  
        b();
    }
    public void b()
    {
        System.out.println("this should print");
    }

}
class b extends a
{
    int a=2;
    public static void main(String[] args) {
        b x=new b();
        x.aa();

    }
    public void b()
    {
        System.out.println("this should not print");
    }
}

and if we consider the above output,then here again the output of b() of above should print "this should print" but we are getting the ouput from sublcass "this should not print"

user3239652
  • 775
  • 2
  • 9
  • 23
  • @John3136: That question is specifically about *static* fields. This one is about *non-static* fields. (As it happens, the answers are quite similar, but I wouldn't consider them to be duplicates, since the lack of the `static` modifier makes this question much more interesting.) – ruakh Oct 20 '14 at 04:58
  • because i thought since b is extending a,aa() method would get inherited and when it sees to print a ,first it looks it in b class,and finds it,so it should print that – user3239652 Oct 20 '14 at 05:07
  • that is what called function `overriding` `run-time polymorphism` – Rustam Oct 20 '14 at 05:08
  • @user3239652 when u extending that class and calling the method by default it will take variable from the base class and method of the sub class in your example like its happeing. – Kishan Bheemajiyani Oct 20 '14 at 05:10

4 Answers4

1

Class b inherits class A, so when you call x.aa, it is calling method aa of class a. The member a of class a is initialized with 25 so it prints 25. Class a does not know about the member a of class b.

Tu Le Hong
  • 95
  • 1
  • 8
1

Fields cannot be overridden, and are not virtual. B.a* is independent of A.a, to the point that they can actually have different types. There is no way for B to make A aware of B.a in place of A.a. Instances of B will actually have two fields named a, but one is hidden ("shadowed") by the other. (If desired, code B can refer to its A.a field by writing ((A)this).a. This is because the appropriate a is selected based on the type of ((A)this), which is A, rather than the runtime type of the instance, which would be B or a subtype of B.)

* Note: I have renamed your classes to A and B: per the Java naming conventions, class-names begin with uppercase letters.

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • @TheLostMind: Yes, I understood his/her question, and I believe I answered it. Every instance of `B` has both an `A.a` and a `B.a`. Since, within the definition of `A`, `this` has type `A`, any occurrence of `this.a` (or simply `a`) is referring to `A.a`, not to `B.a`. (By the way, a nitpick: since `A.a` isn't a method, it's not really "called".) – ruakh Oct 20 '14 at 05:23
0
b x=new b();

while you will do this this will print out put of the constructor as its giving because its clearly making the object of b class and while in the case of class A in that class that b() act as the method so it wont call when you are making object of class b.

while in the case of

b x=new b();
x.aa();

this it will by default call the inheritance and will print as u getting output. and one more thing.

 public void aa()
    {
        System.out.println(a);  
    }

in the case of this it will use the local variable of that particular class so it will print whatever you have defined on that class. in your case that is a=25;

by the way nice question.

Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68
0

method aa() is member of class a. so, method aa() can see/access instance member of class a. Now, you have inherited class a to b. it means object of class b can access/call aa(). but, it doesn't mean that aa() is allowed to access variable a of class b. thats why method aa() is printing the variable a of it's own class and prints 25.

now about your second program. in class b, you are overriding method b(). while calling an overridden method, the selection of method(whether from sub class or super class) is done on the basis of instance of a invoking object(whether sub class or super class respectively). you have called method aa() with the instance of class b that is why method b() of class b is called.

nitish005
  • 106
  • 11