According to the code specified below, b.fun2() is not allowed in main method, I know why it is not working. I wonder that how can I solve these kind of problem. According to the solid programming principle, I need to write an interface for my problem. But this problem prevent me to define an interface.
public interface A{
void fun();
}
public class B implements A{
void fun(){
// some code
}
}
public class C implements A{
void fun(){
// some code
}
void fun2(){
// some code
}
}
public class Main {
public static void main(String[] args){
A b = new C();
b.fun2();
}
}
C, why not just declare the variable as typeCinstead ofA?main()method. The code you wrote would be wrong anywhere.Ais without methodfun2(), or whybis typeA. Using interface is for implementing it with more than 1 implementation. Change in both codings will result in a new design.