Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why we can't create object to ABSTRACT & INTERFACE classes?

 
chenna kesava koyyagura
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, friends please tell me why we can't create objects to abstract classes as well as interface classes?


please help me.
kesava
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question makes no sense, but is almost certainly not an Advanced question. Please choose your forum more carefully.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply said, because it is possible that some methods are not implemented. In the case of an interface, non of the methods are implemented.

If you would be able to create an instance, and call a non-implemented method, what code should be executed? Not that of the class / interface itself, since there is no code to execute.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Rob's point, the abstract keyword is the way the author of a class tells readers and users that it is not complete and ready to use. Kind of like "Some assembly required" on the box a toy comes in.
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I think your question is:

"Why can't you instantiate an interface or an abstract class?"

The short answer: because that's how they are defined.

Definitions...

Instantiate - to create one instance of a class. The newly created object is a "kind of" the class.

Interface - a declaration of methods that are expected of a class. If the class declares they implement the interface, it must provide implementations for all the methods declared by the interface.

Abstract Class - a class that cannot be instantiated. Often it will include one or more methods that are also declared abstract and must be implemented by subclasses for the subclass to be concrete (opposite of abstract), and therefore able to be instantiated. Does not have to include abstract methods; the documentation may otherwise specify what is required of a valid concrete subclass.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic