Abstract Class means Blueprint of the model.
Interface means functionality of the model(Blueprint) if the class implements one(an interface).
Using Abstract class we can take n number of instances. Like using the Blueprint of a Car
we can take n number of similar cars. But we cannot use the Bluprint or model to drive the
car.That is the meaning of abstract. So, Car is the Blueprint and Ford Car is the derived class from car which uses all the
properties of class and some more properties which is specific to Ford.
Frog is a derived class from Mammal.
When it lives on land it implements the function of hopping.
when it lives on water it implements the function of swimming.
So we find that frog uses both methods hopping and swimming. So it is better to declare
in some interface so that any frog which implements the interface will define its own
way of transportation.
So let me create an Interface called Amphibian and give two method Swimming() and hopping().
It looks like
public interface Amphibian{
public void swim();
public void hop();
}
Let we define the Frog class as
public class Frog implements Amphibian{
public void swim(){
//Define your words
}
public void hop(){
//Define your words
}
}
The above class Frog when implements the interface Amphibian it has to define the methods
declared in the interface Amphibian. It can be an empty method. But it should be defined.
The use of an interface is any class which wants to use some remote functionalities can
implement that particular interface and can perform the task like man using Parachute to
fly in air.
- Compiled by Dharmendra