This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract classes" Watch "Abstract classes" New topic
Author

Abstract classes

Karan Rajan
Greenhorn

Joined: May 19, 2005
Posts: 12
My understanding of the purpose of abstract classes is that the abstract classes let you define some behaviors; they force your subclasses to provide others.

Now, an abstract class need not have any abstract methods. So if we do have an abstract class without any abstract methods then we will not be forcing a subclass to override any method.

So why would we want to have an abstract class without any abstract methods ?
I noticed this as I was doing some reading on HttpServlet class. The HttpServlet class is an abstract class without any of its methods being abstract.

Many thanks.
Kamal Ahmed
Ranch Hand

Joined: Feb 15, 2005
Posts: 91
It�s possible to declare a class as abstract without including any abstract methods. This is useful when you�ve got a class in which it doesn�t make sense to have any abstract methods, and yet you want to prevent any instances of that class.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
If the class were not abstract, then a developer may be tempted to use it as is. But in the case of HttpServlet, at least one of its methods must be overridden (usually doGet() or doPost()). So, my guess is that Sun is forcing a developer to subclass HttpServlet who, in doing so, is then more likely to do the right thing and override the appropriate method(s).


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There are some abstract DefaultWhatever classes in Swing. They implement all the methods required by the Whatever interface, but all the methods do nothing. You can extend this class to override any methods you need with less effort than implementing all the methods in the interface. It feels like cheating somehow, but it's very handy.


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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24168
    
  30

Hi,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Initials aren't enough. You can change your display name here. Thanks!


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract classes
 
Similar Threads
Basic queries about Servlet
another mock exam question
HttpServlet
why HttpServlet is abstract?
abstract class without abstract method