0% found this document useful (0 votes)
128 views

Java Technical Write-UP: Topic: - Method Overloading & Method Overriding

Method overloading allows methods to have the same name but different parameters. When a method in a subclass has the same name and signature as a method in its superclass, the subclass method overrides the superclass method. Constructors are called from the superclass to subclass. The super keyword calls the superclass constructor or accesses hidden superclass members. Overriding changes the subclass method implementation while hiding uses the superclass version depending on the reference type.

Uploaded by

Ankur Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views

Java Technical Write-UP: Topic: - Method Overloading & Method Overriding

Method overloading allows methods to have the same name but different parameters. When a method in a subclass has the same name and signature as a method in its superclass, the subclass method overrides the superclass method. Constructors are called from the superclass to subclass. The super keyword calls the superclass constructor or accesses hidden superclass members. Overriding changes the subclass method implementation while hiding uses the superclass version depending on the reference type.

Uploaded by

Ankur Jain
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

JAVA TECHNICAL WRITEUP

TOPIC :- METHOD OVERLOADING & METHOD OVERRIDING

SUBMITTED BYPRERNA SINGH A1000710018 MCA 4-A

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); }

Output will be displayed as:

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.

Overriding and Hiding Methods


Class Methods If a subclass defines a class method with the same signature as a class method in the superclass, the method in the subclass hides the one in the superclass. The distinction between hiding and overriding has important implications. The version of the overridden method that gets invoked is the one in the subclass. The version of the hidden method that gets invoked depends on whether it is invoked from the superclass or the subclass. Let's look at an example that contains two classes. The first is Animal, which contains one instance method and one class method:
public class Animal { public static void testClassMethod() { System.out.println("The class" + " method in Animal."); } public void testInstanceMethod() { System.out.println("The instance " + " method in Animal."); } } The second class, a subclass of Animal, is called Cat: public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method" + " in Cat."); } public void testInstanceMethod() { System.out.println("The instance method" + " in Cat."); } public static void main(String[] args) { Cat myCat = new Cat(); Animal myAnimal = myCat; Animal.testClassMethod(); myAnimal.testInstanceMethod(); } }

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:

The class method in Animal. The instance method in Cat.

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.

Rules for method overriding:


The argument list should be exactly the same as that of the overridden method. The return type should be the same or a subtype of the return type declared in the original overridden method in the super class. The access level cannot be more restrictive than the overridden method's access level. For example: if the super class method is declared public then the overridding method in the sub class cannot be either private or public. However the access level can be less restrictive than the overridden method's access level. Instance methods can be overridden only if they are inherited by the subclass. A method declared final cannot be overridden. A method declared static cannot be overridden but can be re-declared. If a method cannot be inherited then it cannot be overridden. A subclass in a different package can only override the non-final methods declared public or protected. An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method. Constructors cannot be overridden.

Using the super keyword:


When invoking a superclass version of an overridden method the super keyword is used.

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 } }

This would produce following result:

Animals can move Dogs can walk and run

You might also like