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 ?

2
  • 2
    Downcast it to B? Commented Oct 26, 2017 at 23:20
  • 1
    In general: if you need to use a method from a class C, then you should have the reference stored in a variable of type C. Commented Oct 26, 2017 at 23:58

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Mike, thanks for the solution. I have found an alternate solution as well as posted below.
-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()

7 Comments

Um. new Class_To_Test_Interface_encapsulation().MethodM() evaluates to void. You cant assign it to an InterfaceA.
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
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: ideone.com/NE8dV4
I have tried it ....it's compiling and running in eclipse... I am still not getting your point with void?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.