Overriding and overloading are two salient features of Java. Overriding allows you to modify the behavior of an inherited method to meet the specific needs of a subclass, while overloading allows you to use the same method name to implement different (but related) functionalities. Therefore, overriding and overloading facilitate code extensibility and flexibility.
Remember that Overriding and overloading are two major and crucial types of Polymorphism.
Method Overriding
As we know that a subclass (child class) inherits all the (non-private) methods and variables of the superclass. In some situations, you might need to change the behavior of an inherited method, such as when implementing polymorphism. In that case, you would redefine the method by keeping the same signature but rewriting the body. This is exactly what is called method overriding.
In other words, method overriding is a feature of Java that lets the programmer declare and implement a method in a subclass that has the same signature as a method in the superclass. The same signature in this case means the same name and the same number of parameters and their types appearing in the same order. While the signature must be the same, the code in the method body may be, and generally is, different.
The body of the overriding method may or may not include a call to the overridden method. That is, you can specifically invoke the overridden method from the body of the overriding method, if you want to. As an example, consider the code example below.
public class OverridingExample {
public static void main(String[] args)
{
Animal
heyAnimal = new Animal();
Cow
c = new Cow();
Buffalo
b = new Buffalo();
heyAnimal
= c;
heyAnimal.saySomething();
heyAnimal
= b;
heyAnimal.saySomething();
}
}
class Animal {
public void saySomething() {
System.out.println("Animal
sound...");
}
}
class Cow extends Animal {
public void saySomething() {
super.saySomething();
System.out.println("Cow
Sound!");
}
}
class Buffalo extends Animal {
public void saySomething() {
super.saySomething();
System.out.println("Buffalo
sound!");
}
}
If you execute the code listing it will give output like:
Animal sound...
Cow Sound!
Animal sound...
Buffalo sound!
The following are the key points about method overriding:
• You cannot override a method that has the final modifier.
• You cannot override a static method to make it non-static.
• The overriding method and the overridden method must have the same return type. J2SE 5.0 allows a covariant return type as well.
• The number of parameters and their types in the overriding method must be same as in the overridden method and the types must appear in the same order. However, the names of the parameters may be different.
• You cannot override a method to make it less accessible. For example, overriding a public method and declaring it protected will generate a compiler error, whereas overriding a protected method and declaring it public will be fine.
• If the overriding method has a throws clause in its declaration, then the following two conditions must be true:
Cow Sound!
Animal sound...
Buffalo sound!
The following are the key points about method overriding:
• You cannot override a method that has the final modifier.
• You cannot override a static method to make it non-static.
• The overriding method and the overridden method must have the same return type. J2SE 5.0 allows a covariant return type as well.
• The number of parameters and their types in the overriding method must be same as in the overridden method and the types must appear in the same order. However, the names of the parameters may be different.
• You cannot override a method to make it less accessible. For example, overriding a public method and declaring it protected will generate a compiler error, whereas overriding a protected method and declaring it public will be fine.
• If the overriding method has a throws clause in its declaration, then the following two conditions must be true:
- The overridden method must have a throws clause, as well.
- Each exception included in the throws clause of the overriding method must be either one of the exceptions in the throws clause of the overridden method or a subclass of it.
Method Overloading
Method overloading is helpful when the same task is to be performed in slightly different ways under different conditions. So, method overloading is a feature of Java that facilitates defining multiple methods in a class with identical names. In contrast to overriding methods, no two overloaded methods could have the same parameter types in the same order. The return types in overloaded methods may be the same or different, while the return type of an overriding method must match that of the overridden method.For example, assume that we want to write a class for calculating the area of different shapes such as a triangle, a rectangle, and a square. All of these tasks are the same to the extent that they all relate to calculating the area, but they are performed differently because the formulae to calculate the area in all of these cases would differ and would require different input. To perform this task, we write three versions of an overloaded method calculateArea(…) inside the class AreaCalculator in below code:
Overloading Example:
class TestAreaCalculator {
public static void main(String[] args)
{
AreaCalculator
ac = new AreaCalculator();
System.out.println("Area of a
rectangle with dimension (2.0, 3.0):"
+
ac.calculateArea(2.0f, 3.0f));
System.out.println("Area of a
triangle with dimension(2.0, 3.0, 4.0):"
+
ac.calculateArea(2.0, 3.0, 4.0));
System.out.println("Area of a
circle with radius 2.0: "
+
ac.calculateArea(2.0));
}
}
class AreaCalculator {
float calculateArea(float length, float width) {
return length * width;
}
double calculateArea(double radius) {
return ((Math.PI) * radius * radius);
}
double calculateArea(double a, double b, double c) {
double s = (a + b + c) /
2.0;
return Math.sqrt(s *
(s - a) * (s - b) * (s - c));
}
}
The output from this code is:
Area of a
rectangle with dimension (2.0, 3.0): 6.0
Area of a rectangle with dimension (2.0, 3.0, 4.0): 2.9047375096555625
Area of a circle with radius 2.0: 12.566370614359172
Area of a rectangle with dimension (2.0, 3.0, 4.0): 2.9047375096555625
Area of a circle with radius 2.0: 12.566370614359172
The following are the key points about method overriding:
• Two or more methods in the same class with the same name are called overloaded if they have either different sets of parameter types or the same set of parameter types in different order.
• The return type of two overloaded methods can be different or the same.
• Overloaded methods are effectively independent of each other.
• The compiler determines which of the overloaded methods to call by looking at the argument list of the method call.
• Any of the methods inherited from the superclass can also be overloaded.
• Overloaded versions of a method can have different or the same checked exceptions in the throws clauses.
• Overloaded versions of a method can have different modifiers.
Reference(s): SCJP Exam for J2SE 5
No comments:
Post a Comment