2

I have the following java code:

class A {
 int someMethod () { return 1; }
 int someMethod (A a) { return 2; }
 int someMethod (B b) { return 3; }
 int someMethod (C c) { return 4; }
 static A anotherMethod ( Object obj) { return (A) obj; }
 }

class B extends A {
 int someMethod () { return 6; }
 int someMethod (A a) { return 7; }
 int someMethod (B b) { return 8; }
 int someMethod (C c) { return 9; }
 static A anotherMethod ( Object obj) { return (B) obj; }
 }

 class C extends A {
 int someMethod () { return 11; }
 int someMethod (A a) { return 12; }
 int someMethod (B b) { return 13; }
 int someMethod (C c) { return 14; }
 static C anotherMethod ( Object obj) { return (C) obj; }
 }

public static void main ( String [] args ){
 A a = new A(); B b = new B(); C c = new C();
 System .out. println (A. anotherMethod (b). someMethod (b));
}

As expected the output is 8.

Ok now i delete the someMethod(B b) in class A:

class A {
 int someMethod () { return 1; }
 int someMethod (A a) { return 2; }
 int someMethod (C c) { return 4; }
 static A anotherMethod ( Object obj) { return (A) obj; }
}

I discussed the output with my friends, but nobody could explain exactly why we get a 7 as output now?!?!???

knacker123
  • 79
  • 9

4 Answers4

3

This happens because this snippet:

A.anotherMethod(b)

gives you an object typed to A. You then call:

.someMethod(b)

on that instance. Now, seeing as the A class doesn't have a someMethod(B b) method any more, it will instead call someMethod(A a) - which it can do because B is a subclass of A.

Because the instance you called the method on is actually of type B, and the B class overrides someMethod(A a) that's the one that ends up being called, hence your output of 7.

JonK
  • 2,097
  • 2
  • 25
  • 36
0

This is because you are casting B object to A firstly and then you have a reference of type A inside a variable called b and when you pass b to the method what you are really passing is a reference to A.

A.anotherMethod (b) // Casts and returns a reference of type A. 
.someMethod(b) // What you really have here is a reference of type A 

so this will call the someMethod(A a) and therefore will output 7.

0

You can not override static methods in java.

Even if you override that will not consider or work as overriden method.

Below link is for your reference.

Can I override and overload static methods in Java?

Community
  • 1
  • 1
Pratik
  • 944
  • 4
  • 20
0

If code same scenario in more simple way, then everything becomes clear:

public class Playground {

public static void main(String[] args) throws Exception {
    B b = new B();
    A casted = (A) b;
    System.out.println(casted.someMethod(b));
}

}

class A {
int someMethod(A a) { return 2; }
int someMethod(B b) { return 3; }
}

class B extends A {
int someMethod(A a) { return 7; }
int someMethod(B b) { return 8; }
}

Hence, we got 'casted' object which is instance of B, but casted to A. When we call someMethod(b) on it, JVM pick up A.someMethod(B b) and exeutes its overrided version. In case when A.someMethod(B b) is not presented, JVM pick up A.someMethod(A a) and executes its overrided version.

andrii
  • 1,248
  • 1
  • 11
  • 25