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

I don’t want my class to be inherited by any other class. What should I do?

589


What is a qualifier in a sentence?

541


What is immutable in java?

558


Why is the main method static?

612


What is the disadvantage of java?

514






In java, how we can disallow serialization of variables?

564


Can we define a package statement after the import statement in java?

590


What are aggregate functions explain with examples?

534


Can we able to pass objects as an arguments in java?

543


What is string in java?

570


Explain scope or life time of local variables in java?

573


How to invoke external process in java.

577


What are the different types of multitasking?

668


What is a return in java?

564


What is adapter in java?

527