Lets suppose I have Planet and Star classes and GameLogic class in the following way
//Planet.java
package Game;
class Planet //invisible outside of package
{
public String getType() {return "Im a Planet";}
}
.
//Star.java
package Game;
class Star //invisible outside of package
{
public String getType() {return "Im a Star";}
}
.
//GameLogic.java
package Game;
public class GameLogic //notice the PUBLIC keyword
{
//arrayList of stars
//arrayList of planets
//a ton of methods for the logic of the game itself
}
These all belong to the Game package which treats only the whole logic of the game with no input/output.
Then, in a different package called UI I have the Text class (UI.Text) which controls user input, prints output and has direct access to the GameLogic because the game can only be played by creating an instance of the GameLogic class.
//Text.java
package UI;
import Game.*;
public class Text
{
//some control vars
//MAIN method
//input and output methods
//bla bla bla
}
So the issue is: The game will be instantiated in Text.java by creating a GameLogic instance. Given this, if i wanted to output the getType() functions from Star and Planet, I'd have to complete the "road" that connects Text with Planet/Star in the following way
//GameLogic.java
package Game;
public class GameLogic
{
//same as before
public String getPlanetType(int n) {return planetArrayList.get(n).getType();}
public String getStarType(int n) {return starArrayList.get(n).getType();}
}
In short, the getType() methods already exist in a "too-far-away" class, so it forces me to create these simple, yet repetitive, "bridge" methods to connect both ends.
TL-DR version:
class A has getType()
class B has array of A
class C needs to access getType() from array of A in B
I believe this is a very inefficient and time consuming method and that there is a better alternative (possibly involving Interfaces?), so if anyone could provide me with some help, I'd be very grateful.
Thank you