4

I am practicing inheritance.

I have two similar classes that I'd like to assimilate into one array, so I thought to use the Object class as a superclass since everything is a sublcass of Object.

So, for example I put T class and CT class into an array called all like so:

 Object all[] = new Object[6];

    all[0] = T1;

    all[1] = CT2;

    all[2] =T3;

    all[3] = CT1;

    all[4] = T2;

    all[5] = CT3;

I skipped the declarations as thats not my problem.

My real issue becomes when I wish to call a function within the array utilizing a loop:

for (int i = 0; i < 6; i++) {

    all[i].beingShot(randomNum, randomNum, AK47.getAccuracy());
}

The classes involved with T and CT respectively both have the beingShot method, which is public.

Eclipse advises casting them as a quick fix. I'm wondering if there is any logical alternative other than creating my own Object class that holds the beingShot method, or adding this to the class of Object, although I feel either of these choices would cause more problems in the long run.

Thanks!

jahroy
  • 22,322
  • 9
  • 59
  • 108
ZAX
  • 968
  • 3
  • 21
  • 49
  • `Object` class doesn't have a `beingShot` method. – Luiggi Mendoza Oct 02 '12 at 04:28
  • @LuiggiMendoza Yes, I recognize that in my question. Is there a way to add it into the existing object class? is that the smartest option? is casting a logical option that will work? etc – ZAX Oct 02 '12 at 04:29
  • 1
    It will be better to have an array of a class or interface that has the `beingShot` method, not plain `Object`s and type-casting. You could accidentally add an item that you don't want like a `String` or another. – Luiggi Mendoza Oct 02 '12 at 04:31
  • You shouldn't be using Objects here if you want to be able to call `beingShot` on each item in the array. You should instead create an interface named `Shootable` and decalare an array of `Shootable`s. See my answer below... – jahroy Oct 02 '12 at 04:35

4 Answers4

13

If both classes implement the same method(s), you should consider creating an interface.

Interfaces are very powerful and easy to use.

You could call your interface Shootable.

You can create an array of different objects that implement Shootable and treat them all the same.

// Define a VERY simple interface with one method.

interface Shootable {
    public void beingShot();
}

// Any class that implements this interface can be treated interchangeably

class Revolver implements Shootable {
    public void beingShot() {
        System.out.println("Revolver: firing 1 round");
}

class MachineGun implements Shootable {
    public void beingShot() {
        System.out.println("Machine Gun: firing 50 rounds");
    }
}

class HockeyPuck implements Shootable {
    public void beingShot() {
        System.out.println("Hockey Puck: 80 MPH slapshot");
    }
}

class RayBourquePuck implements Shootable {
    public void beingShot() {
        System.out.println("Hockey Puck: 110 MPH slapshot");
    }
}

class OunceOfWhiskey implements Shootable {
    public void beingShot() {
        System.out.println("Whiskey Shot: 1 oz down the hatch...");
    }
}

// You can declare an array of objects that implement Shootable

Shootable[] shooters = new Shootable[4];

// You can store any Shootable object in your array:

shooters[0] = new MachineGun();
shooters[1] = new Revolver();
shooters[2] = new HockeyPuck();
shooters[3] = new OunceOfWhiskey();

// A Shootable object can reference any item from the array

Shootable anyShootableItem;

// The same object can to refer to a MachineGun OR a HockeyPuck

anyShootableItem = shooters[0];
anyShootableItem.beingShot();

anyShootableItem = shooters[2];
anyShootableItem.beingShot();

// You can call beingShot on any item from the array without casting

shooters[0].beingShot();
shooters[1].beingShot();

// Let's shoot each object for fun:

for (Shootable s : shooters) {
    s.beingShot();
}

Here's a great related question and answer.

Community
  • 1
  • 1
jahroy
  • 22,322
  • 9
  • 59
  • 108
  • 1
    80mph slapshot? I know the NHL is locked out and all that, but I bet even a decent AHL'er could do better than that! – yshavit Oct 02 '12 at 06:32
1

Object doesn't have the method beingShot. If all of the objects in array are of the same class, then your array should be of that same class. Otherwise they all should have same interface implemented or extend the same class. I can't imagine why would you want explicitly extend Object here, it doesn't add any functionality whatsoever.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • They're not of the same class. CT and T are of two different classes, my apologies if that wasn't clear. That is why I'm using the Object array. And I'm trying to get it to recognize the beingShot method – ZAX Oct 02 '12 at 04:31
  • So here is your inheritance exercise. Create a class from which those other classes you have inherit the method beingShot. – sashkello Oct 02 '12 at 04:33
  • Other option would be, as I mentioned, to have an interface which those two classes implement. But that's not what you are trying to exercise in... – sashkello Oct 02 '12 at 04:35
1

You need to typecast your object references to appropriate class to call their method..

For each reference you fetch from your array, you need to check using instanceof operator, of which is the instance referred to by your object reference.. Accordingly you can typecast the reference to that class..

But Typecasting is an ugly thing.. You should avoid it as far as possible.. If you have to choose which method to invoke based on exact sub class, you should probably go with an Interface.. It is the best way you can achieve what you want here...

And I think you have got enough information about how to implement it..

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • It would be a far better solution to either use generics (to avoid casting) or to create an interface `Foo` and decalre an array of objects that implement `Foo`. – jahroy Oct 02 '12 at 04:36
  • @jahroy.. Yeah that is true.. I just pointed out where he was wrong.. But later I saw, he's got betters answers already.. – Rohit Jain Oct 02 '12 at 04:38
0

You cant do it...since Java does not support extension method. (C# does)

READ THE LINK BELOW:

Java equivalent to C# extension methods

Community
  • 1
  • 1
Vin.X
  • 4,759
  • 3
  • 28
  • 35