I'm starting to think I don't understand polymorphism quite like I thought I did.
I have the following situation:
public class Testing {
public static void main(String[] args){
interTest myTest = new classTest();
myTest.classMethod();
}
}
With the given interface:
public interface interTest {
public boolean checkBoolean();
public void method();
}
And then the concrete class:
public class classTest implements interTest{
public classTest() {
// TODO Auto-generated constructor stub
}
public void classMethod(){
System.out.println("fail");
}
// Both method() and checkBoolean() are overridden here & do nothing.
}
}
The Oracle documents demonstrate implementing an interface and then adding additional methods, or even implementing multiple interfaces (and therefore including methods that aren't in one of the interfaces) and I thought this was commonplace, until I ran into issues trying to do this myself.
In this instance I am not able to access classMethod
because it is not inside the interface.
The method classMethod() is undefined for the type interTest
What am I not understanding about polymorphism? I thought declaring a variable in the form:
Interface object = new ConcreteClass();
created an interface object that could access ConcreteClass() methods. This is how you make multiple objects that are all of the same type (interface) and can fit in a type specific list but are different.
Why can't I call the myTest.classMethod()
method?