"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 are there separate wait and sleep methods? : Java thread

583


Explain JMS in detail.

620


What are the different types of constructor?

530


Define interface in java?

609


What are different types of arrays?

539






Which of the following is not an isolation level in the JDBC

1614


how to write a program for sending mails between client and server

1557


Can we call virtual funciton in a constructor ?

1780


What is collection class in java? List down its methods and interfaces.

534


Which programming language is most secure?

537


what is mena by object block any what is the use of that

1776


Can main() method in java can return any data?

653


What is final variable?

503


What is a dynamic array in java?

567


What is the purpose of file class?

550