Reputation: 1
I have a below abstract class Critter,
/* Critter.java */
/**
* The abstract class Critter defines a base class for anything(which can be empty)
* that can exist at a specific location in the ocean.
* @author mohet01
*
*/
public abstract class Critter {
/**
* Below data member defines a location of a Critter in an Ocean
*/
Point location;
public Critter(int x, int y){
location = new Point(x,y);
}
public Point getLocation(){
return location;
}
/**
* This method computes the new value of location(which can be EMPTY) property of Critter.
* No operation is performed as this is a base class.
*/
public abstract Critter update(Ocean currentTimeStepSea);
}
which currently 3 subclasses inherit namely Shark class, Fish class and Empty class
/* Empty.java */
/**
* The Empty class defines itself as an entity as it has some meaning/significance
* being empty in an Ocean. Check update() method for more meaning.
* @author mohet01
*
*/
public class Empty extends Critter{ ...}
/* Shark.java */
/**
* The Shark class defines behavior of a Shark in an Ocean.
* @author mohet01
*
*/
public class Shark extends Critter{ }
/* Fish.java */
/**
* The Fish class defines the behavior of a Fish in an Ocean
* @author mohet01
*
*/
public class Fish extends Critter{ }
My question is:
If there is a chance of adding a new behaviour(method) in Critter class based on future coming subclasses of Ocean creatures, Do you consider above design as a flaw?
If yes, How would you suggest me to proceed?
Additional info: Remaining classes that are part of this application(not relevant to current query) are Ocean class, Point class, SimText class, Utility class.
Complete code can be seen(if required) at query section in link
Upvotes: 2
Views: 96
Reputation: 17363
Interfaces define common behaviour, abstract classes provide common implementations. Hence, simply create a Locatable interface that your critters implement:
public interface Locatable {
Point getLocation();
}
When you have new behaviours, just create new interfaces to represent them that your critters implement:
public class Fish implements Locatable, Prey {}
public class Shark implements Locatable, Predator {}
public interface Predator {
void eat(Prey prey);
}
public interface Prey {
void hideFrom(Predator predator);
}
Upvotes: 1