we cannot create an object of interface but we can create
a variable of it

Answer Posted / mahesh

For creating an object, interface does not contain the body of its abstract methods. Hence compiler does not have enough data for creating an object out of it.
On the other hand you can create a variable of that interface with any other class implementing that interface.
E.g. A is an interface and B is a class implementing A.
Now if you create
A obj = new A();
This statement wont give an implementation of methods inside A.
But,
class B implements A{
}

A obj = new B();
Here class B will be defining all the abstract methods inside interface A.
Hence it is not possible to create an object of interface but you can create a variable of it.

Is This Answer Correct ?    9 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which is better ascii or unicode?

577


How many classes can any class inherit java?

512


What is a generic type?

566


What is a protected class in java?

546


Where is java located?

502






What will happen when using pass by reference in java?

528


What is method in java with example?

494


Why main method is called first in java?

547


What are the basic interfaces of java collections framework?

611


What is the purpose of the enableevents() method?

587


Why method overriding is used?

543


Give example to differentiate between call by value and call by reference.

597


What are class types in java?

583


Is 0 true or is 1 true?

520


What ide should I use for java?

496