1

I have an interface(A) the contain method myMethod() and a class(B) also contain the same method myMethod() and another class(c) extends and implement A and B.

No i created the object of class A a = new C() and call the a.myMethod();.

How it is executing the myMethod of class B.

interface A {
    void myMethod();
}

class B {
    public void myMethod() {
        System.out.println("My Method");
    }
}

class C extends B implements A {

}

class MainClass {
    public static void main(String[] args) {
        A a = new C();
        a.myMethod();
    }
}

Output of the program -

 My Method
Vimukthi
  • 846
  • 6
  • 19
  • 1
    Why does this surprise you? What did you expect the result to be? – Boris the Spider Jun 25 '19 at 05:53
  • 1
    @BoristheSpider I just want to know the concept behind it. I am not much more aware about OOPs concepts. – Dheeraj Singh Bhadoria Jun 25 '19 at 05:55
  • You require a `class` to have a method matching the signature in the `interface` and you then indicate that the same class should inherit from a `class` that provides such a method. I am not sure what your question is. Might I suggest reading a [tutorial](https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html) on the very basics of inheritance? – Boris the Spider Jun 25 '19 at 05:58
  • Think of it this way, the method `myMethod` is also in `C`, because `C` inherits `B`. – Sweeper Jun 25 '19 at 06:00
  • 1
    It is not a bad question, and I think this is dealt with differently for example in C#. https://stackoverflow.com/q/2371178/14955 – Thilo Jun 25 '19 at 06:03
  • If you're learning OOP, then the FIRST thing I would recommend is start using actual words - replace A, B C with (say) "Audible", "Bird", Canary", and rename "myMethod" to "sings()". You can now immediately see that Canary is a type of Bird that is audible (it sings), and your main method becomes "Audible audible = new Canary(); aubible.sing();". Do you see how, mapping the classes to everyday objects, the OOP concepts become intuitive and natural, rather than something to be learnt by rote ? – racraman Jun 25 '19 at 06:04

3 Answers3

1

The methods have the same signature. This means, that the methods look the same, after all types have been reduced to their most compact form, and all (seemingly) redundant information has been removed during compilation. It is what is ultimately 'precompiled' to the .class files.

What the compiler remembers about the interface method: name: myMethod, returns: void, parameters: {}

What the compiler remembers about the interface method: name: myMethod, returns: void, parameters: {}

(For both methods, the compiler also remembers, that they are instance-methods, as opposed to static methods, and therefore there actually is a parameter [the 'this' object]. But this is only interesting much later in your developer life :))

So in essence, they are the same method, to the compiler. In other words, the class you extend covers that method for you, and you do not have to implement it explicitly. You can however (if you wish to) override the behavior of your extended class B and do something else, if required.

TreffnonX
  • 2,924
  • 15
  • 23
  • @Thilo true. I applied too much information here :) – TreffnonX Jun 25 '19 at 06:13
  • 1
    @BoristheSpider Its not incorrect though, just too much information. The remaining signature is still the erasure signature, if generics have been removed or not. But it was confusing here, so I removed it. – TreffnonX Jun 25 '19 at 06:15
1

First it will look for implementation of myMethod() in class C. since it is not there it will check in your extended class B. Then it will execute that class B myMethod(). But if you have implemented myMethod() in class C your program will execute class C myMethod().

Vimukthi
  • 846
  • 6
  • 19
0

The executed method depends on the type of the object and not the variable (polymorphism). In your case the object is of type C which inherits the implementation of myMethod from type B.

Baher
  • 86
  • 3