Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: How to remove argument from abstract method.

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.

    is this a good way to do
    To do what?

  3. #3
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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...

  4. #4
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ??

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default 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.
    Last edited by KevinWorkman; June 30th, 2011 at 11:14 AM.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: How to remove argument from abstract method.

    I thought I already told you...

    If you want advice on a particular example, post it.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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 ?

  9. #9
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

    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?
    Last edited by dlorde; June 30th, 2011 at 11:30 AM.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. ??

  11. #11
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default 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.

  12. #12
    Junior Member
    Join Date
    Jun 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to remove argument from abstract method.

    I try to come back with some sample code to explain my problem.

    Thanks

  13. #13
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default 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.

Similar Threads

  1. [SOLVED] Send File (path) as argument to method
    By efluvio in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 31st, 2011, 01:13 PM
  2. Argument promotion
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 3
    Last Post: May 16th, 2011, 07:02 AM
  3. Replies: 2
    Last Post: February 28th, 2011, 10:51 AM
  4. I need to fix the remove method on DoublyLinkedList.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 23rd, 2010, 09:20 PM
  5. Generic method using an int[] argument
    By dubois.ford in forum Collections and Generics
    Replies: 2
    Last Post: March 18th, 2010, 07:18 AM