Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mc3712

[java] Confusing OOP

This topic is 5832 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''ve been studying OOP myself for sometime now, but I haven''t actually done almost anything, just read things. Now I started making one bigger tile-based game (with Java), and I have some questions of the structures. I thought to make something like this: (Classes inside the () are abstract) Tile (GameObject) <-- (Item) <-- SmallBomb <-- Dynamite <-- (Creature) <-- Player <-- (AIMonster) <-- Alien <-- GrenadeMonster <-- FurryMan <-- Slime The first question I had in mind was this: I thought it would be good that Item-class had Item(Player player)-constructor, but then the Player-class would get it too, and that doesn''t sound very reasonable. Why can''t any derived class have it''s own constructors? (I mean, if I would make Item(Player player)-constructor for Item-class, then I''d have to make it to GameObject too, or the compiler notes me about it). I''m still soo confused with all this. Anyone have any ideas about any of above? Any good links to some OOP & games tutorials? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hmm.. it didn''t accept the black spaces for some reason.

Tile
GameObject
.........<-- Item
................<-- SmallShell
................<-- LargeShell
.........<-- Creature
................<-- Player
................<-- AIMonster
........................<-- Alien
........................<-- GrenadeMonster
........................<-- Slime
........................<-- FurryMan

Share this post


Link to post
Share on other sites
quote:

I thought it would be good that Item-class had Item(Player player)-constructor, but then the Player-class would get it too, and that doesn''t sound very reasonable.


What exactly do you mean here? Making class Item have the constructor Item(Player player) isn''t going to affect the Player class in anyway. They''re on a different branch of the inheritance tree.

quote:

Why can''t any derived class have it''s own constructors?
(I mean, if I would make Item(Player player)-constructor for Item-class, then I''d have to make it to GameObject too,
or the compiler notes me about it).


No, you don''t. Changes you make in the subclass (i.e. Item) wouldn''t affect the base class (i.e. GameObject). If you''re getting warnings, it could be that you''re doing something wrong. Perhaps if you posted some code, we could help.

Share this post


Link to post
Share on other sites
I know what you're talking about.

Firstly, a small piece about constructors and derived classes.
If you don't create your own constructor, the compiler will create a no-argument constructor for you. If you do create your own constructor(doesn't matter how many arguments there are or what they are), then the compiler will no longer create a default no-argument constructor for you.

In constructors, the very first statement* has to be a call to the constructor of the parent class. If you don't put one there, the compiler will put a call to the no-arguments parent constructor for you. This call incidentally has the form super(....).

What is happening in your case is you are creating a constructor for Item with one argument, this means that there will no longer be a no-argument constructor due to the first point above. You then try to compile SmallShell for example, because you haven't put a super(...) call in as the first statement in the SmallShell constructor, the compiler will put in a super() call for you-a call to the no-arguments parent constructor, which doesn't exist, hence it won't compile.

So to do what you want, do something like:

public class Item extends GameObject {
public Item(Player p){
//Do what you will
}
}

and either,

public class SmallShell extends Item {
public SmallShell(Player p) {
super(p);
//Do what you will
}
}

or

public class SmallShell extends Item {
public SmallShell() {
super(null);
//Do what you will.
}
}

or put your own no-argument constructor in Item

*-This isn't true, it can also be a call to a different constructor in the same class, but I didn't want to confuse the issue at hand

Jiim Jonez
--
www.javage.net

[edited by - Jiim on November 2, 2002 10:43:59 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!