-1

I want to use java interface in a way that i will make a call defining interface in my other class like 'private SoapURL soapURL;' and than i can access any class's method for example : i want to use login:-

private SoapURL soapURL;
SoapUrl = LoginSoap ();

String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();

Is there any way to do something like this. I am sorry i am not very good with Object Oriented principles but if there is a solution for my problem i would like to know it. Thanks in advance.

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

LoginSoap class

public class LoginSoap implements SoapURL {

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

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

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

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

    @Override
    public String getTag() {
        return "Login Activity";
    }
}

SignUpSoap class

public class SignUpSoap implements SoapURL {

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

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

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

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

    @Override
    public String getTag() {
        return "SignUp Activity";
    }
}

ResetPasswordSoap class

public class ResetPasswordSoap implements SoapURL {

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

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

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

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

    @Override
    public String getTag() {
        return "Forget Password Activity";
    }
}
10
  • 1
    What is your current code and where is your concrete problem?
    – Smutje
    Feb 19, 2015 at 20:09
  • Your interfaces all look correct. Are you wanting to write a piece of code that figures out which implementation you should use?
    – Ryan D
    Feb 19, 2015 at 20:10
  • i have given my code above, i just want to know is it possible to access classes data through calling interface.. like soupURL = LoginSoap(); than the values of soapUrl.getUrl(); will change to LoginSoap Class Feb 19, 2015 at 20:12
  • Yes, what's the problem? You can do that. Feb 19, 2015 at 20:12
  • how ? could you please give an example or point me to any tutorial link Feb 19, 2015 at 20:13

3 Answers 3

1

Your implementation looks correct. To make use of it, you can do this in main:

SoapURL reset = new ResetPasswordSoap();
System.out.println(reset.getUrl());

This is a method of minimizing coupling in large systems. And reduces dependency between objects by making use of a common interface for groups of objects that work together. You might be new at Object oriented principles, but you are one step ahead of the game already

To pass it to a function, you do:

public JPanel resetPass(SoapURL reset) {
    ...
}

// In main:
JPanel resetPassPanel = resetPass(reset);
0
1

Just do, for example:

SoapURL example = new LoginSoap();
String a = example.getTag();

a should be equal to "Login Activity"

0
1

The main use of Interface is polymorphism, or the ability to perform the same operation on a number of different objects, which is exactly what you wanted in your scenario

Your approach is absolutely fine , just a modification needed

private SoapURL soapURL;
//SoapUrl = LoginSoap (); // This line should be replaced with the Below line
soapURL=new LoginSoap();

String nameSpace = soapURL.getMethodName();
String url = soapURL.getUrl();

Since LoginSoap, SignUpSoap,ResetPasswordSoap classes are implemented classes of SoapURL Interface , thus reference variable of SoapURL can store Object of any of these child classes

soapURL=new LoginSoap();//soapURL.someMethod will call method of LoginSoapClass
soapURL=new SignUpSoap();// will call method of SignUpSoap class
soapURL=new ResetPasswordSoap();
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.