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
I don’t want my class to be inherited by any other class. What should I do?
What is a qualifier in a sentence?
What is immutable in java?
Why is the main method static?
What is the disadvantage of java?
In java, how we can disallow serialization of variables?
Can we define a package statement after the import statement in java?
What are aggregate functions explain with examples?
Can we able to pass objects as an arguments in java?
What is string in java?
Explain scope or life time of local variables in java?
How to invoke external process in java.
What are the different types of multitasking?
What is a return in java?
What is adapter in java?