I will start this question with an example and then I will tell what I have in my mind.
public class testCommand extends commandBase implements commandTypeA{
//this is from commandTypeA
@Override
public type executable() {
//type is enum
return type.EVERYONE;
}
//this is from commandBase
@Override
public String name() {
return "test";
}
}
public class testCommand2 extends commandBase implements commandTypeB{
//this is from commandTypeB
@Override
public String description() {
return "A testCommand";
}
//this is from commandBase
@Override
public String name() {
return "test";
}
}
In that example I don't want to have description
in commandTypeA
and executable
in commandTypeA
I will create also many classes like testCommand
and testCommand2
. I will always extend commandBase
and will implement commandTypeA
or commandTypeB
.
I want to store those classes into an ArrayList or just a variable (that could store both of the classes - with implemented commandTypeA
or commandTypeB
and all of their methods - including methods from the extended and implemented classes).
Example with ArrayList:
ArrayList<*datatype*> registeredCommands = new ArrayList<*datatype*>;
registeredCommands.add(new testCommand());
registeredCommands.add(new testCommand2());
Example with variable:
*datatype* currentCommand = new testCommand();
currentCommand = new new testCommand2();
Also I need to perform actions later in my code like
Example with ArrayList:
system.out.println((commandBase) registeredCommands.get(0).name());
system.out.println((commandTypeA) registeredCommands.get(0).executable());
system.out.println((commandTypeB) registeredCommands2.get(1).description());
Example with variable:
*datatype* currentCommand = new testCommand();
system.out.println((commandBase) currentCommand.name());
system.out.println((commandTypeA) currentCommand.executable());
currentCommand = new new testCommand2();
system.out.println((commandTypeB) currentCommand.description());
My questions are:
- Is there any type of datatype that i could use in my case?
- Can I do the same thing I have in mind in other (better) ways?