Results 1 to 3 of 3
  1. #1
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

    Exclamation Overloading abstract methods??!?

    Hi,
    Im writing a calculator using enums. Some of the operations take only 1 parameter while others take 2. I have an abstract method called apply which I've tried to overload so it can take either 1 or 2 doubles. The problem is when I try to compile the compiler complains that my enums that use apply with 1 param haven't overridden the version of apply with 2 params and vice versa. How do I go about solving this? Here's snippets of my code:
    Java Code:
      
      UNARYPLUS{ @Override
            public double apply( double x ){ 
                return ++x; 
            } 
        }
    Java Code:
     
        MULTIPLY{@Override
            public double apply( double x, double y ){
                return x * y;
            }
        }
    Java Code:
    public abstract double apply( double operand );
    public abstract double apply( double first, double second );
    and heres a snippet of the compiler errors i get:
    Java Code:
    Operations.java:9: <anonymous Operations$1> is not abstract and does not override abstract method apply(double,double) in Operations
        UNARYPLUS{ @Override
        ^
    Java Code:
    Operations.java:29: <anonymous Operations$5> is not abstract and does not override abstract method apply(double) in Operations
        MULTIPLY{@Override
        ^
    Thanks very much in advance!

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    12

    Default

    instead of making the apply methods abstract, they need to have an empty body in the default implementation even though they would never get called,
    Java Code:
    public  double apply( double operand ) {
      // empty
    }
    public double apply( double first, double second ) {
      // empty
    }
    or, each operation would need to override them.

    Java Code:
    UNARYPLUS{
            @Override
            public double apply( double x ){ 
                return ++x; 
            } 
    
            @Override
            public double apply(double first, double second) {
              // throw an exception, or could leave empty.
              throw new IllegalArgumentException("this is not supported for this operand");
            }
        }
    but its likely easier to just make the default methods not abstract with empty bodies.

  3. #3
    tfitz666 is offline Member
    Join Date
    Jul 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Replies: 2
    Last Post: 11-21-2008, 12:20 AM
  2. Abstract Class with Static Methods
    By bugger in forum New To Java
    Replies: 7
    Last Post: 09-05-2008, 12:20 AM
  3. Overloading methods dynamically
    By Syed.muddasir in forum New To Java
    Replies: 6
    Last Post: 07-31-2007, 02:34 PM
  4. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •