7

If I have two classes, A and B,

public class A {
    public int test() {
        return 1;
    }
}

public class B extends A{
    public int test() {
        return 2;
    }
}

If I do: A a1 = new B(), then a1.test() returns 2 instead of 1 as desired. Is this just a quirk of Java, or is there some reason for this behavior?

  • 1
    This is called the `dynamic binding`, You need to know about the `static binding` also. It gets little trickier when you try to override (infact hide) the fields instead of methods. You will find lot of questions on this on SO or google. – RP- Sep 04 '12 at 15:37

8 Answers8

5

This is called polymorphism. At runtime the correct method will be called according to the "real" type of a1, which is B in this case.

As wikipedia puts it nicely:

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).

Tudor
  • 61,523
  • 12
  • 102
  • 142
4

No, that is correct (it is due to polymorphism). All method calls operate on object, not reference type.

Here your object is of type B, so test method of class B will be called.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Thanks, this is what I was confused on. Was expecting behavior based on reference type. –  Sep 04 '12 at 21:01
2

This is polymorphism and more specifically in Java overriding. If you want to invoke Class A's test method from Class B then you need to use super to invoke the super classes method. e.g:

public class B extends A{
   public int test() {
       return super.test();
}
Chris B
  • 925
  • 4
  • 14
0

This is intended behavior. The method test() in class B is overriding the method test() of class A.

Marcelo
  • 11,218
  • 1
  • 37
  • 51
0

For

A a1 = new B();

a1 is pointing towards the object of B which is the real type at run-time. Hence value is printed from Object B.

Santosh
  • 17,667
  • 4
  • 54
  • 79
0
A obj = new A();
obj.test()

will return 1

A obj = new B();
obj.test()

will return 2

B obj = new B();
obj.test()

will return 2

As stated in other answers this is how polymorphism works.

This post may make things a bit clearer

Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
0

Java uses dynamic binding (or late binding), so the method of B is called, not A. This is the opposite of static binding. There is a nice example here.

arshajii
  • 127,459
  • 24
  • 238
  • 287
0

You declare your object as A but your instance is B. So the method which will be called is from class B. B extends A(we can say that A is parent for B) if you will comment method test in B and then recall this method, in this case the method invoked will be test from A class and will return 1.

arnoldrob
  • 813
  • 3
  • 9
  • 15