"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
How concurrent hashmap works?
What is the static variable?
What is immutability in java?
What is runtime locatable code?
1.IN CASE OF DYNAMIC METHOD DISPATCH WHY WE USE REFERENCE VARIABLE,WE CAN USE THE DIFFERENT DEFINED OBJECT DIRECTLY TO ACCESS THE DATA MEMBER AND MEMBER FUNCTION OF THAT RESPECTIVE CLASS?WHAT IS THE MAIN FUNCTION OF "REFERENCE VARIABLE" HERE?
What invokes a thread's run() method in java programming?
Does java trim remove newline?
In Java list the methods that can be overridden?
Can a hashset contain duplicates java?
Can substring create new object?
Difference between abstract and concrete class ?
What is the use of arrays tostring () in java?
What is the association?
Can a class extends itself in java?
What are the main differences between the java platform and other platforms?