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 10 of 10

Thread: Objects

  1. #1
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Objects

    Hypothetically speaking if i had multiple objects all being drinks e.g. coke, fanta and sprite. I then wanted a new object that is a drink, but is a combination of coke, fanta and sprite ( in a new class ). How could i do this ? would this be possible ?


  2. #2
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Objects

    Thanks a lot. Do you mind if i send you a pm with my current code, because the structure is extremely distinctive; and it's quite difficult trying to follow this with my structure. Since it's a project, i'm unable to post my code online: ( . If not, then thanks anyway, much appreciated, i'll just try get it working utilizing your advice.

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Objects

    Edit
    I messed up and deleted my message.. Here is what I wrote one post up.


    Absolutely, this kind of thing is fundamental Object Orientated programming. Polymorphism allows you to define something that represents many types. The basic concept is to define an interface, which we will call 'IDrink'. Then create concrete implementations 'Coke' and 'Sprite'.

    public interface IDrink {
        public void drink();
    }

    public class Sprite implements IDrink {
        public void drink() {
            System.out.println("The thirst quencher.");
        }
    }

    This kind of approach is very flexible because it allows you to define the properties (nouns) and methods (verbs, doing words) of a entire category of objects. Additionally it allows you to treat all of these objects in the same manner. We could easily create a 'VendingMachine' class with an array of 'IDrinks' that dispenses drinks to a 'ThirstyConsumer' who will no doubt enjoy the beverage.

    Adding or changing drinks has been separated from the users by a contract so it's exceptionally easy to add or modify drinks.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Objects

    Are you talking about a drink that is a mix of multiple other drinks? In that case you could have some kind of data structure that holds the composite drinks in a new class that extends Drink.

    But do all of these drinks have a different behavior? Or do they just have different names and attributes? In the latter is the case, then you might want to favor composition over inheritance here and just have a single Drink class that you instantiate multiple times with different attributes.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Objects

    Quote Originally Posted by maths94 View Post
    Thanks a lot. Do you mind if i send you a pm with my current code, because the structure is extremely distinctive; and it's quite difficult trying to follow this with my structure. Since it's a project, i'm unable to post my code online: ( . If not, then thanks anyway, much appreciated, i'll just try get it working utilizing your advice.
    Design questions are very hard to answer because if they are made incorrectly you either need to refactor or re-write. I simply do not have the time to review an existing code base for design flaws and those that can won't (shouldn't) do it for free. Without an SSCCE we cannot advise you on what is wrong with your code only that is seems like Polymorphism (many forms) seems like a good solution to your generalized problem.

  6. #6
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Objects

    Quote Originally Posted by ChristopherLowe View Post
    Design questions are very hard to answer because if they are made incorrectly you either need to refactor or re-write. I simply do not have the time to review an existing code base for design flaws and those that can won't (shouldn't) do it for free. Without an SSCCE we cannot advise you on what is wrong with your code only that is seems like Polymorphism (many forms) seems like a good solution to your generalized problem.
    Someone suggested a hashMap, could this work?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Objects

    is a combination of coke, fanta and sprite
    What drink is a combination of those 3 drinks?
    Is that a completely new drink with those 3 flavors mixed together
    or is there some kind of dispenser that allows a user to get any one of those 3 drinks?
    Before writing any code (HashMap) you need to decide on what the new class is.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Objects

    Or separate the "Drink" (or Beverage?) from the contents. The parent class for the contents may be may be Soda with child classes Coke, Pepsi, Sprite (never in the same establishment), Root Beer, SevenUp, MountainDew, etc. Ultimately, the customer is buying a Drink or Beverage that could be a certain sized cup filled with a single flavor or multiple flavors. It's the customer's Drink or Beverage the specifies what's in it.

  9. #9
    Member
    Join Date
    Nov 2013
    Posts
    51
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Objects

    Quote Originally Posted by Norm View Post
    What drink is a combination of those 3 drinks?
    Is that a completely new drink with those 3 flavors mixed together
    or is there some kind of dispenser that allows a user to get any one of those 3 drinks?
    Before writing any code (HashMap) you need to decide on what the new class is.
    Basically, it's a pizza, Pre-defined Pizza e.g. margarita so i've got topping objects, base ect but i need to somehow combine them to make a 'predefined' pizza

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Objects

    Pizza? I thought we were talking about sodas. Don't talk in riddles.

Similar Threads

  1. Need help on objects
    By alex067 in forum Java Theory & Questions
    Replies: 4
    Last Post: September 10th, 2012, 06:23 PM
  2. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  3. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 03:41 AM
  4. New to Objects...
    By Java Neil in forum What's Wrong With My Code?
    Replies: 17
    Last Post: March 24th, 2011, 07:00 AM
  5. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM