Can someone please show me and example of an abstract class in Java? Something with a real world application (rather than a text-book sample) would be preferable.
Thanks!
Can someone please show me and example of an abstract class in Java? Something with a real world application (rather than a text-book sample) would be preferable.
Thanks!
Read this Tutorial on Abstract Methods and Classes.
First, you declare an abstract class, GraphicObject, to provide member variables and methods that are wholly shared by all subclasses, such as the current position and the moveTo method. GraphicObject also declares abstract methods for methods, such as draw or resize, that need to be implemented by all subclasses but must be implemented in different ways. The GraphicObject class can look something like this:
abstract class GraphicObject {
int x, y;
...
void moveTo(int newX, int newY) {
...
}
abstract void draw();
abstract void resize();
}
Each non-abstract subclass of GraphicObject, such as Circle and Rectangle, must provide implementations for the draw and resize methods:
class Circle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
class Rectangle extends GraphicObject {
void draw() {
...
}
void resize() {
...
}
}
For a real-world example, why not look at java.util.AbstractCollection
?
For a real world example of an abstract class, lets take a look at AbstractTableModel
. AbstractTableModel
provides an implementation of the TableModel
interface (for use with a JTable
). TableModel
defines the following methods:
public boolean isCellEditable(int rowIndex, int columnIndex);
public Class<?> getColumnClass(int columnIndex);
public int getColumnCount();
public int getRowCount();
public Object getValueAt(int rowIndex, int columnIndex);
public String getColumnName(int columnIndex);
public void addTableModelListener(TableModelListener l);
public void removeTableModelListener(TableModelListener l);
public void setValueAt(Object aValue, int rowIndex, int columnIndex);
AbstractTableModel
provides a default implementation for all but three of these methods (getRowCount
, getColumnCount
, and getValueAt
), forcing the exteding class to provide at least the implementation to define the size and provide data to the table. It also provides a good default handling of listeners, as well as a bunch of convenience methods to fire events to those listeners.
I think this serves as a common real-world example:
This may be a bit further away from what you requested but lets say we are using an ORM framework like Hibernate (no need to really worry about what that is or what it means, just know that we are basically going to be inserting/updating and retrieving objects from a database).
For each table we want to insert into, we are going to have a unique database access object (DAO) to perform our set of desired database operations, but its important to note that all DAOs will be performing some similar tasks so this is where abstract classes can come to our rescue. We can declare a AbstractBaseDao class which outlines some basic operations we might need for all of our DAOs:
public class AbstractBaseDao {
public void saveOrUpdate(Object o){
//save or update object from database
}
public void loadAll(Class clazz){
//Load all from database
}
public Object loadById(Class clazz, Long id){
// Retrieve object by Id from database
}
// Additional common methods
}
So once again its not terribly important how those methods are implemented for this example, just realize that all DAOs need those methods and an abstract class gives us code-reuse amongst classes.
Now with our BaseDao we are free to make concrete and specific DAOs for each table that still have our desired common functionality:
public class PersonDao extends AbstractBaseDao{
public List<People> listAllPersonsByLastName(String lastName){
//Load people by last names from database
}
public List<People> listAllPersonsByBirthYear(Date date){
//List people by birth year from database
}
}
Now PersonDao can perform any specific tasks and we don't waste anytime rewriting code that we've already written. A nice example of code-reuse.
I think that this is a quite common example of the use of abstract classes in the industry so I hope it helps with what you were looking for.