Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How would you design the relationship between these two classes?

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How would you design the relationship between these two classes?

    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.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: How would you design the relationship between these two classes?

    I think your second organization makes more sense; except, I don't think you need to make crawl() abstract. Since it is the same for adult and baby, you can define exactly what crawl() does right in the Human class.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: How would you design the relationship between these two classes?

    Google the phrases "composition versus inheritance" and "composition over inheritance". Each technique has its use in different situations. In your case you should evaluate whether composition may be more appropriate...for instance, every adult may not be baby, but every adult has a kid inside

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would you design the relationship between these two classes?

    @copeg. So you think that the best solution would be to instantiate an object of Baby within Adult to get code re-use? I think your reasoning that there is a child inside us all is creative. IMO, a little bit of a stretch, but creative nonetheless .

    @snowguy. If I were to use the design that you suggested and just have a Human class and an Adult class, how would Baby fit in? Are you saying that I wouldn't even need a Baby class? I guess I could create an object named baby (ie. Human baby = new Human()), but I don't like the idea that Human class models a baby whereas an Adult class models an adult. It would be nice to have a Baby class model a baby too.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,336
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: How would you design the relationship between these two classes?

    So you think that the best solution would be to instantiate an object of Baby within Adult to get code re-use? I think your reasoning that there is a child inside us all is creative. IMO, a little bit of a stretch, but creative nonetheless
    I didn't say best solution, I suggested you look at the idea of composition. In my experience, many situations make much more sense to design away from inheritance for the benefit of re-usability, structure, and maintainability. This isn't to say inheritance isn't powerful or wrong, it is to say you need to think about your design from other standpoints and what you think makes sense from the standpoint of re-usability, structure, and maintainability. And to keep in mind the often cited phrase: Encapsulate what changes. In my view, your post doesn't have enough information to pose what is the 'best' design (and best is relative - you may look at your code months from now and think 'what the heck was I thinking')

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would you design the relationship between these two classes?

    What you said makes sense. Thanks a lot for the help, copeg.

Similar Threads

  1. Relationship of sizes
    By zscipio in forum Java SE APIs
    Replies: 1
    Last Post: January 31st, 2012, 12:18 PM
  2. Is this bad code design?
    By JavaGirl9001 in forum Exceptions
    Replies: 1
    Last Post: November 28th, 2011, 02:25 AM
  3. Help in possible Design UI
    By beni.kurniawan in forum AWT / Java Swing
    Replies: 1
    Last Post: April 15th, 2011, 10:51 AM
  4. Class design
    By arithmetics in forum Object Oriented Programming
    Replies: 4
    Last Post: November 4th, 2010, 08:44 AM
  5. How do you design a complex program
    By mydarkpassenger in forum Java Theory & Questions
    Replies: 5
    Last Post: March 19th, 2010, 06:52 PM