Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


java Constructor parameters


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 23 June 2014 - 07:31 PM

I am trying to create a text adventure in java. This is my first large program so, i may be going about it the wrong way. I planed to create a super class for generic items that parents sub classes for different types of items like health items and equipable items.

 

I have a basic class called item that has id, name, description, and weight variables. I have sub classes that add other attributes. do i need to include the super class's variables in the parameter for the subclass constructor? if not how would i set the super class's variables when I instantiate the subclass?



Sponsor:

#2 Glass_Knife   Moderators   -  Reputation: 4838

Like
3Likes
Like

Posted 23 June 2014 - 07:56 PM

public class A {
   protected int x;
   public A(int x) { this.x = x; }
}

public class B extends A {
   private int y;
   public B(int x, int y) {
      super(x);
      this.y = y;
   }
}

If the base class doesn't have an empty constructor, then all the need items need to be given to the constructor when the object is created.

 

Having said that, you may be trying to create an object structure when you don't need one.  You should favor composing items instead of inheriting.  Large, deep inheritance trees, while neat from a computer science aspect, can become hard to manage.

public class A {
   private int x;
   public A(int x) {
      this.x = x;
   }
}

public class B {
   private int y;
   public B(int y) {
      this.y = y;
   }
}

public AB {
   private A;
   private B;
   public AB( A a, B b ) {
      this.a = a;
      this.b = b;
   }
}


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#3 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 23 June 2014 - 08:55 PM

@Glass_knife: so, if I continue with my inheritance craziness, I would have to feed a long list of args into the sub-class constructor and feed some of those into a super constructor.

Are you trying to say that instead I should just make different stand alone classes for each item? And, is this due to my level of experience with java or the scope of the project?

#4 SeraphLance   Members   -  Reputation: 1438

Like
6Likes
Like

Posted 23 June 2014 - 11:06 PM

It's extremely common for Java programmers to overuse inheritance, even in the professional world.  This is partly due to how it's taught, and partly due to the culture surrounding the language.  No, I wouldn't make each item its own class.  Remember that a class is a template for creating objects.  Your individual items are objects, so just make them items.  What kind of specialized behavior would you need an item subclass for in the first place?  Consider that for a bit, and think about whether that behavior is really special to a certain set of things.



#5 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 June 2014 - 01:01 AM

@SeraphLance: I guess what I meant was that I could make a class for all items with all possible attributes and make different constructors - picking and choosing which instance vars to use based on the type of item.

#6 stein102   Members   -  Reputation: 485

Like
2Likes
Like

Posted 24 June 2014 - 11:24 AM

I would have to agree with Glass_Knife, you should favor composition over monolithic inheritance trees. Basically what that means is that an in game object(or entity) is made up of mutiple components. So for example if you had a Iron sword, it could have a wieldable component, a sword component, an iron component, it might even have a firey enchantment component.

 

You could code all of this into your game, but I'd suggest making it as data-driven as possible. So perhaps you could read from a text file or an XML file what each item is built of and your game would populate the world with those items. This is just a suggestion though, on smaller scale projects this may be overkill.

 

There's lots of good reading available on the topic of Entity-Component systems. There are a few articles on this site that go over how they work, a simple google search for "Entity-Component systems" should give you quite a bit to chew on for now.

 

Another good resource is the Artemis framework, so that might be something to look into(Although I'd say it's a bit beyond the scope of this project).



#7 Glass_Knife   Moderators   -  Reputation: 4838

Like
2Likes
Like

Posted 24 June 2014 - 11:44 AM


I would have to agree with Glass_Knife, you should favor composition over monolithic inheritance trees. Basically what that means is that an in game object(or entity) is made up of mutiple components. So for example if you had a Iron sword, it could have a wieldable component, a sword component, an iron component, it might even have a firey enchantment component.

 

This is another way to go.  I'm actually not suggesting any design up front.  Just code up what you need doing the simplest thing possible (i.e. no inheritance or composition or anything).  When you find you keep writing the same stuff over and over again, refactor to common code.  This may be a base class with child classes, or it may be objects composed of other objects.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#8 stein102   Members   -  Reputation: 485

Like
0Likes
Like

Posted 24 June 2014 - 12:04 PM

 


I would have to agree with Glass_Knife, you should favor composition over monolithic inheritance trees. Basically what that means is that an in game object(or entity) is made up of mutiple components. So for example if you had a Iron sword, it could have a wieldable component, a sword component, an iron component, it might even have a firey enchantment component.

 

This is another way to go.  I'm actually not suggesting any design up front.  Just code up what you need doing the simplest thing possible (i.e. no inheritance or composition or anything).  When you find you keep writing the same stuff over and over again, refactor to common code.  This may be a base class with child classes, or it may be objects composed of other objects.

 

 

That's probably the best way to go, what I described may very well be overkill for this particular project. You should probably go with the simplest possible approach to get what you need to do done, there's no need to over complicate things.



#9 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 June 2014 - 12:32 PM


 When you find you keep writing the same stuff over and over again, refactor to common code.  This may be a base class with child classes, or it may be objects composed of other objects.

 

that's what i was trying to avoid. I was trying to anticipate what I would need to re write.

 

I looked up "is - a" and "has a" relationships here: 

 

It seems to me that Health item and equipable items could share common attributes so it "is a " type of item.

 

I was also thinking of making a large item class and picking and choosing which values to use in different constructors (not sure if i mentioned this before). Is this what some of you are talking about?



