"we cannot create an object of interface but we can create
a variable of it".diacuss the statement with the help of
an example.

Answer Posted / guest

1. Suppose interface interfaceDemo.java is as below

interface interfaceDemo {
public void methodA();
public void methodB();
}

2. Suppose class InterfaceDemoMain.java is as below

class InterfaceDemoMain implements interfaceDemo {
public void methodA() {
System.out.println("Inside methodA");
}

public void methodB() {
System.out.println("Inside methodB");
}

public void methodC() {
System.out.println("Inside methodC");
}

public static void main(String[] ar) {
InterfaceDemoMain idm = new InterfaceDemoMain();
interfaceDemo id;
id = idm;
id.methodA();
id.methodB();
id.methodC();// error
idm.methodC();
}
}

3. Here id.methodC(); will give error, only methods declared
inside interface are accessible to interface reference.

Is This Answer Correct ?    13 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

why java does not support unsigned keyword?

4262


What is the protected method modifier?

563


What kind of variables can a class consist?

591


How to sort list of list in java?

663


Why stringbuilder is not thread safe in java?

574






Can we override tostring method in java?

533


If two threads have same priority which thread will be executed first ?

844


Is java still necessary?

621


What is local class in java?

524


What are 5 boolean operators?

622


How do you join strings in java?

528


What is assembly language?

552


What are data structures in java?

535


Variable of the boolean type is automatically initialized as?

596


What mechanism does java use for memory management?

494