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
What are methods of a class?
What is the use of accept () method in java?
Can a class extend 2 classes in java?
why we use merge option in hybernate pls give a ex snippet
What variables are stored in stack?
Which way a developer should use for creating thread, i.e. Sub classing thread or implementing runnable.
How do you write methodology?
Which api is provided by java for operations on set of objects?
Can we restart a dead thread in java?
what is singleton in java?
What are decalarations?
Is java developer a good career?
Why is multithreading important?
What do you understand by the bean persistent property?
Does java list allow null?