0

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:

  1. 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?

  2. 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.

Force.comBat
  • 235
  • 2
  • 3
  • 11
  • 3
    Why is `Man` an abstract class? `Man` and `Woman` should be concrete classes. `Mark` should not be a *class*; rather `Human` should have a `name` field and an *instance* of `Man` should have a name `"Mark"`. – Bohemian Sep 12 '17 at 05:18
  • Thanks... I was trying to see if I can use any abstract class in my example so I used Man. But it seems it does not make sense as you explained. Is there any abstract class I can introduce in my example that would make sense to explain someone? – Force.comBat Sep 12 '17 at 05:23
  • I feel like your declaration is wrong . Let's take a real world scenario you can't say `Man` and `Woman` are uncompleted thing just like `human`.You can have `Human` as abstract man and woman can be extends – soorapadman Sep 12 '17 at 05:23
  • This link can give you some idea:https://stackoverflow.com/questions/16781329/when-do-i-have-to-use-interfaces-instead-of-abstract-classes – soorapadman Sep 12 '17 at 05:27
  • You mean Humans can be abstract class and Man and Woman can be concrete class? – Force.comBat Sep 12 '17 at 05:29
  • Thanks , my take is, You should abstract class when you have a common functionality to share across different classes. If your abstract class can have only abstract methods then it makes sense to make it as Interface. – Force.comBat Sep 12 '17 at 05:48

3 Answers3

1

An interface is a contract with the outside world, your best face forward. In a way, it also allows you to implement multiple inheritance in Java.

An abstract class is there to provide a common set of functionalities which can be shared by all its subclasses, e.g., attributes, fields, etc.

Now, let's go over the examples you have provided in your posting.

  1. Man and Woman didn't need to be abstract classes if an instance of a Man or Woman can exist without a profession. Thus, they can be concrete classes.
  2. You also don't want Man or Woman to be an interface. This will avoid a class to implement both Man and Woman interfaces.
  3. By being abstract classes, Man and Woman classes can provide a common set of functionalities.
  4. A class named Mark is not a suitable name for a concrete class which extends Man and implements SoftwareEngineer. May be ManSoftwareEngineer is a better name (I know it's subjective). However, Mark is a good name for an instance of ManSoftwareEngineer
Indra Basak
  • 7,124
  • 1
  • 26
  • 45
1

I would like to quote a comment from this link : What's the difference between an abstract class and an interface?

classes define what something is, interfaces define what something can do

Keeping this concept in mind, Man and Woman define what person is all about. Whereas a Profession is just an characteristic of a person, neutral towards gender. That's the reason to have it as an interface.

Now to your questions :

1. 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 ?

These abstract classes will be helpful if you're creating a new Man, say Sam. Sam is going to have different behaviors. Therefore by having an abstract class 'Man' you're able to add more details to Sam, while keeping base minimum details required for a 'Man'. Infact, by definition of Abstract classes, you might have a default functionality implemented for some of it's (Man) functions, which ofcourse you can override for the derived class (Sam). Note, Interfaces cannot have any functionality at all, all they define is just structure. And i suggest you to think of Human as an abstract class.

2. 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 ?

Simply because a Man or Woman is not just an characteristic, but it will define much of the composition in the derived classes. Secondly as interfaces are particularly focused on a characteristics, languages allow multiple inheritance from the interfaces. Whereas a person cannot be Man and Woman all at same time.

Summing it up, you define an Abstract class whenever your intent is to largely cover all aspects of the derived class. And interfaces for just characteristics that might be shared by many different types. For e.g IRead interface for all those objects that can read. Now reading is not only applicable for Man or Woman, it could extend to gadgets as well.

Praveen Rai
  • 777
  • 1
  • 7
  • 28
0

This case will typically come under Startegy design pattern. (with Suggestion of having common abstract base class for abstract sub classes Man/ Woman). Never use any concrete base class.

In your example SoftwareEngineer (name of class can not be Mark as it can be object of that class) can never become Professor at run time. In reality Mark can become, right?

Strategy pattern with assumption of one profession at a time (for multiple professions simultaneously we will need to modify pattern Impl) will use Human class (M/W) as a context and Professor/SoftwareEngineer as concrete Strategies in general. Problem does not end here, it will generate new challenges about different earnings due to different professions as it will ask for state change. But problem can be solved by using Strategy as a basic pattern.

Kedar Tokekar
  • 418
  • 3
  • 10