"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 are the Main functions of Java?

602


What's the purpose of using break in each case of switch statement?

487


What is java full form?

505


What is Java Package and which package is imported by default?

567


describe synchronization in respect to multithreading? : Java thread

476






How does predicate work in java?

504


What about main thread in java?

618


What is the difference between an inner class and a sub-class?

485


What's the default access specifier for variables and methods of a class?

484


Why do we use return statement?

519


What is string [] java?

501


Can we overload final method in java?

528


Howto get an object that will perform date & time calculations then format it for output in some different locales with different date style.can ne1 tel me the answer of this question.pls

1428


Is space a char?

511


Can inner class extend any class?

575