• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

abstract class with concrete methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Raghu Mat
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
----------
Regards,
Raghu
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
I've been selected to go to the moon! All thanks to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic