简体   繁体   中英

How to access method in base class from child class in java

How to access parent class's methods from child class? Following is my class implementation:

public interface BaseUrl {
    public String getNameSpace();
    public String getUrl();
}

BaseSoapUrl Class:

public class BaseSoapUrl implements BaseUrl {

    @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
}

SoapURL Interface:

public interface SoapURL {
    public String getSoapAction();
    public String getMethodName();
}

LoginSoap Class:

public class LoginSoap extends BaseSoapUrl implements SoapURL {

   @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

For the sake of simplifying the code i want to implement them like this. earlier it was something like below :

public interface SoapURL {
    public String getNameSpace();
    public String getUrl();
    public String getSoapAction();
    public String getMethodName();
}

LoginSoap Class:

public class LoginSoap implements SoapURL {

   @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
   @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

and i could access these methods like below :

private SoapURL soapURL = new LoginSoap();
String static final url = soapURL.getUrl();

Now the return values form public String getNameSpace(); and public String getUrl(); are going to be same in all child classes ; so why to write code again and again. There are going to be many classes which is going to implements SoapURL interface because of web service is being used.

so my question is how to access methods which is in BaseSoapUrl through soapURL ?

You can re-factor the SoapURL interface to be an abstract class , where you can provide some common implementation of the getNameSpace() and getUrl() methods and leave the others as abstract ones, so that the sub-classes will be forced to provide implementation for them.

public abstract class SoapURL {
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }
    public String getUrl() {
       return "https://host.com/AndroidWFC/MobileWS.asmx";
    }

    public abstract String getSoapAction();
    public abstract String getMethodName();
}

I think your refactoring was not correct. If you consider that a SoapUrl must have a getNameSpace(), then define the getNameSpace() method in the interface. If the purpose of your refactoring was to always return the same values for some of the SoapUrl's methods, I would suggest to follow this organization:

public interface SoapUrl {
    String getNameSpace();
    String getUrl();
    String getSoapAction();
    String getMethodName();
}

public abstract class BaseSoapUrl implements SoapUrl {
    @Override
    public String getNameSpace() {
        return "https://host.com/AndroidWFC/";
    }

    @Override
    public String getUrl() {
        return "https://host.com/AndroidWFC/MobileWS.asmx";
    }
}

public class LoginSoap extends BaseSoapUrl {

    @Override
    public String getSoapAction() {
        return "https://host.com/AndroidWFC/UserControl";
    }

    @Override
    public String getMethodName() {
        return "UserControl";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM