0

Interface:

public interface InterfaceA {
    public void PartOfIinterfaceA();
}

Class:

public class Class_To_Test_Interface_encapsulation implements InterfaceA {

public void MethodM() {

}

@Override
public void PartOfIinterfaceA() {
    // TODO Auto-generated method stub

}

}

Main class:

public class MainClass {


public static void main(String args[]) {

    InterfaceA Ia = new Class_To_Test_Interface_encapsulation();
     Ia.MethodM();
}
}

Its giving me following error : The method MethodM() is undefined for in type InterfaceA() I very well know, why its giving me error and its logical. Also, I refrred , //Use methods declared in implementation that are not defined in interface

But, my question is, is there any other way we can , where a piece of code referring to an instance of B (with type InterfaceA) can in fact access m ?

madhead
  • 31,729
  • 16
  • 153
  • 201
Mihir
  • 45
  • 8

2 Answers2

2

It doesn't make sense to access m on an object (InterfaceA) that doesn't have m (the method is not defined in the interface). If you know that a subset of InterfaceA will have m defined, you need to determine membership in that subset before accessing m:

if (Ia instanceof Class_To_Test_interface_encapsulation) {
  ((Class_To_Test_interface_encapsulation)Ia).MethodM();
}
Mike
  • 501
  • 3
  • 5
  • Hi Mike, thanks for the solution. I have found an alternate solution as well as posted below. – Mihir Oct 27 '17 at 03:02
-1

InterfaceA Ia = new Class_To_Test_Interface_encapsulation().MethodM();

This will skip compile time error, and resolve at runtime correctly and call the method MethodM()

Mihir
  • 45
  • 8
  • Um. `new Class_To_Test_Interface_encapsulation().MethodM()` evaluates to `void`. You cant assign it to an `InterfaceA`. – omajid Oct 27 '17 at 03:13
  • new Class_To_Test_Interface_encapsulation() is a child type of Interface. So when i create a new objcet of that class it would definitely gets assigned to interface which gets resolved at run-time and than the method from the class Class_To_Test_Interface_encapsulation().MethodM() gets called. From where does void came from ? can you have a look at my original code once again – Mihir Oct 31 '17 at 06:07
  • The compiler sees `InterfaceA Ia = Class_To_Test_Interface_encapsulation().MethodM();` as `InterfaceA Ia = (new Class_To_Test_Interface_encapsulation().MethodM())`. It will call `MethodM` and assign the result of that to `Ia`, which is not compatible with type `void`. Your solution doesn't even compile: https://ideone.com/NE8dV4 – omajid Oct 31 '17 at 13:36
  • I have tried it ....it's compiling and running in eclipse... I am still not getting your point with void? – Mihir Nov 01 '17 at 00:33
  • What I am saying is that the solution you have posted cannot work. When you do `new Class_To_Test_interface_encapsulation()`, the result is of type `Class_To_Test_interface_encapsulation`. When you call the `MethodM()` on it, `MethodM` returns a result of type `void`. When the compiler sees the entire line, it see the type of the right hand expression is of type `void` (that's what `MethodM()` returns) and it thinks you are trying to effectively assign that type to `InterfaceA`. As the linked ideone.com example shows, `void cannot be converted to InterfaceA`. *something* is missing. – omajid Nov 01 '17 at 03:04
  • 1
    Hey, apologies to miss your point. Now I got it. It worked only when I do this public InterfaceA MethodM() { return new Class_To_Test_Interface_encapsulation(); } and than call the above method in Main using InterfaceA Ia = new Class_To_Test_Interface_encapsulation().MethodM(); Not sure whether this is the correct solution to my problem – Mihir Nov 01 '17 at 07:30
  • It probably isnt, but at least now we understand how your solution works. Thanks for explaining it! – omajid Nov 01 '17 at 13:42