Referencing through super class/interface reference - Java
I am new to Java and I understand the underlying basic concepts of
inheritance. I have a question regarding referencing through superclass.
As the methods of class inherited from a superclass or implemented using
an interface can be referenced through a superclass reference (interface
or class). How would it work when both extends and implements are involved
with a class?
class A {
void test() {
System.out.println("One");
}
}
interface J {
void first();
}
// This class object can referenced using A like A a = new B()
class B extends A {
// code
}
// This class object can referenced using J like J j = new B()
class B implements J {
// code
}
// my question is what happens in case of below which referencing for
runtime polymorphism?
class B extends A implements J {
// code
}
Which fails to compile with:
Main.java:16: error: duplicate class: B
class B implements J {
^
Main.java:21: error: duplicate class: B
class B extends A implements J {
^
2 errors
No comments:
Post a Comment