"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
What is a control variable example?
What is the difference between hashmap and hashtable in java?
What is a double?
In java, how we can disallow serialization of variables?
Can we inherit inner class?
Give any two differences between C++ and java.
What is the purpose of interface?
Why spring singleton is not thread safe?
How can u increase the heap size in the memory?
Name few "optional" classes introduced with java 8 ?
What is the maximum size of hashmap in java?
Can we have any code between try and catch blocks?
Is sizeof a preprocessor?
What is integers and example?
What is the difference between a choice and a list?