Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I Instantiate an Abstract Class?

 
Mahendran Aiyappan
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I am new to Java. I want to know whether I can instantiate an Abstract class, directly or indirectly. Please provide me your useful comments.


Thanks & Regards,
Mahendran A.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahendran A, please check your private messages. You can see them by clicking My Private Messages.

To answer your question: No, you cannot instantiate an abstract class. An abstract class is a class that is not yet "finished" - it contains methods without an implementation. A non-abstract subclass is supposed to implement those methods.

See: The Java Tutorial - Abstract Methods and Classes.
 
sudhir nim
Ranch Hand
Posts: 212
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No abstract class can not be instantiated directly, but you can declare variables of abstract class type
[ November 16, 2007: Message edited by: sudhir nim ]
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also create anonymous subclasses that implement all abstract methods:

These anonymous subclasses only support the constructors of the abstract class though, you can't create your own ones. If you need that kind of behaviour you need to create actual subclasses.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic