Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitations in subclasses OOP

Tags:

In java, what is a good way to design around a subclass that reduces the functionality of the superclass?

For example, consider the class "Man", that has the function "See" and the subclass "BlindMan" that shouldn't have that function, but should have everything else that "Man" has.

The only solution I can come up with is having an abstract class "Man", and two subclasses "SeeingMan" and "BlindMan", with SeeinMan adding a function "See".

However the problem with this solution is that if I now want to add a "DeafMan" class - what does it extend? SeeingMan? and what if that man is both deaf and blind?

like image 884
indieman Avatar asked Jun 13 '13 06:06

indieman


People also ask

What is the advantage of using subclasses?

Using subclasses has several advantages: Reuse of code: Through inheritance, a subclass can reuse methods that already exist in a superclass. Specialization: In a subclass you can add new methods to handle cases that the superclass does not handle. You can also add new data items that the superclass does not need.

What do subclasses not inherit?

A subclass does not inherit the private members of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass. A nested class has access to all the private members of its enclosing class—both fields and methods.

What are subclasses in OOP?

Subclasses, also referred to as derived classes, heir classes, or child classes, are classes that inherit one or more language entities from another class/classes.

Are there any practical limits on levels of sub classing in Java?

In Java, there is no practical limit on number of subclasses that can be derived from the super class. But, the hierarchy must follow a linear fashion. Therefore, when creating a subclass, instead of declaring all new class members, we designate it to use the members of existing class.


2 Answers

I think you should use Composition rather than inheritance in this case, or have various subclasses that make up the human.

While I do understand your logic, a BaseClass is a contract, that guarantees all classes of that type should adhere to this behavior, having a subclass that removes the parent method is a big NO NO..

While you could throw various exceptions, I wouldn't go down that path at all. Think about it this way, say I am a developer who only needs to access Human object, I expect a certain behavior, and all of a sudden I call an interface method and get..an Exception just because I called it?? You should not be aware of the derived classes implementations and when you can or cano't call them.

Here are a few solutions:

Make Human a composition of BasicHumanFunctions, VisionSystem, etc. Then blind man would have only a few of those.

class Human {   private BasicHumanFunctions _basicFunctions; //contains breathe funcitonality.   private VisionSystem _vision; //contains see }  class BlindMan {    private BasicHumanFunctions _basicFunctions; } 

Make Human base class contain only the same behaviour all humans would like breathing etc, and then create a HealthyHuman and a BlindHuman etc, each creating their own methods. You can then use HealthHuman and subclass that further if you need.

class Human {    void breathe() {};    // other Human basic functions; }   class HealthyHuman extends Human {    void see() {};    //other healthy human functions }  class BlindHuman extends Human {   void useCane() {}; } 

For the second case you can still use composition too to share behaviour:

class BlindHuman extends Human {    private VoiceSubsystem _voice = new VoiceSybsystem();    void speaker() {  _voice.speaker();} } 
like image 73
Dory Zidon Avatar answered Sep 28 '22 23:09

Dory Zidon


The most important principle, is Lyskov's substitution principle.

Once you get it and apply it, you can build your model however you want.

For example, you could say that a blind man still sees, only he only gets black (or grey) pictures. There's also the question of legally blind people you want still see something if a blurred or partial picture.

You could also say that the action the object can do is not see. What we do is to watch. Seeing is the (successful) result of watching. Even with working eyes, you may watch without seeing anything! And blind people can still "see" something without eyes, or with non-functioning eyes, by listening (thus hearing, and building a mental "picture").

So, forget about "see" and "hear". Use watch and listen. And expect nothing from that. (wide pre-conditions).

like image 28
informatimago Avatar answered Sep 28 '22 22:09

informatimago