I have a rather basic question about interfaces, something I’m rather new too. I typically would instantiate my class with an overloaded constructor. I’m now trying to use interfaces and wondering how I would populate my constructor. Would I just use something like setSomeMethod(arguement1, arguement2) in my interface to populate my attributes?
I’d also like to note I’m using “Tapestry5” framework with service Injection. Example
public class Main {
@Inject
private Bicycle bicycle;
public Main() {
//Not sure how to pass constructor variables in
this.bicycle();
}
}
Interface
public interface bicycle {
public void someMethod(String arg1, String arg2);
}
Impl Class
public class MountainBike implements bicycle {
private String arg1;
private String arg2;
public MountainBike() {
//Assuming there is no way to overload this constructor
}
public void someMethod(String arg1, String2 arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
}
}
Then how do you handle extended classes? I’m not sure how to populate the extended class constructor.
public class MountainBike extends BicycleParts implements bicycle {
private String arg1;
private String arg2;
public MountainBike() {
//Assuming there is no way to overload this constructor
//Not sure where to put super either, but clearly won't work here.
//super(arg1);
}
public void someMethod(String arg1, String2 arg2) {
this.arg1 = arg1;
this.arg2 = arg2;
//Assuming it doesn't work outside of a constructor, so shouldn't work
//here either.
//super(arg1);
}
}
public class BicycleParts {
private String arg1;
public void BicycleParts(String arg1) {
this.arg1 = arg1;
}
}
Thanks in advance.
First off, your
bicyclemethod should be declared with a return type:Interfaces define a contract for methods and not how objects are instantiated. They can also define static variables.
To use
someMethodin your MountainBike constructor, you could make the call in the constructor:Wrt, your question on extending the class, the super statement must appear as the very first statement in the constructor i.e.: