Reputation: 1042
I have a class called Property
which has nothing but get
-methods. All the fields will be set when a new instance of Property
is created. Property
implements an interface called IProperty
.
Due to some bug in a library I use, I have to set the name of an instance of Property
anew after its creation. Therefore it was suggested to create a WrapperProperty
class that will provide a public setName
-method which itself calls a therefore created setName()
-method in Property
, which will be protected/package view.
The problem is that I cannot make this method protected in Property
, because Eclipse tells me to add it to the interface IProperty
and make it public.
Is there some work-around to it?
WrapperIProperty:
public class WrapperIProperty {
private IProperty prop;
WrapperIProperty(Property prop) {
this.prop = prop;
}
public void setName(String name) {
prop.setName(name);
}
}
Property:
public class Property implements IProperty {
String name;
protected void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getFoobar() {
return 123;
}
public int getWhatever() {
return 987;
}
}
IProperty:
public interface IProperty {
public int getWhatever();
public int getFoobar();
public String getName();
}
This is how it looks at the moment. Obviously it won't work, since I cannot let the method be protected in the Property
class. Therefore I best get rid of the interfacee entry somehow. But how?
Upvotes: 0
Views: 311
Reputation: 1042
Found the solution to that problem:
WrapperIProperty :
public class WrapperIProperty {
private Property prop;
public WrapperIProperty(IProperty prop) {
this.prop = (Property) prop;
}
public void setName(String name) {
prop.setName(name);
}
}
Property:
public class Property implements IProperty {
private String name = null;
[...]
void setName(String name) {
this.name = name;
}
}
IProperty:
public interface IProperty {
[...]
}
This will do the job
Upvotes: 0
Reputation: 5928
What you probably want to do is to leave the IProperty
interface alone (don't add the setName
method to it) and create a delegating wrapper class which provides the method you want (wraps an implementation of the interface).
This way you can feed wrapped properties and regular properties to whatever needs them.
public class WrappedProperty implements IProperty {
private String name;
private Property prop;
WrappedProperty (Property prop) {
this.prop = prop;
}
protected void setName(String name) {
this.name = name;
}
public int getWhatever() {
return prop.getWhatever();
}
public int getFoobar() {
return prop.getFoobar();
}
public String getName() {
if (this.name == null) {
return prop.getName():
} else {
return this.name;
}
}
}
public class Property implements IProperty {
public String getName() {
return "blah";
}
public int getFoobar() {
return 123;
}
public int getWhatever() {
return 987;
}
}
public interface IProperty {
public int getWhatever();
public int getFoobar();
public String getName();
}
Upvotes: 1
Reputation: 1264
You cannot have a public methodName in an Interface and a private or protected methodName in a Class implementing this Interface.
So you can have the methodName public in your Class :
UPDATE
If you want it only in Interface you have to change your Interface in an AbstractClass and put in it the method public final returnCode methodName if the method is common for all inherited classes
Upvotes: 0
Reputation: 7899
Methods in an Interface
are public in scope so implementing class cannot override methods by reducing their accessibility. Make them public
Upvotes: 0