I stumbled across that situation but I don\'t know how to handle it the right way:
class Myclass { }
class MyclassWithAwesomeStuff extends Myclass {
public b
Just use an interface:
interface AwesomeStuffable{
public boolean isAwesome();
}
let your classes implement it:
class MyClass implements AwesomeStuffable{
public boolean isAwesome(){
//your logic here
}
}
And let your ArrayList
hold just AwesomeStuffable
objects.
Just to get yourself unblocked, you can do the following:
if (m instanceof MyClassWithAwesomeStuff) {
if (((MyClassWithAwesomeStuff) m).awesomeStuff) {
}
}
But, using instanceof defeats the purpose of inheritance and it appears to be a design flaw to have a need to check for this flag for only some objects in list
in your code. If you expand the context, probably something better can be suggested.
Test if m
is a MyclassWithAwesomeStuff
with the instanceof
operator.
if (m instanceof MyclassWithAwesomeStuff)
{
MyclassWithAwesomeStuff mwas = (MyclassWithAwesomeStuff) m;
// Now you can access "awesomeStuff" with "mwas"
}