Reputation: 1
I have 3 classes with different fields:
Class Cow(){
private integer legs;
private integer eyes;}
Class Fish(){
private integer fins;
private integer eyes;}
Class Pinguin(){
private integer legs;
private integer fins;
private integer eyes;}
I want to make sure that the fields are valid / allowed fields. Is there any design pattern for this ? If I use inheritance from Animal() then the Cow may have fins which I want to avoid. I just want that fins is called fins and not 'FiN' or 'fiins' in the subclass. Any suggestion ?
Upvotes: 0
Views: 71
Reputation: 5082
Create a parent class which will have all the common properties and then extend the parent class in the child class. In this case:
class Animal{
protected int eyes;
}
class Cow extends Aminal{
protected int legs;
}
The Animal
class is the parent class whereas the Cow
is a child class which extends Animal
, and will also contain the data member eyes
.
Also, check the syntax of the statements. Your classes won't compile.
Upvotes: 0
Reputation: 2192
This is basic object oriented design. Just follow the real world as close as possible. Create a base class Animal() with subclasses like Birds(), Mammals(), Insects() and so on. Go further down in that tree.
Upvotes: 2