This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class and Interface

 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here I don't want to know the differences between them. But How did you guys actually used them in Real Time Application.
Mine use of interface in real time application:
Currently, we are using MySQL database. I developed one class and implement this interface. I used interface here because I thought if tomorrow database changes to Oracle then all I need to do is to create one more class and provide implementation of those methods related to new database whatever differs.

Is this correct to use Interface?

But I couldn't see any real time scenario where I must use abstract class. . I know that when you want to provide common characteristics to some classes at that time you can use Abstract class. Like Shape could be abstract class which provides implementation of method color but leaves the implementation of method area to its subclasses.

But I don't find any scenario where I can use abstract class. So I would like to know where you people have used abstract class and interface in real time?
[ December 31, 2008: Message edited by: Vishal Pandya ]
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use Abstract classes a lot. It'd be hard to give a specific example without explaining the business domain I work in.

But for an excellent example, look no further than Java itself. As one example, there is the List interface. There are a number of classes that implement that interface directly. Two of them -- AbstractList and AbstractSequentialList -- are abstract in order to provide some common functionality. AbstractSequentialList is actually a subclass of AbstractList so that it takes the common functionality of AbstractList and further enhances it. These abstract classes than have concrete classes.

And to take it a step further, the Apache Commons Collection API has a number of List classes. Most implement the List interface from Java. The Commons Collection API has a number of Abstract List implementations that the final concrete classes then use.

There are many other examples within Java itself. Take a look through the API for more. Ans there are many examples within many of the Apache Common projects.
[ December 31, 2008: Message edited by: Mark Vedder ]
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark! Oh! One more Mark. Anyways that was a very good example. But I am tempted to use one in my application. Let's say it's very simple application. Registration Module and Login Screen. Once user successfully logs in he will be presented with all the information that he has given at the registration time. There would be one option to edit those information.And Logout. That's it. Now is there any scope of using Abstract class?
[ December 31, 2008: Message edited by: Vishal Pandya ]
 
Paul Clapham
Sheriff
Pie
Posts: 20757
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're asking about real life examples, not real time examples, right? A real-time application is one which must do each of its tasks in a fixed time interval. This is a very specialized kind of application and is not usually written in Java. (For example, a pause for garbage collection could make the application fail.)

Anyway, where I work we have an order entry program which accepts orders in a particular format. Generally it's a stream of data where the data items (customer number, item number, quantity, delivery date, and so on) are separated by + signs. However there are several different ways of specifying the data items (e.g. sometimes the customer number is preceded by a C, sometimes it's the first item in the stream and not preceded by anything, and so on). A stream may contain only one order or it may contain many orders.

So we have an abstract class which knows how to process a stream of data where the data items are separated by + signs. So it has code which does that. It has abstract methods which must do things like finding if a particular data item can appear at this point in the stream, or finding out what kind of data item the current one is, or doing the processing for a data item. It also has concrete methods which do things like adding an item to an order or sending a completed order to be processed.

Then we have several concrete subclasses of that abstract class, one for each of the actual versions of the format. And whenever we find we have to support a new version (as we did just last month) then we write a new concrete subclass which implements all the abstract methods in the right way for that version.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic