Reputation: 1670
I got a problem with java when i try to override a method,my code is following:
public abstract class CustomAdapter{
public abstract Boolean addItem(Class<? extends Object> aObject);
}
public class RainAdapter extends CustomAdapter {
@Override
public Boolean addItem(ClassOfRain aRainInfo) {
// do something
return true;
}
}
Can I declare the the "Class" to "ClassOfRain"? If yes,how to do?
Thanks for your reading!
Upvotes: 4
Views: 9236
Reputation: 13765
the method signatures must match when implementing abstract/interface methods and/or overwriting ... you could do something like this tho
public abstract class CustomAdapter{
public abstract Boolean addItem( Object o );
}
public class RainAdapter extends CustomAdapter {
public Boolean addItem( Object o ){
if ( o.getClassName().equals( "ClassOfRain" ) ){
return this.addItem( (ClassOfRain) o );
}
return false;
}
private Boolean addItem(ClassOfRain aRainInfo) {
// do something
return true;
}
}
Upvotes: 0
Reputation: 62469
I think you are a bit confused. Are you sure you are not trying to say:
public abstract class CustomAdapter<T extends Object> {
public abstract Boolean addItem(T aObject);
}
public class RainAdapter extends CustomAdapter<Rain> {
@Override
public Boolean addItem(Rain aRainInfo) {
// do something
return true;
}
}
In my interpretation of your class structure, you are trying to make a generic addItem
method, so passing around the actual class object is of no use.
Upvotes: 5
Reputation: 1543
Class<Rain.class>
would hold the reflection of Rain
class. But this will not work, because overridden methods must have the same formal parameters, so you'll have to use Class<? extends Object>
.
Upvotes: 0
Reputation: 13073
That is not possible. A method override means that you put exactly the same method header. Only thing you can change is the name of the given parameter.
Upvotes: 0