Reputation: 2055
I have error when I send this:
myClaass.put(new ImenResult(1).Show());
myClaass.put(new ImenResult(2));
I understand why I have got this problem because I send to class which have interface parameter. My question is: what I have to add to interface to send this data and haven`t got error because this structure have to be this same (not my decision) because later I can send String or another things (Class). I know this is very difficult question.
Program:
Class_1 myClaass = new Class_1();
myClaass.put(new ImenResult(1).Show());
myClaass.put(new ImenResult(2));
myClaass.put(new ImenResult(3));
ImenResult:
public class ImenResult implements IImenResult{
public Integer i;
public ImenResult(Integer i ) {
this.i = i;
}
@Override
public Integer Show() {
return i;
}
Class_1
public class Class_1 implements IQRack{
public List<IAdant> adan;
public void put(IAdant value) {
adan.add(value);
}
}
IAdan
public interface IAdant {
}
Thanks for help I told you this is difficult question (of course for people who are not Java specialist) but topic is fixed. I gave you points. Thanks again for helped. [closed]
Upvotes: 1
Views: 142
Reputation: 17839
you should implement the method put(Integer)
in Class_1
for this to work
public class Class_1 implements IQRack {
public List<IAdant> adan;
public void put(IAdant value) {
adan.add(value);
}
void put(Integer Show) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
UPDATE After your comment i think this is what you are trying to do:
public class Class_1 implements IQRack {
private List<IAdant> adan = new ArrayList<IAdant>();
public Class_1(IAdant i) {
this.put(i);
}
void put(IAdant Show) {
throw new UnsupportedOperationException("Not yet implemented");
}
}
public class IAdant {
Boolean bValue;
String sValue;
Integer iValue;
public IAdant(Object o) {
if (o.getClass().isInstance(new Integer(1))) {
iValue = (Integer) o;
} else if (o.getClass().isInstance(false)) {
bValue = (Boolean) o;
} else if (o.getClass().isInstance("")) {
sValue = (String) o;
}
}
}
Upvotes: 3
Reputation: 46428
myClaass.put(new ImenResult(1).Show());
myClaass.put(new ImenResult(2));
first case would file, cuz, your put method expects IAdant(any that that implements IAdant) and you are passing an integer as the show method would return one.
second case would fail because of the same reason that your ImenResult class is not implementing or a Type of IAdant.
if you want the second case to work, implement IAdant in ImenResult class.
public class ImenResult implements IImenResult,IAdant{
//implement the methods from both the interfaces
}
now myClaass.put(new ImenResult(2));
line would work with out any compiler errors.
If you wanna get the first case myClaass.put(new ImenResult(1).Show()); to compile , you have to overload the put method in myClass class.
public void put(int val) {
//do your logic here
}
Upvotes: 2
Reputation: 5869
The line: myClaass.put(new ImenResult(1).Show());
will fail, because myClaass.put()
is expecting to get an object that implements IAdant
, but you are sending and Integer
. Integer
does not implement IAdant
.
Upvotes: 3