"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 the same as procedures?
What methodology can be employed to locate substrings inside a string?
Tell me the latest versions in java related areas?
How does thread synchronization occurs inside a monitor? What levels of synchronization can you apply?
What does null mean in java?
What is main function purpose?
Does java set allow duplicates?
Which collection is sorted in java?
What is string variable?
Why is it important to initialize a variable?
What is purpose of find feature?
What is the right data type to represent a price in java?
In how many ways we can create threads in java?
What are heterogeneous objects?
What is boolean strategy?