Alex Rivera | Logout

Limitations in subclasses OOP

Asked 2013-06-13T06:27:46.580
23

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?

Edit
Report

1 Answer

4

Go for Interfaces.

Define interface for CommonMan which has common properties and use individual interfaces For seperate subClasses which have their specific methods.Then you can get the Both the interface methods.

Example:

public class SpecificMan extends CommonMan implements specifiManInter

{

//methods of CommonMan

//Methods of SpecificMan 

}

CommonMan Class:

public class CommonMan implements CommonManInter {

 //methods of CommonMan

}
answered 2013-06-13T06:42:55.313

Your Answer