Results 1 to 10 of 12

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

Threaded View

  1. #1
    Join Date
    Dec 2010
    Beans
    109
    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/

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
  •