I have the following classes and interface
public class A {
public void printSomething() {
System.out.println("printing from A");
}
}
interface B {
public void printSomething();
}
public class C extends A implements B {
public static void main(String aa[]) {
C c = new C();
c.printSomething();
}
}
When I compile the above code, it compiles without any errors. And when I run the program it prints the following.
printing from A
I was expecting the compiler to give a similar error like the following error that I got when my class C did not extend class A
C.java:1: error: C is not abstract and does not override abstract method printSomething() in B public class C implements B{
^
C.java:8: error: cannot find symbol
c.printSomething();
^
symbol: method printSomething()
location: variable c of type C
2 errors
I know that when a class implements an interface then we need to define the interface methods in the class or declare the class abstract. But then how just by extending a class (with a defined method of same signature) am I able to overcome this compiler warning and run the program as well.
Ccan be treated as anA; andAhas that method... - Boris the Spiderinterfaceforces you to have acces to the method defined in theinterfacein the class, not to actually have it written inside the class that does implement theinterface. If the class does inherit from a class that did already define the method and it has access to it the conditions are fullfilled and hence you don“t need to overwrite the method again. - SomeJavaGuy