Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

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

  1. #11
    Join Date
    Dec 2010
    Ubuntu 13.04 Raring Ringtail

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

    Quote Originally Posted by r-senior View Post
    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?
    My programming background is a beginner, but I managed to figure the problem out by reading and researching, so I manage to learn as well as to correct the problems.
    "If you are in hell, keep going." Winston Churchil

  2. #12
    Join Date
    Jan 2012

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

    Quote Originally Posted by pavelexpertov View Post
    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:

    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:

    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:

    Desktop$ javac
    ./ 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.
    use this instead
    calc3 cal = new calc3();     
    System.out.println( calc.calculate( pass the values here ) ); //Calling method from the second class with an arugement.
    and int calc3 class,
    you cannot do this unless it extends calc1 and the variable should be global and public.
    int num = calc1.num1 + calc1.num2;

Page 2 of 2 FirstFirst 12

Tags for this Thread


Posting Permissions

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