Loading...

Sunday, March 9, 2014

When to use Abstract classes and interface in real time scenarios?

Question : When to use Abstract classes and interface in real time scenarios?

"Suppose you have to provide salaryincrement method(service) to your 
multiple clients.Every client has it's own logic for the increment.So 
how will you do it."

Main concern of this question was whether i'll use abstract class or 
interface?

Please don't give animal or car example.Just give any real application 
or project example in which at some place you used abstract class 
and at some place you used interface.

when u creating the project from scratch than you have to create the 
abstract class, one of the demerit of abstract class is that it 
provide tight coupling and interface provide loose coupling......for 
example create a abstract Animal class{
// provide the abstract method which are common to all animal like eat,run,etc
}

==================================

Declare your class as an abstract class when your class describes an 
abstract idea
rather than a specific one.
Lets take an example from real life for an abstract class:
public abstract class Food{
}
Food! who doesn?t like food! there are so much types of Food
but wait... what exactly is Food???? or, why did I declare Food abstract????
I'll answer that question with another question
Suppose i asked you "what did you eat for lunch?"
you may answer: "I ate pizza for lunch!" or maybe "I ate Pancake for lunch!"
but you will never answer, "I ate Food for lunch!"
why? because Food is an abstract definition for "Things we eat".
while Pizza, and Pancake are subclasses of food. in code it would look 
something like this:
public abstract class Food {}
public class Pizza extends Food {}
public class Pancake extends Food {}
You can declare instances of Pizza, and Pancake but not of food.
Ok. now, after we understood what an abstract class is, lets move to:
Abstract methods!
An abstract method is declared that way:
public abstract void abstractMethod();
Some important things concerned abstract methods:
1. Abstract method cannot be declared private!
2. Abstract methods don't have a body. these are empty methods.
so when declaring an abstract method, end the line with ";".
3.Only abstract classes can contain abstract methods!
(or interfaces, will be explained later).

Why would you want to use abstract method?
lets return to our food example.
to eat food, you have to prepare it.(cook\fry or so).
so how do you prepare Food?
there is no question for this question. why??
because each instance of food(pizza, Pancake, salad) is prepared in a 
different way!
public abstract class Food{
public food(){
}
public abstract void prepare();//the abstract method
}

public class Pizza extends Food {
public Pizza(){
}
public void prepare(){
//cut tomatoes, have the dough, add cheese, insert the oven...
}
}//end of pizza subclass

public class Pancake extends Food{
public pancake(){
}
public void prepare(){
//add all ingredients on the pan, add sauce, onions, fry it...
}
}//end of Pancake class

In that example you see how you use an abstract method.
abstract method should be used when you want to do the same operation, 
but in different ways.

INTERFACE
------------------
What is an interface?
Think of an interface as a contract that a class does.
in order to implement an interface a class must implement the 
interfaces methods.
An interface contains constants and abstract methods.(only abstract methods!).

Lets proceed, and have an example for using interfaces from the Java API.
The List interface.
A List is an interface.
ArrayList and LinkedList are classes implementin the List interface.
suppose in your code you nedd a List of String objects(could be any 
other data type though).
List<String> stringList = new ArrayList<String>();
Later on your code, you decide that you made a mistake, and a 
LinkedList is what you need.
So you just change the declaration to:
List<String> stringList = new LinkedList<String>();

When will you use an interface? why not use an abstract class?

The following code is the wrong way to achieve our task. it is just to 
show why an abstract class is the wrong choice for what we want to do.
public abstract class Washable {
public Washable(){
}
public abstract void wash(){;
}
now, washing a car is different than washing a bicycle, and is 
definitely different than washing a rollerblades. right?
also, washing colored clothes is different than washing white clothes.
so, what will you do if you want to create a car class?
public class car extends vehicle, extends Washable { //cant extend both!
}

What you want to do in such a case is declare Washable as an interface.
public interface Washable {
void wash();
}

=======================================

Use Abstract When ? =>
*If various implementation are of the same kind and use common 
behaviour or status Then better to use abstract .
* If you plan on updating the base class throughout the life of 
program.It is best to allow that base class to be an abstract class. 
why ? => Because you can make a change to it and all the inheriting 
classes will now have this new functionality.

Use Interface When ?
* Interface should be used when we need to define common behaviour 
which can have different variations of implementation.
* You see that something in your design will change frequently . then 
use interface.

-- 
Bst Rgds,
Shakir.
"Knowledge grows when it is shared"
www.jtechnova.com
http://jtechnovablog.blogspot.in/

-----
You have been invited to join JavaTechZone Yahoo Group.
https://groups.yahoo.com/neo/groups/javatechzone/

Pls check our blogs:
http://jtechnovablog.blogspot.in/2014/02/most-frequently-asked-java-interview.html
http://jtechnovablog.blogspot.in/2012/09/difference-between-save-and.html

Kindly visit www.jtechnova.com for More Information

No comments:

Post a Comment