About classes extended from base classes (Java)
I am a beginner of Java, trying to write a party task system for the game I am writing. I have a few questions I want to answer I've walked around and asked others, but they're not familiar with Java
In the past, I tried to make a bunch of classes and use multiple get methods to access them I find writing very boring and think I can unify them under abstract / implementation classes So the code looks more like
DynastyPQInterface pq = new CustomPQ // or .... DynastyPQInterface pq = new OtherCustomPQ
Of course, this brings difficulties such as using only implementation methods It does not allow me to access exclusive methods of classes I may want to use later
Finally, what I want to do is to be able to use a single get method to return any of these derived classes, but still retain the ability to commonly use get methods to call their common methods, such as execute, create, end, and allow me to specifically contact their exclusive methods Is there a way to do this, or is it impossible?
If not clear
The code I'm writing now is a base class that extends to other classes in this way
DynastyPQ (base) -> methods include (run(),execute(),end()) CustomAPQ (inherited from DynastyPQ) -> (has exclusive methods like getPoints()) CustomBPQ (inherited from DynastyPQ) -> (has exclusive methods like revivePlayer())
I want to write a get method to get rid of multiplicity What I have now is
DynastyPQ dynastyPQ; DynastyPQ getPQ() { return dynastyPQ; } void setPQ(DynastyPQ pq) { dynastyPQ = pq; }
To do so
DynastyPQ pq = new CarnivalPQ();
I can only access dynastypq's method, not Carnival's method
Is there any way to access unique methods and generally be able to execute four basic functions regardless of the type of class, or did I miss anything earlier?
tl; Dr – > I want a get method that generally returns all classes inherited from class X; However, I want to be able to access the exclusive methods of each class
Solution
You can cast an object to a derived class:
DynastyPQ pq = new CustomAPQ(); ((CustomAPQ)pq).customAPQmethod();
If you don't know what the dynamic type is (the type used after the new operator), you can use the instanceof keyword:
DynastyPQ pq = getPQ(); if (pq instanceof CustomAPQ) { CustomAPQ a = (CustomAPQ)pq; a.customAPQmethod(); } else if (pq instanceof CustomBPQ) { CustomBPQ b = (CustomBPQ)pq; b.customBPQmethod(); } else { // Neither a CustomAPQ nor a CustomBPQ. }
If you don't want to do this, you can use polymorphism:
class DynastyPQ { final void run() { // code. } final void execute() { // code. } final void create() { // code. } void specific1() {} void specific2() {} } class CustomAPQ extends DynastyPQ { @Override void specific1() { // do stuff specific to CustomAPQ. } @Override void specific2() { // do stuff specific to CustomAPQ. } } class CustomBPQ extends DynastyPQ { @Override void specific1() { // do stuff specific to CustomBPQ. } @Override void specific2() { // do stuff specific to CustomBPQ. } }
Now you can:
DynastyPQ pq = new CustomAPQ(); pq.specific1();
The called method is customapq:: specific1() If specific1 () is not declared in customapq, it will do nothing