Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java get property only exist in child class

Tags:

java

class Parent {

}

class Child extends parent {

 String something;
//get and set
}


Parent parent = getChildObject();

//without casing getChildObject() to Child object. i cannot do child.getSomething(); Can i do something like parent.getProperty("something"); to get the value?

like image 671
cometta Avatar asked Feb 15 '26 08:02

cometta


1 Answers

The correct way to do this is:

if (parent instanceof Child) {
    String something = ((Child) parent).getSomething();
}

You can also do this by using reflection, but that is a lot more complicated.

like image 109
nfechner Avatar answered Feb 17 '26 22:02

nfechner