• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use Of abstract class without any abstract method  RSS feed

 
Deepak Sahu
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can someone explain one scenario where we will use a abstract class without having abstract method ?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, here's one from Swing: MouseAdapter.

If you want to use the MouseListener interface, there are 5 methods you need to implement. But it's quite common that you only need to react to one or two. So you've got to implement blank methods for the others.

The point of MouseAdapter is to make this a bit easier. It has blank implementations of all the methods in MouseListener (plus those in MouseMotionListener). If you use that you only need to override those that you are interested in.

The class is abstract because there is absolutely no point in instantiating it - without being overridden all the methods do nothing all. But the individual methods aren't abstract because then you'd have to implement them, defeating the object.
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make an abstract Vehicle class with methods like start and stop and concrete subclasses like Car and Motorcycle. There is no need for any of the methods to be abstract. If you do that, you can have subclasses with no fields and no methods, only constructors, because all the fields and methods are implemented in the superclass.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Deepak Sahu wrote:Can someone explain one scenario where we will use a abstract class without having abstract method ?

There are two I can think of:
1. A skeleton implementation of an interface that provides "default" implementations for all its methods. This is sort of "deprecated" since default implementations were introduced in version 8, but you might well find it in legacy(?) systems.
2. A "root" class that does the same. Off the top of my head I can't think of a burning need, and I've never come across one myself; but it might possibly be useful within a package, or as part of a framework. I suspect Matthew's example might fit the bill though.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another bigger example - HttpServlet. The idea is that you have to implement at least one of the service response methods (doGet, doPost, etc.). It's rare that you'll have to implement all of them, so making them abstract would be a pain, but a servlet that can't handle any response is pointless so you might as well make it abstract.

Really, making a class abstract can mean "it makes no sense to instantiate this, so we won't let you" as well as "you can't instantiate this because it contains methods with no implementation".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!