PlatypusVenom
PlatypusVenom

Reputation: 557

Access the same method among different classes?

I am programming a simple platformer game, and I have several types of platforms. I created a class for the most simple type and made the rest subclasses, with the only difference between each class being the value of their variables (so they all share the same variable and method names).

In my collision detection, I loop through a HashMap. Within that HashMap are ArrayLists of the instances of each class. But when I use a nested loop to loop through try to call their methods implicitly, I have found that I cannot access these methods without explicitly declaring which class I want to call the method from.

I have done research, although the only way I can see of doing this is to loop through the instances of each class separately, meaning one loop per class; I would rather not do this, since it would be a lot more code than I feel is necessary.

Upvotes: 2

Views: 4315

Answers (3)

Bifz
Bifz

Reputation: 403

Are your subclasses overriding the common methods from the super class? In other words, are your subclass' common methods declared in your simpler class? If it is the case, you can simply call the method as if it is a simple class:

public abstract class Fruit {
    public abstract void method();
}

public class Apple extends Fruit {
    @Override
    public void method() {
        System.out.println("I'm an apple");
    }
}

public class Orange extends Fruit {
    @Override
    public void method()
        System.out.println("I'm an orange");
    }
}

Using this you can simply call your method from any fruit, since it has your method declared. No need to know which fruit it is. The following code:

Fruit fruit = new Orange();
fruit.method();

will output: "I'm an orange".

Upvotes: 2

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726599

In order to be able to call a common method on classes of different types you need to give your objects a common supertype declaring the common method - i.e. they should have a common superclass, or implement a common interface.

Interfaces provide an easier way of declaring common functionality, because a class can implement multiple interfaces, but it can extend only one class.

Provide an interface with the common method, then declare the map to use objects of that interface, i.e.

interface CommonInterface {
    void commonMethod(int arg);
}

class One implements CommonInterface {
    public void commonMethod(int arg) {
        ...
    }
}

class Two implements CommonInterface {
    public void commonMethod(int arg) {
        ...
    }
}

Here is what you can do now:

Map<String,CommonInterface> myMap = new HashMap<>();
myMap.put("one", new One());
myMap.put("two", new Two());
for (Map.Entry<String,CommonInterface> e : myMap.entrySet()) {
    System.out.println(e.getKey());
    CommonInterface c = e.getValue();
    c.commonMethod(123);
}

Upvotes: 2

pop1040
pop1040

Reputation: 57

Simple, make each platform class implement an IPlatform interface or extand a base class. Look up java polymorphism and interfaces.

Upvotes: 2

Related Questions