Abstract Class | Interface |
---|---|
A method must be declared as abstract. Abstract methods doesn't have any implementation | All methods, by default are abstract methods only. So, one cannot declare variables or concrete methods in interfaces |
Abstract methods can be declared with access modifiers like public, private, protected. When implementing in subclass these methods must be defined with the same visibility | All methods in an interface must be declared public |
Abstract class can contain variables and concrete methods | Interface cannot contain variables and concrete methods, except constants |
A class can inherit only one abstract class ( multiple inheritance is not possible ) | A class can implement, many interfaces ( multiple inheritance is possible ) |
abstract class MotorVehicle
{
int fuel;
int getFuel()
{
return this.fuel;
}
abstract void run();
}
class Car extends MotorVehicle
{
void run()
{
print("Vroom");
}
}
interface MotorVehicle
{
int getFuel();
void run();
}
class Car implements MotorVehicle
{
int fuel;
void run()
{
print("Vroom");
}
int getFuel()
{
return this.fuel;
}
}