• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class

 
Emad Salahuddin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that if you have an abstract method, you need to declare the class as abstract as well. My question is that can a class be declared abstract without having any abstract methods in it.
Regards
Emad
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. An example from the Java API is any of the Adapter classes in the java.awt.event package.
This will compile:
public abstract class JunkTest {}
[ June 11, 2003: Message edited by: Thomas Paul ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't want a class to be instantiated then there are two options:
1) if you want the class to be used as a parent in an inheritance hierarchy then make the class abstract (java.awt.event.FocusAdapter)
2) if you do not want the class to be used as a parent in an inheritance hierarchy then make the class final and give it a private constructor (java.lang.Math)
 
Emad Salahuddin
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Thomas For your help.
 
Ma Ning
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Emad,
A great way to find answers to such questions is to simply code a simple example and see what the compiler has to say about it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic