Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Beginning Java and the fly likes abstract class with concrete methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "abstract class with concrete methods" Watch "abstract class with concrete methods" New topic

abstract class with concrete methods

Raghu Mat

Joined: Feb 19, 2004
Posts: 5
why does any one want to have an abstract class with concrete methods only,(NO abstract methods) if one cannot instantiate the class. Moreover, why does any one want to put concrete methods in an abstract class and extend it in order to use them.(Remember, the class contains concrete methods only and NOT abstract methods).
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
I think the question is more like... why wouldn't they? First of all, if a class ONLY has static methods, you aren't going to want to instantiate it anyway. What would be the point in doing so, when you can already use all of the methods externally? As for inheritence... an abstract concept like Food might have a "bite" method. There is pretty only one way to bite (yes, I know that can be debated, this is just an example), so if Apple and Orange inherited the bite() method from your abstract Food class, they would need to be the same. And of course, Food is abstract because there has never been an instance of food, just types of food.
[ July 01, 2004: Message edited by: Darin Niard ]
Jos� Alberto Mu�iz Navarro
Ranch Hand

Joined: Oct 27, 2003
Posts: 33
Well... if you have a concrete method you can call it in the subclass. That doesn't mean you should be able to instantiate the class.

On the other hand you have the abstract methods which cannot be directly called.
Sandeep Jindal
Ranch Hand

Joined: Aug 25, 2003
Posts: 180
Dear Raghu,
One of the good examples of your answer is adapter classes in Java2. Like MouseAdapter. It has all the methods defined(though empty) but still is an abstract class. And the reason is simple that sun wants us not to create the object of the class as it is.

Hope this clears.

SCJP 5.0
Raghu Mat

Joined: Feb 19, 2004
Posts: 5
Thanx everyone,
specifically to Sandeep, for pointing me to the MouseAdapter class.I am including the explaination at SUN site ,in case if any others want to be more clear on this topic.
:-) A interviewer really screwed me up, with this question.
public abstract class MouseAdapter
extends Object
implements MouseListener

An abstract adapter class for receiving mouse events. The methods in this class are empty. This class exists as convenience for creating listener objects.

Mouse events let you track when a mouse is pressed, released, clicked, when it enters a component, and when it exits. (To track mouse moves and mouse drags, use the MouseMotionAdapter.)

Extend this class to create a MouseEvent listener and override the methods for the events of interest.
(If you implement the MouseListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

Create a listener object using the extended class and then register it with a component using the component's addMouseListener method. When a mouse button is pressed, released, or clicked (pressed and released), or when the mouse cursor enters or exits the component, the relevant method in the listener object is invoked and the MouseEvent is passed to it.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You may find abstract classes with non-empty methods, too. We have an abstract command class that has a bunch of convenience methods. It's not a complete class because the main execute() method IS empty so it should not be instantiated and used as is. This is a common technique for "framework" type things.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: abstract class with concrete methods
It's not a secret anymore!