pradeep cs
pradeep cs

Reputation: 523

read child class variables in parent class

I am trying to access child class variables in parent class..can u suggest me how to proceed based on below code snippet?

public abstract class Base{

//some abstract methods
//one more method to parse the xml
    public final void parseXml(){
        String clName = Thread.currentThread().getStackTrace()[1].getClassName(); //child class name
        if(xmlFile_+clName){ //i am trying to access "Test.xmlFile_Test",
          //execute the if string is available
        }

    }
}
public class Test extends Base{
    public static final String xmlFile_Test = "<Hello>sample</Hello>";
    public int execute(){
        parseXml(); //This should call  base class method
    }
}

Where is my wrong step.. this is psuedo code, which might help you to answer

Upvotes: 0

Views: 1538

Answers (1)

asenovm
asenovm

Reputation: 6517

Create a method called getXMLFile() in the Base class and all its subclasses

public class Base{
    protected String getXMLFile(){
        return "BaseXML";
    }

    public void foo(){
        if(getXMLFile() ....){
            ...
        }
    }
}

public class Test{
    @Override
    protected String getXMLFile(){
        return "TestXML";
    }
}

Upvotes: 2

Related Questions