How to remove argument from abstract method.
Hi
I have a query regarding the use of an abstract function in an abstract class.
public abstract class A{
public abstact void func(Object o);
public void func1(){
x = func(o)
}
}
public class B extends A{
public void func(Object o){
y = o;
return y;
}
}
public class C extends A{
public void func(Object o){
y = #some value#
return y
}
}
As in the class c the override function func is not using the argument anywhere in its definition whereas class b is using that argument. So i want to ask is this a good way to do or there is some other method.
Please help me in this.
Thanks
Re: How to remove argument from abstract method.
A comment on the code you've posted:
Two of the methods (not functions) are void but they return a value.
void methods do NOT return a value.
Quote:
is this a good way to do
To do what?
Re: How to remove argument from abstract method.
It looks like poor design. Without knowing the specific example you have in mind, I can't be more precise, but in general, the abstract class should have an overloaded no-argument method for the subclass to implement.
Some designs allow a null argument to be passed to a method, which is arguably poor practice, and there can be circumstances when a method implementation deliberately ignores an argument, such as Mock Objects or the the Null Object pattern, but that's effectively exceptional behaviour. You should seriously consider why the caller would call a method and pass an argument that will be ignored...
Re: How to remove argument from abstract method.
Sorry thats my mistake i means this code
public abstract class A{
public abstact Object func(Object o);
public void func1(){
x = func(o)
}
}
public class B extends A{
public Object func(Object o){
y = o;
return y;
}
}
public class C extends A{
public Object func(Object o){
y = some value
return y
}
}
I want to ask that is this a good way of implementation as in one class the override function using the argument inside whereas in other class that override function is not using the argument. Is this a good approach ??
Re: How to remove argument from abstract method.
The reason the argument is passing is because it is used in some classes that are extending that abstract class but there are also classes which are extending this abstract class but they are not using that argument while defining that function.
So what should i do to handle this.
Re: How to remove argument from abstract method.
A good approach to WHAT? You've received the answer- it looks like a sign of a bad design, but without any details, we can't answer you any more specifically.
Edit- I just saw your second post, which goes into a bit more detail. I think you're overthinking it- just code it how it makes sense to you, and stop worrying about doing it the absolute correct way. No matter what you do, somebody else would have done it differently.
Re: How to remove argument from abstract method.
I thought I already told you...
If you want advice on a particular example, post it.
Re: How to remove argument from abstract method.
Ok so let me try to explain you what i am doing.
I have an abstract class which is extended by 5 more classes. The abstract class contains the whole code of making an xml file. But there are few abstract methods declare in abstract class which are define by the classes which is extending this class. And their definition are specific to different products who are extending this abstract class.
So the definition of the function i am talking about in the above code is implemented differently by all products but the argument that i am passing in it is used by few classes that are extending this abstract class but others have no use of that argument.
What approach now should i take to implement this thing ?
Re: How to remove argument from abstract method.
You haven't given enough information to say - why are some class methods able to ignore the argument?
I suggest you post an SSCCE.
Quote:
What approach now should i take to implement this thing ?
Can you explain exactly what 'thing' you want to implement? Is it the abstract class itself or a subclass implementation?
Re: How to remove argument from abstract method.
Actually the argument m passing is a map and some classes are ignoring thet argument because they doesnt require the values from map to return a value while some classes do require that map.
And am creating objects for these subclasses to create an XML for these product specific.
Also the subclasses are overriding the other few more functions in their respective classes which are define abstract in the parent abstract class.
Is this help you in understanding what i am doing. ??
Re: How to remove argument from abstract method.
So it sounds like what you're saying is that the caller of this method will always supply a map (presumably as default data for implementations that can't obtain their own data), but some implementations won't need it. In this case, I don't see anything wrong with implementing a method that takes the argument but doesn't use it, although it seems somewhat clumsy.
As has already been repeated, it's not possible to be more specific without knowing the exact details and/or code. Adding 'XML' to your explanation doesn't help at all.
Re: How to remove argument from abstract method.
I try to come back with some sample code to explain my problem.
Thanks
Re: How to remove argument from abstract method.
Recapping
You have ClassA which is abstract with an abstract method and that method has a Map as a parameter. ClassB extends ClassA and uses the Map in the method. ClassC extends ClassA but does not use the Map.
In that case as others have said, you have a bad design. Either ClassC should not be extending ClassA or ClassA should not have that abstract method.