"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

What is the properties class?

824


What is unmodifiable collection in java?

713


What is encapsulation in java?

855


Where is jre installed?

803


Explain polymorphism citing an example.

812


Define reflection.

754


What is compiler and what its output.

899


What is the default execution method in java?

870


What are recursive functions? Give some examples?

860


How to access arraylist elements in java?

715


Can singleton class be inherited in java?

743


what is the purpose of the runtime class?

794


Which container method is used to cause a container to be laid out and redisplayed in java programming?

865


Explain about object oriented programming and its features?

805


What are the two categories of data types in the java programming language?

731