Java Technical Write-UP: Topic: - Method Overloading & Method Overriding
Java Technical Write-UP: Topic: - Method Overloading & Method Overriding
Overloading
Method overloading is one of the ways that java implements polymorphism. When an overloaded method is called, java uses the type and/or number of arguments to decide which version of the overloaded method to actually call. Overloaded methods may or may not have different return types. When java encounters a call to an overloaded method, it simply executes the version of the method whose parameters match the arguments used in the call. However, this match need not always be exact. In some cases javas automatic type conversions can play a role in overload resolution. Java will employ its automatic type conversion only if no exact match is found. The important points - A difference in return type only is not sufficient to constitute an overload and is illegal. You should restrict your use of overloaded method names to situations where the methods really are performing the same basic function with different data. Overloaded methods may call one another simply by providing a normal method call with an appropriately formed argument list.
Overloaded Constructors:
These behave as overloaded methods. Example below demonstrates method overloading in java. In java method overloading means creating more than a single method with same name with different signatures. In the example three methods are created with same name. Java understands these methods with there signatures. Java identifies the methods by comparing their signatures like return types, constructor parameters & access modifier used.
code:
class Overload { void test(int a) { System.out.println("a: " + a); } void test(int a, int b) { System.out.println("a and b: " + a + "," + b); } double test(double a) { System.out.println("double a: " + a); return a*a; } } class MethodOverloading { public static void main(String args[]) { Overload overload = new Overload(); double result; overload.test(10); overload.test(10, 20); result = overload.test(5.5); System.out.println("Result : " + result); }
Overriding
In a class hierarchy, when a method in a subclass has the same name and type signature as a method in its superclass, then the method in the subclass is said to override the method in the superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version in the superclass will be hidden. If you wish to access the superclass version of an overridden method, you can do so by using super. In order for any particular method to override another correctly : The return type, method name, type and order of arguments must be identical to those of a method in a parent class. The accessibility must not be more restrictive than original method. The method must not throw checked exceptions of classes that are not possible for the original method.
Parental Constructors:
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of the keyword super. This keyword has two general forms. The first calls the superclass constructor. The second is used to access a member of the superclass that has been hidden by a member of a subclass. 1. 2. Constructor call: The call to the superclass constructor must always be the first statement executed inside a subclass constructor. The second form of super acts like this, except that it always refers to the superclass of the subclass in which it is used (e.g. super.member). This form of super is most applicable to situations in which member names of a subclass hide members by the same name in the superclass.
In a class hierarchy, constructors are called in order of derivation from superclass to subclass. Further, since super () must be the first statement executed in a subclass constructor, this order is the same whether or not super () is used. If super () is not used, then the default or no-arguments constructor of each superclass will be executed. This is because a superclass has no knowledge of any subclass, any initialization it needs to perform is separate from and possibly prerequisite to any initialization performed by the subclass. Therefore it must be executed first.
The Cat class overrides the instance method in Animal and hides the class method in Animal. The main method in this class creates an instance of Cat and callstestClassMethod() on the class and testInstanceMethod() on the instance. The output from this program is as follows:
As promised, the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invoked is the one in the subclass.
class Animal{ public void move(){ System.out.println("Animals can move"); } } class Dog extends Animal{ public void move(){ super.move(); // invokes the super class method System.out.println("Dogs can walk and run"); } } public class TestDog{ public static void main(String args[]){ Animal b = new Dog(); // Animal reference but Dog object b.move();//Runs the method in Dog class } }