#10 Glass_Knife   Moderators   -  Reputation: 4838

Like
2Likes
Like

Posted 24 June 2014 - 12:49 PM


that's what i was trying to avoid. I was trying to anticipate what I would need to re write.

 

Number of times I have designed an API and had the end result match the design = 0.

 

As an example, let us say you have an Equipable base class, and then a HealthItem which "is a" Equipable item.  Later you come up with different kinds of PowerUps, that aren't equipable but bestow some temporary benefit.  Then you make a MagicBooster that "is a" PowerUp.

 

Months later you try to create a MegaHealthBooster, that is a HealthItem and is a MagicBooster.  But now you have multiple inheritance, which is bad in C++ and not even allowed in many other languages.  To get around this, you'll have to redesign your classes or create some silly hacks that will cause weird bugs down the line.  

 

Just be aware that this will be an issue if you take the "BaseClass / ChildClass" design.

 

If you had game objects that are composed of other objects, then a MHB that contains a HealthItem and a MagicBooster is easier.


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#11 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 June 2014 - 01:50 PM

@Glass_Knife: i did run into problems with that inheritance system i was using. I intended to load the items into a room by a single list. I didn't think this could be done because i had separate classes for HealthItems and Equipable items (by equipable i meant weapons or tools).I don't think i could reference both types of items by just making a list of items.

 

I tried the other way I had proposed. I ran into a problem where both Health item constructor and equipable item constructor had the same type and number of parameters.

 

I'm all out of ideas now :(

suggestions anyone?



#12 Glass_Knife   Moderators   -  Reputation: 4838

Like
0Likes
Like

Posted 24 June 2014 - 02:08 PM


I'm all out of ideas now
suggestions anyone?

 

You fell victim to one of the classic blunders:  Premature optimization:

 

Read this: http://c2.com/cgi/wiki?PrematureOptimization

 

Long story short, you're trying to design an efficient solution to a problem you think you may have in the future.

 

https://www.youtube.com/watch?v=njUOew3dNyk


I think, therefore I am. I think? - "George Carlin"
Indie Game Programming

#13 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 June 2014 - 02:21 PM

I think someone might have suggested this before and i finally understand it. I created an item class with basic attributes for an item and created another class for Health items where i am making an item object ass one of the health item's variables



#14 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 24 June 2014 - 06:52 PM

I ran into another problem where. I created item class with basic attributes for an item and created another class for each type of item that consists of a basic item object. so, in order to keep all the items i needed to store them in a list or array of objects. My problem now is that when i can't reference functions for the objects in the array.



#15 Aldacron   GDNet+   -  Reputation: 3241

Like
3Likes
Like

Posted 24 June 2014 - 08:24 PM

You seem to be fundamentally misunderstanding something. Your specific item types, like Weapon, shouldn't contain an item. That isn't going to help you at all. For a purely componentized approach, your item class should hold a list (or map) of Components. Specific components give the item its properties.

For example, you would have a Component base class and some subclasses might be WeaponComponent and BuffComponent. Your Item class maintains properties common to all items, like durability, weight, isTradeable, or whatever. A WeaponComponent would maintain properties that differentiate weapons from other items, damageAmount, damageType, requiredSkill, and so on. Using this approach, you could define your weapon components in a JSON file (or YAML/XML/Take Your Pick). Swords, maces, and bows become defined purely by data, rather than by concrete classes. Then, when you load in a weapon, you can do something like this:

 

WeaponComponent weapon = new WeaponComponent( /* values from wherever go here */ );
Item item = new Item( /* item values for this particular weapon * / );
item.addComponent( weapon );

// Let's make it magic
BuffComponent buff = new BuffComponent( /* buff values from somewhere */ );
item.addComponent( buff );

itemsList.add( item );

Using this, you can turn any item in the game, such as a chair, into a weapon. You can give any item, whether it's a weapon or not, a magic buff. It gives you a great deal of flexibility. However, you also increase the complexity a bit. For one thing, you'll need a way to tell if an item has a specific component or not. For example, when the player wants to attack an NPC, you'll want to check if the item he selects to attack with actually has a weapon component. How you do this mostly depends on what sort of container you choose to hold an Item's components, but also whether you want the item types to be fixed or want to allow modders to add new items, and so on.

Between the extreme of the purely data-driven component-based approach and the other extreme of the strict class hierarchy lies a middle ground where you can come up with a sort of hybrid system. In other words, there is more than one way to skin this cat. Every approach will come with its pros and cons. For games that are small in scope and that don't need the benefits of a component-based system, the class hierarchy is perfectly fine. Implementing one of these systems is the easy part. The hard part in making yourself aware of all of the options available to you and learning how to choose the approach that fits your needs.


Edited by Aldacron, 24 June 2014 - 08:27 PM.


#16 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 25 June 2014 - 12:28 AM

@Aldacron O-oh! I see, so I was trying to stick the gameObject inside of the component instead of the component into the game object!

 

Thank you to all who have participated in the topic!



#17 Kk1496   Members   -  Reputation: 194

Like
0Likes
Like

Posted 25 June 2014 - 03:26 PM

Ok. so, i tried creating an item class with the basic things I wanted for every item and a list to load the necessary components into. I created a print function to print all of those attributes. Then, i moved on to the first component. I created variables for the things that i wanted to vary in this type of item. However, I don't know how to make the print function work for the components. I am extremely confused about how to make the classes interact in general. How do i reference the item when i'm trying to print out all of it's attributes.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS