Say you had these two classes:
public class Adult { public void crawl() {...} public void walk() {...} public void run() {...} } public class Baby { public void crawl() {...} }
And pretend that crawl(), walk(), and run() are different enough to deem their separation into their own methods appropriate. Also, assume that crawl() in both Adult and Baby share the same implementation. What is the most appropriate way to make a relationship between these two classes? I have come up with a couple option below:
1. Adult extends Baby. This seems appropriate until you explain the relationship in English: "An Adult "is-a" Baby". Then it doesn't make sense.
public class Baby { public void crawl() {...} } public class Adult extends Baby { public void walk() {...} public void run() {...} }
2. Adult and Baby extend an abstract class Human. However, in this case, there's no real difference in implementation of crawl() - I am just creating the superclass Human purely for the sake of the relationship making 'sense'.
public abstract class Human { public abstract void crawl(); } public class Adult extends Human { @Override public void crawl() {...} public void walk() {...} public void run() {...} } public class Baby extends Human { @Override public void crawl() {...} }
I feel like I am doing something wrong either way. What is the best way to code this relationship. If it helps at all, this relates to a real coding problem I am having in a game. I have an NPC and playable Player class. Player has all the behaviors and attributes of an NPC + more, but it just doesn't seem 'logical' to extend NPC and call my Player class an NPC.
Thanks in advance.