[go: up one dir, main page]

0% found this document useful (0 votes)
30 views15 pages

Polymorphism: - Overloading - Overriding

This document discusses polymorphism in Java, specifically method overloading and overriding. It defines polymorphism as having multiple forms or shapes. In Java, polymorphism refers to having multiple methods with the same name in the same class. There are two types of polymorphism: overloading, which involves methods with different signatures in the same class; and overriding, which replaces an inherited method with another having the same signature. It provides examples of each.

Uploaded by

Golmei Shaheam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views15 pages

Polymorphism: - Overloading - Overriding

This document discusses polymorphism in Java, specifically method overloading and overriding. It defines polymorphism as having multiple forms or shapes. In Java, polymorphism refers to having multiple methods with the same name in the same class. There are two types of polymorphism: overloading, which involves methods with different signatures in the same class; and overriding, which replaces an inherited method with another having the same signature. It provides examples of each.

Uploaded by

Golmei Shaheam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

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

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

1. Super class reference variable can refer to a sub class object.


2. Super class variable if refers to sub class object can call only
overridden methods.
3. Call to an overridden method is decided by the type of object
referred to.

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;

Exception in thread "main"


java.lang.ClassCastException: B
at override3.main(override3.java:39)
*/
}
}
Examples Overriding

class A A a1 = new B();


{ a1.show() ; // Valid
void show() { …. } // a1.show(10); // Invalid
} //a1.print(); // Invalid
class B extends A
{
void show() { …. }
void show(int x) { … }
void print() { … }
}
When a super class variable points to a sub class object,
then it can only call overridden methods of the sub class.
class A
{
protected void show()
{
System.out.println("Hi");
}
}
class B extends A D:\Java1>javac AB.java
{ AB.java:10: show() in B cannot
void show() override show() in A; attempting
{ to assign weaker
System.out.println("Hi"); access privileges; was protected
} void show()
} ^
1 error
IS THIS METHOD
class A OVERRIDING
{
private void show()
{
System.out.println("Hi");
}
}
NO
class B extends A
{ CODE WILL
int show() COMPILE & RUN
{ SUCESSFULLY
System.out.println("Hi");
return 10;
}
}
class A What’s Wrong Here
{
static int show()
{
System.out.println("class A");
return 0;
}
}

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

You might also like