I am unable to figure out why instantiation of interface and abstract class is restricted in java. I know reference for implementation of interface and abstract class can be created. I am clear about that, but why it can't be instantiated? Anyone please help me
The point of both an interface and an abstract class is to provide an API which has to be implemented in a concrete class.
For example, suppose I declare this interface:
public interface Foo
{
int bar();
}
And imagine if this were valid code:
Foo foo = new Foo();
int x = foo.bar();
What could the value of x
be? We haven't specified an implementation of bar
anywhere. It's a meaningless call, without a real implementation to back it up.
the If you think of a class as blueprints for creating (instantiating) an instance, much like the blueprints for a house tell you how to build a house. Think of an interface as a floorplan for the house - its an incomplete view (specification) of the house. There isn't enough detail to build the house from it - its only an outline of the rooms. An abstract method is worse - its just the outline of one room.
Interfaces and Abstract classes are not concrete classes. They are deamed to be incomplete and not to be created. You can use a subclass or implementing class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With