• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class

 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i tried a class with abstract modifier without declaring any abstract methods.It's working.then what is the use of Abstract there.
it is also possible to define constructors for abstract classes.but we don't create objects to Abstract classes.then why?


Thanks
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a class abstract but still have non-abstract methods and constructors in it.

It's very useful because you could define a certain set of behaviors, but leave one or more behaviors undefined. You would then have to create a concrete subclass that defined the behaviors that the superclass didn't if you want to create an instance of it, but they would inherit the behavior(s) that the superclass defined.
 
Douglas Chorpita
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question is a good one.

You can define a class as abstract because you don't want anyone to instantiate it (because it really is not a "mature" class).

At the same time, you can define default implementations for ALL the methods. A class that subclasses this class isn't forced to override anything specific. Nevertheless, it is IMPLIED (but not required) that some methods, but any specific methods SHOULD be overridden.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic