Home
Overloading methods
1.They appear in the same class or a subclass
2.They have the same name but,
have different parameter lists, and can have different return types.
An example of an overloaded method is print[] in the java.io.PrintStream class
Overriding methods
It allows a subclass to re-define a method it inherits from it's superclass
overriding methods:
1. It appears in subclasses
2. They have the same name as a superclass method
3. They have the same parameter list as a
superclass method
4. They have the same return type as as a superclass method
5. They have the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
·If the superclass method is public, the overriding method must be public
·If the superclass method is protected, the overriding
method may be protected or public
·If the superclass method is package, the overriding method may be packagage, protected, or public
·If the superclass methods is private, it is not inherited and overriding is not an issue
What is overloading in java ?
In Java it is possible to define two or more methods within the same class that share the same name, as long as their parameter declarations are
different. When this is the case,the methods are said to be overloaded, and the process is referred to as method
overloading. Method overloading is one of the ways that Java implements polymorphism.
What is Overriding in java ?
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.
a] In overloading, there is a relationship between methods available in the same class whereas in overriding, there is relationship between a superclass method and subclass method.
b] Overloading does not block inheritance from the superclass whereas overriding blocks inheritance from the superclass.
c] In overloading, separate methods share the same name whereas in overriding, subclass method replaces the superclass.
d] Overloading must have different method signatures whereas overriding must have same signature
Overloading
- If two [or more] methods of a class [whether both declared in the same class, or both inherited by a class, or one declared and one inherited] have the same name but different signatures, then the method name is said to be overloaded.
- The signature of a method consists of the name of the method and the number and types of formal parameters in particular order. A class must not declare two methods with the same signature, or a compile-time error occurs.
- Only the method name is reused in overloading, so method overloading is actually method name overloading.
- Overloaded methods may have arguments with different types and order of the arguments may be different.
- Overloaded methods are not required to have the same return type or the list of thrown exceptions.
- Overloading is particularly used while implementing several methods that implement similar behavior but for different data types.
- Overloaded methods are independent methods of a class and can call each other just like any other method.
- Constructor
Overloading
- Constructors can also be overloaded in similar fashion, as they are also methods.
- Java has some classes with overloaded constructors. For example, wrapper class for int has two overloaded constructors. One constructor takes int as argument and another takes string argument.Integer aNumber = new Integer[2]; Integer anotherNumber = new Integer["2005"];
- One constructor can call another overloaded constructor of the same class by using this keyword.
- One constructor can call constructor of its super class by using the super keyword.
Overriding
- When a class defines a method with same method name, argument types, argument order and return type as a method in its super class, its called method overriding.
- The method in the super class is said to be overridden by the method in the subclass.
- Overriding method actually replaces the behavior in super class for a subclass.
- Rules for overriding
- Methods overriding cannot be declared more private than the super class method.
- Any exceptions declared in overriding method must be of the same type as those thrown by the super class, or a subclass of that type.
- Methods declared as final cannot be overridden.
- An overriding method can be declared as final as the keyword final only suggests that this method cannot be further overridden.
- Methods declared as private cannot be overridden as they are not visible outside the class.
- Overriding method can call the overridden method [just like any other method] in its super class with keyword super.
The call super.method[] will invoke the method of immediate super class. - Though keyword super is used to refer super class, method call super.super.method[] is invalid.
Difference between overloading and overriding.
| ||
'Is a', 'has a' relationship The Generalization [Inheritance] and 'is-a' relationship Generalization is the result of abstracting the common attributes and behavior from a group of closely related classes and moving them into a common super class. Specialization is the inverse of generalization. A common test for generalization is the IS-A test. Generalization may also look for following phrases in problem definition to identify the inheritance.
For example, if part of problem definition says 'Dog IS AN animal', implementation will more likely have Animal class and Dog class will be the subclass of the Animal class. The Composition and 'has-a' relationship
Variables are shadowed and methods are overridden.When a class member variable is accessed through an object reference, the variable to be selected depends on the declared class of the object reference variable. In any method call, actual method to invoke is determined by the 'Class' [or type] of an object on which the method is called and not on the type of variable holding the object reference. In addition, this is decided at runtime. |