"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

How many types of operators are there?

756


What is numeric function?

759


Can a static class have a constructor java?

765


What does g mean in regex?

769


What is a substring of a string?

839


What is null mean in java?

868


What is the final class?

776


How does list work in java?

728


What is the purpose of sizeof operator?

784


What class of exceptions are generated by the java run-time system?

925


Which software is used for java programming?

796


Write a program to print count of empty strings in java 8?

788


How do you replace a string in java?

782


How do you reverse a list?

801


What are the differences between getting and load method?

782