There are two ways to achieve abstraction in java
1. Abstract class (0 to 100%)
2. Interface (100%)
Interface and Abstract class both are looks similar from declaration point of view. But an Interface contains only the definition / signature of functionality, and if we have some common functionality as well as common signatures, then we need to use an abstract class.
Example:-
// I say all motor vehicles should look like this:
interface MotorVehicle
{
void run();
int getFuel();
}
// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{
int fuel;
void run()
{
print("Wrroooooooom");
}
int getFuel()
{
return this.fuel;
}
}
***********************************************************************************
// I say all motor vehicles should look like this:
abstract class MotorVehicle
{
int fuel;
// They ALL have fuel, so lets implement this for everybody.
int getFuel()
{
return this.fuel;
}
// That can be very different, force them to provide their
// own implementation.
abstract void run();
}
// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
void run()
{
print("Wrroooooooom");
}
}
So Interface says, "hey, I accept things looking that way".
Abstract class says, "these classes should look like that, and they have that in common"
Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).
Abstract class does not support multiple inheritance. Interface supports multiple inheritance.
Abstract class contains constructors but Interface can't.
Summary
1. An interface defines a contract that some implementation will fulfill for you.
2. An abstract class provides a default behavior that your implementation can reuse.
3. An abstract class is a class that is only partially implemented by the programmer. An interface is a fully abstract class.
4. abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.
1. Abstract class (0 to 100%)
2. Interface (100%)
Interface and Abstract class both are looks similar from declaration point of view. But an Interface contains only the definition / signature of functionality, and if we have some common functionality as well as common signatures, then we need to use an abstract class.
Example:-
// I say all motor vehicles should look like this:
interface MotorVehicle
{
void run();
int getFuel();
}
// My team mate complies and writes vehicle looking that way
class Car implements MotorVehicle
{
int fuel;
void run()
{
print("Wrroooooooom");
}
int getFuel()
{
return this.fuel;
}
}
***********************************************************************************
// I say all motor vehicles should look like this:
abstract class MotorVehicle
{
int fuel;
// They ALL have fuel, so lets implement this for everybody.
int getFuel()
{
return this.fuel;
}
// That can be very different, force them to provide their
// own implementation.
abstract void run();
}
// My teammate complies and writes vehicle looking that way
class Car extends MotorVehicle
{
void run()
{
print("Wrroooooooom");
}
}
So Interface says, "hey, I accept things looking that way".
Abstract class says, "these classes should look like that, and they have that in common"
Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (they are defined public by default).
Abstract class does not support multiple inheritance. Interface supports multiple inheritance.
Abstract class contains constructors but Interface can't.
Summary
1. An interface defines a contract that some implementation will fulfill for you.
2. An abstract class provides a default behavior that your implementation can reuse.
3. An abstract class is a class that is only partially implemented by the programmer. An interface is a fully abstract class.
4. abstract class establishes "is a" relation with concrete classes. interface provides "has a" capability for classes.
No comments:
Post a Comment