Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why interface and abstract methods can't be instantiated?

Tags:

java

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

like image 444
satheesh.droid Avatar asked Mar 14 '11 11:03

satheesh.droid


3 Answers

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.

like image 131
Jon Skeet Avatar answered Oct 04 '22 18:10

Jon Skeet


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.

like image 42
Bert F Avatar answered Oct 04 '22 20:10

Bert F


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.

like image 35
Peter Lawrey Avatar answered Oct 04 '22 20:10

Peter Lawrey