Polymorph Is M
Polymorph Is M
Polymorphism
Polymorphism means many (poly) shapes
(morph)
In Java, polymorphism refers to the fact that
you can have multiple methods with the
same name in the same class
There are two kinds of polymorphism:
Overloading
Two or more methods with different signatures
Overriding
Replacing an inherited method with another having the
same signature
Signatures
In any programming language, a signature is what
distinguishes one function or method from another
In C, every function has to have a different name
In Java, two methods have to differ in their names
or in the number or types of their parameters
foo(int i) and foo(int i, int j) are different
foo(int i) and foo(int k) are the same
foo(int i, double d) and foo(double d, int i) are different
Method Overloading
Method Overloading is the process of using
methods having same names with different
signature.
A method can be overloaded in the same class
or in a subclass.
Which overloaded method to call is based on
reference type and determined at compile time.
It is also called as compile time polymorphism.
Automatic type conversions apply to
overloading.
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.2);
System.out.println("Result of ob.test(123.2): " + result);
}
}
No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test (123.2): 15178.24
OVERRIDING
The process of defining a method in child class with the same
name & signature as that of a method already present in parent
class.
If a class inherits a method from its super class, then
there is a chance to override the method provided that it
is not marked final.
OVERRIDING EXAMPLE 1
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and run");
}
}
class TestDog{
public static void main(String args[]){
Animal a = new Animal();
// Animal reference and object
Animals can move
class Animal{
public void move(){
System.out.println("Animals can move");
}
}
class Dog extends Animal{
public void move(){
System.out.println("Dogs can walk and
run");
}
}
class Cat extends Dog{
public void move(){
System.out.println("Cats can jump,walk
and run");
}
}
class Rat extends Cat{
public void move(){
System.out.println("Rats can
dance,jump,walk and run");
}
}
class TestDog{
public static void main(String
args[]){
Animal a = new Animal();
Animal reference and object
b.move();
//Runs the method in Dog class
}
}
Output
OVERRIDING EXAMPLE 2
1. class Animal{
2. }
3. class Dog extends Animal{
4.
public void bark(){
5.
System.out.println("Dogs can bark");
6.
}
7. }
8. public class TestDog{
9.
public static void main(String args[]){
10.
Animal a = new Animal(); // Animal reference
and object
11.
Animal b = new Dog(); // Animal reference but
Dog object
12.
b.bark();
Result (ERROR):
13.
}
14.}
TestDog.java:12: cannot find
symbol
symbol : method bark()
location: class Animal b.bark();