Recently in a interview I was asked a very general question "what is abstract in java".I gave the definition and it was followed with some other question on abstract as what is abstract method and difference between abstract method and concrete method and etc. Then at last interviewer asked to give a real time example when I should use or define a class as abstract.I got confused.I gave some example but he was not convinced.
I googled it but found no real solution.
So can someone give me real time example i.e. when he defined a class as abstract in his/her project and why?
Thanks.
This question is related to
java
class
abstract-class
I use often abstract classes in conjuction with Template method pattern.
In main abstract class I wrote the skeleton of main algorithm and make abstract methods as hooks where suclasses can make a specific implementation; I used often when writing data parser (or processor) that need to read data from one different place (file, database or some other sources), have similar processing step (maybe small differences) and different output.
This pattern looks like Strategy pattern but it give you less granularity and can degradated to a difficult mantainable code if main code grow too much or too exceptions from main flow are required (this considerations came from my experience).
Just a small example:
abstract class MainProcess {
public static class Metrics {
int skipped;
int processed;
int stored;
int error;
}
private Metrics metrics;
protected abstract Iterator<Item> readObjectsFromSource();
protected abstract boolean storeItem(Item item);
protected Item processItem(Item item) {
/* do something on item and return it to store, or null to skip */
return item;
}
public Metrics getMetrics() {
return metrics;
}
/* Main method */
final public void process() {
this.metrics = new Metrics();
Iterator<Item> items = readObjectsFromSource();
for(Item item : items) {
metrics.processed++;
item = processItem(item);
if(null != item) {
if(storeItem(item))
metrics.stored++;
else
metrics.error++;
}
else {
metrics.skipped++;
}
}
}
}
class ProcessFromDatabase extends MainProcess {
ProcessFromDatabase(String query) {
this.query = query;
}
protected Iterator<Item> readObjectsFromSource() {
return sessionFactory.getCurrentSession().query(query).list();
}
protected boolean storeItem(Item item) {
return sessionFactory.getCurrentSession().saveOrUpdate(item);
}
}
Here another example.
The best example of an abstract class is GenericServlet
. GenericServlet
is the parent class of HttpServlet
. It is an abstract class.
When inheriting 'GenericServlet' in a custom servlet class, the service()
method must be overridden.
You should be able to cite at least one from the JDK itself. Look in the java.util.collections
package. There are several abstract classes. You should fully understand interface, abstract, and concrete for Map
and why Joshua Bloch wrote it that way.
Here, Something about abstract class...
Real time example--
If you want to make a new car(WagonX) in which all the another car's properties are included like color,size, engine etc.and you want to add some another features like model,baseEngine in your car.Then simply you create a abstract class WagonX where you use all the predefined functionality as abstract and another functionalities are concrete, which is is defined by you.
Another sub class which extend the abstract class WagonX,By default it also access the abstract methods which is instantiated in abstract class.SubClasses also access the concrete methods by creating the subclass's object.
For reusability the code, the developers use abstract class mostly.
abstract class WagonX
{
public abstract void model();
public abstract void color();
public static void baseEngine()
{
// your logic here
}
public static void size()
{
// logic here
}
}
class Car extends WagonX
{
public void model()
{
// logic here
}
public void color()
{
// logic here
}
}
Source: Stackoverflow.com