0% found this document useful (0 votes)
10 views13 pages

Chapter 03 - Polimorphism

Uploaded by

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

Chapter 03 - Polimorphism

Uploaded by

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

OBJECT ORIENTED 1

PROGRAMMING
CS 1032
Chapter – 03
Polymorphism

2
Polymorphism
• Polymorphism in java is a concept
by which we can perform a single
action by different ways.
• More specifically, it is the ability to
redefine methods for derived
classes.
• We can perform polymorphism in
java by method overloading and
method overriding. 3
Overriding
• Declaring a method in sub-class which is already present in parent class
is known as method overriding.
• Redefine the method by keeping the same signature but rewriting the
body.
• The rules for overriding a method:
– method must have same name as in the parent class
– method must have same parameter as in the parent class.
4
– must be IS-A relationship (inheritance).
Overriding Example
• Boy class extends class Human{
Human class. public void eat() {
System.out.println("Human is eating");
• Both the classes have a
}
common method void
}
eat(). class Boy extends Human{
• Boy class is giving its public void eat(){
own implementation to System.out.println("Boy is eating");
the eat() method or in }
other words it is public static void main( String args[]) {
overriding the method Boy obj = new Boy();
eat(). obj.eat(); 5
}
}
Exercise
Bank is a class that provides functionality to get rate of interest. But, rate of interest
varies according to banks. For example, Bank, BOC, PeoplesBank and HNB banks
could provide 2.5, 8.2%, 7.9% and 7.3% Bank rate of interest. Find the Interest values
for the deposit amount for each bank.
Bank
deposit : int
+interestRate() : double

BOC HNB
PeoplesBank
6
+interestRate() : double +interestRate() : double
+interestRate() : double
Overloading and Overriding

• Overriding allows you to modify the behavior of an inherited method to


meet the specific needs of a subclass
• Overloading allows you to use the same method name to implement
different (but related) functionalities

7
Overloading vs Overriding

8
class Animal{
void eat(){
System.out.println("eating..."); class TestPoly{
} } public static void main(String[] args){
class Dog extends Animal{
void eat(){ Animal a;
System.out.println("eating bread..."); a=new Dog();
} } a.eat();
class Cat extends Animal{ a=new Cat();
void eat(){ a.eat();
System.out.println("eating rat..."); a=new Lion();
} } a.eat();
class Lion extends Animal{ }
void eat(){ }
System.out.println("eating meat..."); 9

} }
Exercise

10
11
Reference

• Static Binding
• Dynamic Binding

12
Chapter 04

13

You might also like