I wish to validate my understanding of when/why to use a Abstract or Interface.
My Example is related to humans. A Human can be a Man or a Woman. A human can have different profession in life. So this how I use them:
I will declare the professions as Interface, because it will establish a contract of what a human can do in that profession. Example:
Interface SoftwareEngineer{
code();
}
Interface TruckDriver{
driveTruck();
}
Interface Pilot{
flyPlane();
}
And I will declare Man and Woman as abstract class- because man and woman will who the person is.
Abstract Man{
}
Abstract Woman{
}
The class use to define a person can implement the profession interface to define what the person can do and the person will extend abstract class to define who he/she is.
Class Mark extends Man, Implements SoftwareEngineer{
code(){
}
}
This how I would explain some one about interface and abstract difference from my understanding. But I am wondering how to answer below two questions:
You can not instantiate an abstract class , then if you make man and woman as abstract then how can you instantiate these class. How can it be of any use then?
Why did you make Man and Woman abstract, why can't you just make them as an interface. Class will implement them instead of extending.
These are the questions I ask myself. I might be missing something here. Appreciate the insights in this example.