Previous
Index

Next

Objective 4, Overloading and overriding

State the legal return types for any method given the declarations of all related methods in this or parent classes.

Note on this objective

This seems to be a rather obscurely phrased objective. It appears to be asking you to understand the difference between overloading and overriding.

To appreciate this objective you need a basic understanding of overloading and overriding of methods. This is covered in

Section 6: Overloading Overriding Runtime Type and Object Orientation

Methods in the same class

By related methods I assume that the objective means a method with the same name. If two or more methods in the same class have the same name, the method is said to be overloaded. You can have two methods in a class with the same name but they must have different parameter types and order.


It is the parameter order and types that distinguish between any two versions of overloaded method. The return type does not contribute towards distinguishing between methods.


The following code will generate an error at compile time, the compiler sees amethod as an attempt to define the same method twice. It causes an error that will say something like

method  redefined with different return type: void amethod(int)
was int amethod(int)


class Same{
public static void main(String argv[]){
        Over o = new Over();
        int iBase=0;
        o.amethod(iBase);
        }

        //These two cause a compile time error
        public void amethod(int iOver){
                System.out.println("Over.amethod");
        }

        public int amethod(int iOver){
                System.out.println("Over int return method");
                return 0;
        }

}
    

Be Careful

The return data type does not contribute towards distinguishing between one method and another.

Methods in a sub class

You can overload a method in a sub class. All that it requires is that the new version has a different parameter order and type. The parameter names are not taken into account or the return type.

If you are going to override a method, ie completely replace its functionality in a sub class, the overriding version of the method must have exactly the same signature as the version in the base class it is replacing. This includes the return type. If you create a method in a sub class with the same name and signature but a different return type you will get the same error message as in the previous example. i.e.

method  redefined with different return type: void amethod(int) 

was int amethod(int)

The compiler sees it as a faulty attempt to overload the method rather than override it.Static methods cannot be overriden

If you consider overriding the simple replacement of a method ina child class it is easy to believe at static methods can be overriden. Indeed I have had many emails from people that include code that appears to illustrate how static methods can be overriden. However this does not take into account the more subtle aspect of overriding which is the runtime resolution of which version a method will be called. The following code appears to illustrate how static methods can be overriden.

class Base{
    static   void amethod(){
    System.out.println("Base.amethod");

    }
}

public class Cravengib extends Base{
    public static void main(String arg[]){
    Cravengib cg = new Cravengib();
    cg.amethod();
    }
    static void amethod(){
    System.out.println("Cravengib.amethod");
    }
    
}

If you compile and run this code you will find it outputs the text Cravengib.amethod, which appears to be a nice illustration of overriding. However there is more to overriding than simple replacement of one method by another in a child class. There is also the issue of runtime resolution of methods based on the class type of the reference. and this can be illustrated by making the type of the reference (the part on the left hand side of the instance initialisation) to the type of the class that is being instantiated.

In the previous example, because the method called amethod is associated with the class, and not with any particular instance of the class it does not matter what the type of class being created it, just the type of the reference.. Thus if you change the line before the callng of amethod to read

Base cg= new Cravengib()

You will find that when you run the program you get an output of

Base.amethod

The reference cg is a reference (or pointer) of type Base to an instance in memory of the class Cravengib. When a call is made to a static method, the JVM does not check to see what the type is that is being pointed to i,t just calls the one instance of the method that is associated with the Base class.

By contrast when a method is overriden the JVM checks the type of the class that is being pointed to by the reference and calls the method associated with that type.

To complete the illustration, if you change both versions of amethod to be non static, keep the creation of the class to

Base cg= new Cravengib()

compile and run the code, and you will find that amethod has been overriden and the output will be

Cravengib.amethod

Quiz Logo

Questions

Question 1)

Given the following class definition

public class Upton{
public static void main(String argv[]){
        }

        public void amethod(int i){}
        //Here
}

Which of the following would be legal to place after the comment //Here ?
1) public int amethod(int z){}
2) public int amethod(int i,int j){return 99;}
3) protected void amethod(long l){ }
4) private void anothermethod(){}



Question 2)

Given the following class definition

class Base{
        public void amethod(){
        System.out.println("Base");
        }
}

public class Hay extends Base{

public static void main(String argv[]){
        Hay h = new Hay();
        h.amethod();
        }

}


Which of the following methods in class Hay would compile and cause the program to print out the string "Hay"

1) public int amethod(){ System.out.println("Hay");}
2) public void amethod(long l){ System.out.println("Hay");}
3) public void amethod(){ System.out.println("Hay");}
4) public void amethod(void){ System.out.println("Hay");}

Question 3)

Given the following class definition

public class ShrubHill{
    public void foregate(String sName){}
//Here
}

Which of the following methods could be legally placed immediately after the comment //Here

1) public int foregate(String sName){}
2) public void foregate(StringBuffer sName){}
3) public void foreGate(String sName){}
4) private void foregate(String sType){}

Answers

Answer to Question 1)

2) public int amethod(int i, int j) {return 99;}
3) protected void amethod (long l){}
4) private void anothermethod(){}

Option 1 will not compile on two counts. One is the obvious one that it claims to return an integer. The other is that it is effectively an attempt to redefine a method within the same class. The change of name of the parameter from i to z has no effect and a method cannot be overridden within the same class.

Answer to Question 2)

3) public void amethod(){ System.out.println("Hay");}

Option 3 represents an overriding of the method in the base class, so any zero parameter calls will invoke this version.

Option 1 will return an error indicating you are attempting to redefine a method with a different return type. Although option 2 will compile the call to amethod() invoke the Base class method and the string "Base" will be output. Option 4 was designed to catch out those with a head full of C/C++, there is no such thing as a void method parameter in Java.

Answer to Question 3)

2) public void foregate(StringBuffer sName){}
3) public void foreGate(String sName){}

Option 1 is an attempt to redefine a method twice, the fact that it has an int return type does not contribute to distinguish it from the existing foregate method. Changing the name of a method parameter as in option 4, does not distinguish it from the existing method. Note that in option 2 foreGate has a capital G in it.

Other sources on this subject

Jyothi Krishnan
http://www.geocities.com/SiliconValley/Network/3693/obj_sec1.html#obj4

In that link Jyothi suggests you go to objective 19 which you can find at
http://www.geocities.com/SiliconValley/Network/3693/obj_sec6.html#obj19






Previous
Index

Next