Polymorphism
Polymorphism is the ability of an object to take on many
forms. In programming languages polymorphism is the capability of an action or
method to do different things based on the object that it is acting upon.
Polymorphism is the capability of an action or method to do
different things based on the object that it is acting upon. In other words,
polymorphism allows you define one interface and have multiple implementation.
This is one of the basic principles of object oriented programming.
Method
Overloading
In java method overloading means creating more than one
method with same name but with different signature.
i.e., If name of the
method remains common but the number and type of parameters are different, then
it is called method overloading in Java.
Overloaded methods
may have different return types. Overloaded Methods: Here, these methods have
same return type and name, but the signature(i.e., the parameter it takes are
different).
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.
Example
public class MethodOver
{
int n1;
int n2;
MethodOver()
{
n1 = 10;
n2 = 20;
}
void square()
{
System.out.println("The Square is " + n1 * n2);
}
void square(int p1)
{
n1 = p1;
System.out.println("The Square is " + n1 * n2);
}
void square(int p1, int p2)
{
n1 = p1;
n2 = p2;
System.out.println("The Square is " + n1 * n2);
}
public static void main(String args[])
{
MethodOver obj1 = new MethodOver();
obj1.square(); //call non parameterise method
obj1.square(4); //call method which has 1 argument
obj1.square(7,8); //call method which has 2 argument
}
}
Output
The Square is 200 The Square is 80 The Square is 56 You can
see that here we have 3 square methods with different argument. Its called
method overloading.
Method Overiding
Method overriding in Java is a concept based on polymorphism
OOPS concept which allows programmer to create two methods with same name and
method signature on interface and its various implementation and actual method
is called at runtime depending upon type of object at runtime.
Method overriding
allows you to write flexible and extensible code in Java because you can
introduce new functionality with minimal code change.
Method overriding is
different than method overloading in Java which we have discussed above. In
method overloading, Only name of two overloaded methods are same but method
signature must be different while in method overriding, method signature must
be same. method overriding represent true polymorphic behaviour, where only
name needs to be same underlying method logic can be different.
Here we will see
-
What
is method overriding in Java?
-
Rules to override method in Java?
-
Example
of How to override method in Java.
Some Key Points
1) First and most important rule regarding method overriding
in Java is that you can only override method in sub class. You can not override
method in same class.
2) Second important rule of method overriding in Java that name
and signature of method must be same in Super class and Sub class or in
interface and its implementation.
3) Third rule to override method in Java is that overriding
method can not reduce accessibility of overridden method in Java. For example
if overridden method is public than overriding method can not be protected,
private or package-private; But opposite is true overriding method can increase
accessibility of method in Java, i.e. if overridden method is protected than
overriding method can be protected or public.
4) Another worth noting rule of method overriding in Java is
that overriding method can not throw checked Exception which is higher in
hierarchy than overridden method. Which means if overridden method throws
IOException than overriding method can not throw java.lang.Exception in its
throws clause because java.lang.Exception comes higher than IOException in
Exception hierarchy. This rule doesn't apply to RuntimeException in Java, which
is not even need to be declared in throws clause in Java.
5) You can not override private, static and final method in
Java. private and static method are bonded during compile time using static
binding in Java and doesn't resolve during runtime. overriding final method in
Java is compile time error. Though private and static method can be hidden if
you declare another method with same and signature in sub class.
6) Overridden method is called using dynamic binding in Java
at runtime based upon type of Object. As shown in following example of method
overloading.
7) If you are extending abstract class or implementing
interface than you need to override all abstract method unless your class is
not abstract. abstract method can only be used by using method overriding.
8) Always use @Override annotation while overriding method
in Java. Though this is not rule but its one of the best Java coding practice
to follow. From Java 6 you can use @Override annotation on method inherited
from interface as well.
Method
Overriding Example in Java
Now we know what is method overriding in Java and rules of
method overriding, It's time to see an example of how to override method in
Java. In this example we have used Runnable interface which has an abstract
run() method.
We have two class
Task and Periodic Task which implements Runnable interface and override run
method. For the purpose of demonstrating how method overriding works in Java we
are calling run() method in same thread, which you should not, see difference
between run and start method to know why. Because run() is overridden in two
separate class, call to run() method will be resolved during runtime depending
upon type of Object.
Example
/**
*
* Java program to demonstrate how to override method in
Java.
* Overridden method are resolved during runtime based upon
type of object
*
*/
public class CollectionTest {
public static void main(String args[]) {
Runnable task = new Task();
task.run(); //call overridden method in Task
task = new PeriodicTask();
task.run(); //calls overridden method in PeriodicTas
}
}
class Task implements Runnable{
@Override
public void run() {
System.out.println("Run method overridden in Task
class");
}
}
class PeriodicTask extends Task{
@Override
public void run() {
System.err.println("overridden method run() in
PeriodicTask class");
}
}
Output:
Run method overridden in Task class
overridden method run() in PeriodicTask class
That's all on What is method overriding in Java, Rules of
method overriding in Java and an example of How to override method in Java. In
summary remember to override all abstract method while extending form abstract
class or implementing interface. Overridden method are also slower as compared
to static and final methods because of dynamic binding but it provides you
flexibility, many popular Object oriented design principles are based upon
method overriding in Java.