wood burning stoves 2.0*
The moose likes Java in General and the fly likes What are practical scenarios for using abstract classes? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What are practical scenarios for using abstract classes?" Watch "What are practical scenarios for using abstract classes?" New topic
Author

What are practical scenarios for using abstract classes?

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
In all my projects there were many interfaces being defined and used but never did I get chance to use an abstract class. An interviewer asked me where did you use interfaces I told him the scenerios as I had used them Then he asked me about abstract classes. I explained him but he also wanted to know where I used. I could not tell as I have never used an abstract class in my career.What are the practical scenerios in application where they use them
thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
I've never created an abstract class, either, but for further information, read the topic "abstract class Vs Interface real life scenerios" linked in the "Similar Threads" box at the bottom of this page; it has some good information.

Another example is the javax.servlet.http.HttpServlet class. It has lots of useful methods for implementing servlets, but by itself does not do anything useful - so in order to make sure that it is being extended, instead of being used directly, it is declared abstract.


Ping & DNS - my free Android networking tools app
Balaji Vankadaru
Ranch Hand

Joined: May 31, 2013
Posts: 47
Use case for Interface and Abstract Classes

If your design or your project Implementation is assumed to stay unchanged for certain period of time them Interface would be a wise option.

As Interface is a way of Implementing Multiple Inheritance in Java.

Lets say if your project Implementations get changed very frequently implementing abstract class is a better approach.

In Project level Usage

Dao's are the best candidates for Implementing Interfaces.

Abstract class would serve the purpose when you want to declare Custom Exception for your entire Application.

Abstract class would be a good choice when you have a Parent and child relationship and you want to enforce that your subclass provides implementation to the methods of your parent class.

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I use abstract classes all the time. First, I create an Interface that defines the needed methods. Then I decide which methods are going to be common to all subclasses/implementations. These methods go in the abstract class.

All concrete implementations (classes) simply declare that they extend the abstract class and implement the interface. They all get all the common code.
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 639
Thanks all.

Abstract class would serve the purpose when you want to declare Custom Exception for your entire Application.


I will try this and try to understand why abstract class serves the purpose here.

thanks again.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Monica. Shiralkar wrote:
Abstract class would serve the purpose when you want to declare Custom Exception for your entire Application.
I will try this and try to understand why abstract class serves the purpose here.

This is not a good example, IMO, of a typical use for an abstract class. If anything, Pat's example is more typical.

An abstract class can provide basic implementations of some methods while leaving other methods for subclasses to implement. You will typically find this in classes provided by frameworks. See this paper: http://hillside.net/plop/plop97/Proceedings/woolf.pdf


Junilu - [How to Ask Questions] [How to Answer Questions]
 
 
subject: What are practical scenarios for using abstract classes?