Polymorphism, Mol, or
Polymorphism, Mol, or
class A
{
int x=10,y=20;
public A()
{
//Constructor of super class
}
void display1(){………}
}
class B extends A
{
public B()
{
super();
//calls constructor of super class A.
}
void display2()
{
}
}
To access variables “x” and y of superclass can be access in subclass by
using super keyword.
The word „Polymorphism: is derived form two Greek words „poly„ which
means many and „morphos„ which means forms. So, polymorphism means
the ability to take many forms. In java a variable,an object or method can
exist in different forms ,thus performing various tasks depending on the
context.Because same variable or method can perform different tasks ,the
programmer has the advantage of writing flexible code.
1. We need to remember single name instead of multiple names of the methods that
perform similar type of operations. This helps in reducing the complexity of making
large programs.
2. It perform similar tasks can make complex programs more readable, understandable
and easy to debug.
3. Maintainability of the program becomes easier.
Runtime polymorphism :-
The polymorphism exhibited at runtime is called dynamic polymorphism.
This means when a method is called ,the method call is bound to the
method body at the time of running the program dynamically. In this case
java compiler doesn‟t know which method is called at the time of
compilation. Only ,JVM knows at runtime at which method is to be executed
basing on runtime object. Hence this is also called runtime binding (or) late
binding. To achieve runtime polymorphism in java by using the concept of
method overriding.
Polymorphism can be achieved in two ways.
1) Method Overloading
2) Method Overriding.
Method Overloading :-
Method Overloading is a feature that allows a
class to have more than one method having
the same name, if their argument lists are
different. Method overloading increases the
readability of the program. By using Method
Overloading we can achieve static
Polymorphism.
In method overloading while writing the method signature we have to follow the
following 3 rules.
Rules:-
Three ways to overload a method
In order to overload a method, the argument lists of the methods must differ
in either of these:
1. Changing the Number of parameters(Parameters list).
For example: This is a valid case of overloading
void add(int, int) { }
void add(int, int, int).{ }
2. By changing Data type of parameters.
Example:-
void add(int, int) { }
void add(int, float){ }
3. By changing the sequence of Data type of parameters.
Example:-
void add(int, float){ ……. }
void add(float, int){ ………. }
Method Overriding :-
Re Defining the non static method of a super class in subclass with same
signature is called Method overriding. Overriding is a feature that allows a
subclass or child class to provide a specific implementation of a method that is
already provided by one of its super-classes or parent classes. When a method in a
subclass has the same name, same parameters or signature, and same return
type(or sub-type) as a method in its super-class, then the method in the subclass
is said to override the method in the super-class.
Usage of Java Method Overriding
• Method overriding is one of the way by which java achieve Run Time
Polymorphism.
Rules for Method Overriding :-
1. The argument list should be exactly the same as that of the overridden method.
2. The return type should be the same or a subtype of the return type declared in
the original overridden method in the superclass.
3. The access modifier for an overriding method can allow more, but not less, access
than the overridden method. For example, a protected instance method in the
super-class can be made public, but not private, in the subclass. Doing so, will
generate compile-time error.
4. Final methods can not be overridden : If we don’t want a method to be
overridden, we declare it as final. Private methods can not be overridden
5. Instance methods can be overridden only if they are inherited by the subclass.
6. Static methods can not be overridden(Method Overriding vs Method
Hiding) : When you define a static method with same signature as a static
method in base class, it is known as method hiding.
7. Invoking overridden method from sub-class : We can call parent class
method in overriding method using super keyword.
9.We can not override constructor as parent and child class can never have
constructor with same name(Constructor name must always be same as Class name).
10.If the super-class overridden method does not throw an exception, subclass
overriding method can only throws the unchecked exception, throwing checked
exception will lead to compile-time error.
11.If the super-class overridden method does throws an checked exception, subclass
overriding method can only throw same, subclass exception. Throwing parent
exception in Exception hierarchy will lead to compile time error
Method Hiding
Re Defining the static method of a super class in subclass with same signature is
called Method Hiding. When you define a static method with same signature as
a static method in base class, it is known as method hiding. When a child class
defines a static method with the same signature as a static method in the parent
class, then the child's method hides the one in the parent class.
Differences between Method Overloading and Method Overriding
Differences between Method Overloading and Method hiding