Polymorphism: - Overloading - Overriding
Polymorphism: - Overloading - Overriding
1
Overloading
class Test {
public static void main(String args[]) {
myPrint(5);
myPrint(5.0);
}
static void myPrint(int i) {
System.out.println("int i = " + i);
}
static void myPrint(double d) { // same name, different parameters
System.out.println("double d = " + d);
}
}
int i = 5
double d = 5.0
2
METHOD OVERRIDING
1. Sub class can override the methods defined by the super
class.
2. Overridden Methods in the sub classes should have same
name, same signature , same return type and may have
either the same or higher scope than super class method.
3. Java implements Run Time Polymorphism/ Dynamic
Method Dispatch by Method Overriding. [Late Binding]
4. Call to Overridden Methods is Resolved at Run Time.
5. Call to a overridden method is not decided by the type of
reference variable Rather by the type of the object where
reference variable is pointing.
6. While Overriding a Method, the sub class should assign
either same or higher access level than super class
method.
class A EXAMPLE METHOD OVERRIDING
{
void show()
{
System.out.println("This is show() in A");
}
} B class overrides show()
class B extends A
{
method from super class A
void show()
{
System.out.println("This is show() in B");
}
} class override
{
public static void main(String args[])
Call to {
// super class reference variable
show() of A // can point to sub class object
class A a1 = new A();
Call to a1.show();
a1 = new B();
show() of B a1.show();
class }
class A Is this Method class override1
{ Overriding {
void show(int a) public static void main(String
{ NO args[])
System.out.println("Hello This is {
show() in A"); /*
} A a1 = new B();
} a1.show(); */
class B extends A
{ A a1 = new A();
void show() a1.show(10);
{
System.out.println("Hello This is B b1 = new B();
show() in B"); b1.show(10);
} b1.show(); }
}
OUTPUT
Hello This is show() in A
Hello This is show() in A
Hello This is show() in B
Dynamic Method Dispatch
A a1 = new B(); A
a1.show(); // call to show() of B
a1 = new C();
a1.show(); // call to show() of C B C D
a1 = new D();
a1.show(); // call to show() of D
Assume show() Method is
Overridden by sub classes
DYNAMIC METHOD DISPATCH
class A class C extends A
{ {
void show() void show()
{ {
System.out.println("Hello This is System.out.println("Hello This is
show() in A"); show() in C");
} }
} }
class B extends A class D extends A
{ {
void show() void show()
{ {
System.out.println("Hello This is System.out.println("Hello This is
show() in B"); show() in D");
} }
} }
CONTINUED…..
class override2
{
public static void main(String args[])
{
A a1 = new A();
a1.show();
a1 = new B(); Hello This is show() in A
a1.show();
a1 = new C(); Hello This is show() in B
a1.show();
a1 = new D(); Hello This is show() in C
a1.show();
} Hello This is show() in D
}
class override3
{
public static void main(String args[])
{
A a1 = new B();
B b1 = (B) a1;
/*
A a1 = new B();
C c1 = (C) a1;
class B extends A
{
Static void show()
{ sample.java:12: show() in B cannot override
System.out.println("class B"); show() in A; overridden method is st
} atic
void show()
} ^
1 error
class A What’s Wrong Here
{
static int show()
{
System.out.println("class A"); Nothing
return 0; The code will compile and run
} successfully.
}
Method Hiding
class B extends A
{ B class Hides show() method
static int show() from super class A
{
System.out.println("class B");
}
}
class A
{
static int show() class TestAB
{ {
System.out.println("SHOW METHOD OF CLASS A");
public static void main(String args[])
return 0;
}// End of show() Method {
}// End of class A A a1 = new B();
System.out.println(a1.show());
class B extends A B b1 = new B();
{ b1.show();
static int show() }
{
}
System.out.println("SHOW METHOD OF CLASS B");
return 0;
}// End of show() Method
}// End of class B
E:\Java Programs>java TestAB
SHOW METHOD OF CLASS A
0
SHOW METHOD OF CLASS B