I assume that you already have knowledge on class, object and inheritance. If not, then i strongly suggest you can go to: http://java.sun.com/docs/books/tutorial/java/concepts/ and read this thoroughly.
What is Abstract Class: An abstract class is a class that is declared
abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed.An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:
abstract void box(double X, double Y);If a class includes abstract methods, the class itself must be declared abstract, as in: public abstract class GraphicObject {
// declare fields
// declare non-abstract methods
abstract void draw();
}When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract.In short, These classes are either partially implemented, or not at all implemented.
- This class can contain declarations and also implementations.
- We cannot make instance of Abstract class. We must inherit to use this class.
- A non-abstract class derived from an Abstract class must include implementations for all inherited abstract methods.
- Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).
public interface OperateCar {
// constant declarations, if any
// method signatures
int turn(Direction direction, // An enum with values RIGHT, LEFT
double radius, double startSpeed, double endSpeed);
int changeLanes(Direction direction, double startSpeed, double endSpeed);
int signalTurn(Direction direction, boolean signalOn);
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
......
// more method signatures
}Note that the method signatures have no braces and are terminated with a semicolon.In short, Interface is a pure abstract entity:
- It contains only the declaration.
- We cannot make instance of an Interface. We must inherit to use Interface.
- A class which implements the interface should implement all the members of an Interface.
- Members of an Interface are always
public.
An advantage of Abstract class over Interface
If you want to have additional functionalities for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.An advantage of Interface over Abstract class
A class can inherit multiple interfaces but can inherit only one class.Now let us discuss the most important part - when to use Abstract and when to use Interface.
When to Use Abstract and When to Use Interface
Let me explain it with reference to the scenario:Let us take a classification called Human. As it is a very general classification, I can put all the Human related common data in Abstract class. These common data could be First Name, Last Name, Gender, Education, Occupation, Residence, etc. with some functions as virtual (or overridable), e.g., an occupation which differs from human to human.
Aim of Interface: We want the implementing object to acquire this ability.
Aim of Abstraction: We want the derived object to belong to some group. Abstraction gives the Identity to the derived object.
The sentence "Object A is a Human" is more meaningful than the sentence "Object A is an object which has the ability to walk." (Human can walk, an animal, a bird, a machine - consider it is a robot ... that can also walk.)
Let me explain this with the help of a bit coding in:
abstract class AbstractHuman
{
public abstract string name { get; set;}
public abstract string qualification { get; set;}
//Remaining code goes here }
//Interfaces
interface IEat
{
void eat();
}
interface ISing
{
void sing();
}
interface IWalk
{
void walk();
}
interface IDance
{
void dance();
}
//Class which inherits abstract class and implements interfaces
class MyClass : AbstractHuman, IEat, ISing, IWalk, IDance
{
private string strName = "Sabbir Ahmed";
private string strQualification = "";
public override string name
{
get { return strName; }
set { name = value; }
}
public override string qualification
{
get { return strQualification; }
set { name = value; }
}
public void eat()
{
//Related code }
public void sing()
{
//Related code }
public void walk()
{
//Related code }
public void dance()
{
//Related code }
//The code snippet below checks by what type class could be //accessed as.
public void getAbilities()
{
if (this is IEat)
Console.WriteLine("MyClass can eat");
if (this is IWalk)
Console.WriteLine("MyClass can walk");
if (this is ISing)
Console.WriteLine("MyClass can sing");
if (this is IDance)
Console.WriteLine("MyClass can dance");
}
}
//Test Myclass
class testClass
{
MyClass objMyClass = new MyClass();
public void test()
{
objMyClass.getAbilities();
}
}Output:
MyClass can eat
MyClass can walk
MyClass can sing
MyClass can dance
Technically speaking, here the class is used as more than one type. It is used as four Interface Types. It can also be used as its own type and also the type of its base class.
I hope this will help you to understand the abstract class and interface. You can get more in depth about these two in the http://java.sun.com website.
No comments:
Post a Comment