6 Replies - 368 Views - Last Post: 07 August 2012 - 10:05 AM Rate Topic: -----

#1 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

How to know which subclass?

Posted 07 August 2012 - 08:36 AM

I'm creating shooting game, and I created class Cowboy(abstract class, I will shoot cowboys). And I created 3 subclasses of that abstract class.

All cowboys have different guns, so I want, that cowboy1(who is with shotgun) would make more damage than cowboy2( who have a pistol). In my code, I have Cowboy c variable, for all three types of cowboys. I randomly assign to that 'c' variable different types of cowboys. So I need to know, which sublass I'm using now. I'm talking smth about 'instance of' in another languages. Does java has something similar to this?

Is This A Good Question/Topic? 0
  • +

Replies To: How to know which subclass?

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2117
  • View blog
  • Posts: 3,242
  • Joined: 21-June 11

Re: How to know which subclass?

Posted 07 August 2012 - 08:39 AM

Java has the instanceof, but I don't really see why you'd need it for this use-case.

If you want to have Shoguns do more damage than Cowboys, just let Shogun's damage method return a higher value than that of Cowboy. That's what polymorphism is for.

This post has been edited by sepp2k: 07 August 2012 - 08:40 AM

Was This Post Helpful? 2
  • +
  • -

#3 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: How to know which subclass?

Posted 07 August 2012 - 08:42 AM

why would you need to know? If your subclass methods are the same names as your super class, then the functionality of the sub class method will be used. That's the beauty of inheritance. For example, if you have a shoot() method in Cowboy, then PistolCowboy and RifleCowboy each have a shoot() method that you define using their own damage multiplier, but you still call shoot()

Cowboy c;
c = new PistolCowboy();
c.shoot();

c = new RifleCowboy();
c.shoot();




each shoot is defined differently so you have no reason to know what Cowboy is
Was This Post Helpful? 1
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,110
  • Joined: 19-March 11

Re: How to know which subclass?

Posted 07 August 2012 - 08:42 AM

As a rule, if you find yourself using instanceof, you're doing something wrong. Not always true, but often enough to make you think twice.
Was This Post Helpful? 2
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10568
  • View blog
  • Posts: 39,131
  • Joined: 27-December 08

Re: How to know which subclass?

Posted 07 August 2012 - 08:55 AM

If you really find yourself customizing Weapons enough, I would consider creating an abstract Weapon class. Then have your abstract Character class store a Weapon. The details should then be left to the subclasses.
Was This Post Helpful? 2
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7752
  • View blog
  • Posts: 13,110
  • Joined: 19-March 11

Re: How to know which subclass?

Posted 07 August 2012 - 09:03 AM

Mac's got a good point. (in other news, water was seen to flow downhill earlier today, and a lawyer was found to be lying)
You might want to sketch out your design and your requirements a little. What sorts of things can a user do in this game?

for example, can a cowboy find a weapon and pick it up and use it? Or does a shotgun cowboy only ever get a shotgun?
Was This Post Helpful? 0
  • +
  • -

#7 novakasss  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 352
  • Joined: 11-July 12

Re: How to know which subclass?

Posted 07 August 2012 - 10:05 AM

Hello, I got another problem, so I will put it in this topic. I finished my project. With jar2exe wizard tool, I converted jar to .exe file. When I run game on my computer, everything is ok. But when I sent it to my friend, he said, that he can't open file, error message is "startup main class could not be found". What's wrong with this?

in my program I'm also using sounds, and
import sun.audio.*;
Is this can be the problem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1