Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: What are general rules for overloading method in java?

  1. #1
    Join Date
    Dec 2010
    Beans
    136
    Distro
    Ubuntu 14.04 Trusty Tahr

    Question What are general rules for overloading method in java?

    Basically, I am trying to make methods to return a value when they are called from another method or class. I was wondering what conditions and rules I have to follow to make them work. If you want an example of what I meant, just ask me and I will update the post.

    Here is an example of what I have done. I have created two classes. In the first class, I have tried to make a method in another class to return a value with a command to output value on the console. But I get an error that says there are incompatible types. Here are two classes that I have created and I wanted to make a calculator out of that: The first class:

    Code:
    class calc1
    { public static int num1; //first number variable    
    public static int num2; //Second number variable    
    public static String op; //Operatior variable    
    public static void main(String[] args) //    
    {        num1 = Integer.parseInt(args[0]);        
    num2 = Integer.parseInt(args[2]);        
    op = args[1];        
    calc3.calculate(op); //Calling method from the second class with an arugement.    
    }
    }
    


    Here is the second:

    Code:
    class calc3
    {    public static int calculate(String ops)    
    {        
    switch(ops) //I believe that ops stores value from op variable in the first class.        
    { case "+": { int num = calc1.num1 + calc1.num2;                
    return (System.out.println(num));            
    }       
    }    
    }
    }


    Here is an error message I get after I try to compile it:

    Code:
    Desktop$ javac calc1.java
    ./calc3.java:10: error: incompatible types        return (System.out.println(num));^                                
    required: int 
    found:    void
    1 error

    PS. I believe it is not overriding process, but still I would like to know about calling methods and returning value when they are called.
    Last edited by pavelexpertov; June 15th, 2013 at 10:49 PM.
    "If you are in hell, keep going." Winston Churchil
    ubuntucrazygeek.blogspot.com
    http://motivatedsuccess.tumblr.com/
    http://pavelexpertov.tumblr.com/

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: What are general rules for overloading method in java?

    I think we need an example. Be clear whether you mean overload or override. Check on Wikipedia if you aren't sure.

  3. #3
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: What are general rules for overloading method in java?

    when they are called from another method or class
    Caller-dependent behavior? Gross

  4. #4
    Join Date
    Mar 2010
    Location
    London
    Beans
    924

    Re: What are general rules for overloading method in java?

    As r-senior said, I think with need more information of what you're trying to achieve.

  5. #5
    Join Date
    Mar 2008
    Location
    Missouri, USA
    Beans
    93
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: What are general rules for overloading method in java?

    If you have a class and want too override say, toString(), you would put the @Override notation before the method like:

    public class SmartyPants
    .........
    @Override
    public String toString() {
    return " Not gonna say!";
    }

    ....
    In class b, you instantiate a SmartPants object and call the overridden toString().

    SmartyPant levis = new SmartyPants();

    levis.toString() gives " Not gonna say!"

  6. #6
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: What are general rules for overloading method in java?

    Actually, the @Override annotation is not in any way required. It provides the benefit of making the compiler actually check that you are, indeed, overriding a method from a superclass.

    It is sufficient to just implement the same method in a subclass, and you get the overriding behaviour automatically.
    LambdaGrok. | #ubuntu-programming on FreeNode

  7. #7
    Join Date
    Mar 2008
    Location
    Missouri, USA
    Beans
    93
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: What are general rules for overloading method in java?

    Thank you, all this time I believed it required.
    Ubuntu 13.04 (Raring Ringtail)

  8. #8
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,675
    Distro
    Ubuntu

    Re: What are general rules for overloading method in java?

    Quote Originally Posted by pavelexpertov View Post
    Here is an error message I get after I try to compile it:

    [/FONT][/COLOR]
    Code:
    Desktop$ javac calc1.java
    ./calc3.java:10: error: incompatible types        return (System.out.println(num));^                                
    required: int 
    found:    void
    1 error

    PS. I believe it is not overriding process, but still I would like to know about calling methods and returning value when they are called.
    It's usually better to add a new post to the thread rather than go back and edit a previous one with more information. A lot of people will just look at the last post, see there is nothing new and move on.

    The compilation problem is that you are trying to return the return value of System.out.println from a method. System.out.println is a void method, so there is no return value. You probably want to return the result of the calculation as an int and print it using System.out.println in the calc1 class.

    What's your programming background?
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  9. #9
    Join Date
    Jun 2011
    Location
    Paris
    Beans
    55

    Re: What are general rules for overloading method in java?

    Quote Originally Posted by sum1nil View Post
    Thank you, all this time I believed it required.
    It's definitely useful and can help cut down on bugs.

  10. #10
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: What are general rules for overloading method in java?

    Quote Originally Posted by Leuchten View Post
    It's definitely useful and can help cut down on bugs.
    On the other hand, inheritance and overriding is such a core mechanism in Java that it's vitally important to understand that it does not depend on an annotation.
    LambdaGrok. | #ubuntu-programming on FreeNode

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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