I'm having trouble understanding some basic concepts about inheritance and polymorphism in Java.
I'm trying to call on child only methods for child classes that implement a given parent interface. However, I can not call on these child class only methods when I instantiate the child class object and put it in the parent class interface. How I can I do so without compromising polymorphism.
Contract.java
package test;
public interface Contract {
void getLog();
}
myContract.java
package test;
public class myContract implements Contract {
public void getLog()
{
System.out.println("logging in myContract");
}
public void anotherMethod()
{
System.out.println("another method");
}
}
app.java:
package test;
public class app {
public static void main(String[] args) {
// TODO Auto-generated method stub
Contract myContract = new myContract();
//I can't call below method in child class
myContract.anotherMethod();
}
}