Professional Documents
Culture Documents
1) Explain Abstract Class in Java
1) Explain Abstract Class in Java
1) Explain Abstract Class in Java
interface myinterface
{
void disp();
}
public class MyClass
{
public static void main(String[] args)
{
myinterface m1,m2=null;
m1.disp();
m1.fun();
m1.toString();
m1.equals(m2);
m1.hashCode();
}
}
Which of the above invocations are
illegal at compile-time?
Ans:- only “m1.fun();” is illegal, rest
all are legal at compile-time. It is
true that using interface reference we can
invoke only those methods which are present in it.
But “toString()” ,”equals()” and “hashCode()”
invocations are legal because these methods are
available inside “java.lang.Object” which is the base
class of all the classes in java. Now compiler
understands that any implementation of given
interface is by default sub class of “java.lang.Object”,
hence compiler allows you to invoke above
mentioned methods using interface reference.