In: Computer Science
If a class A implements interface I1 and class C and D are derived from class A, a variable of type I1 can be used to hold references of what type of objects? which one is correct
1. A, C, and D
2. A and D
Ans)
I1.java
public interface I1 {
int ref=0;
public void reef();
}
////
A.java
public class A implements I1{
int ref=0;
public void reef()
{
System.out.println("in class
A");
}
}
///
c.java
public class C extends A {
public void reef()
{
System.out.println("in class
C");
}
}
//
D.java
public class D extends A {
}
///
Test.java
public class test {
public static void main(String[] args) {
I1 d=new D();
d.reef();
I1 c=new C();
c.reef();
I1 a=new A();
a.reef();
}
}
///
In the above code you can see that there is no method in class D ,hence the refrance pointer of Interface I1 will check in Class A to print method D
Screenshot of the result in console is shown below:
Thank You!