[go: up one dir, main page]

0% found this document useful (0 votes)
16 views18 pages

Week 8

Uploaded by

MG Master Gaming
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)
16 views18 pages

Week 8

Uploaded by

MG Master Gaming
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/ 18

Polymorphism

Instructor Name: Muhammad Khalid


Department of Computer Science, HITEC University Taxila - Pakistan
Polymorphism
2

◻ Polymorphism is derived from two Greek words, “poly” and


“morph”, which mean “many” and “forms”, respectively. Hence,
polymorphism meaning in Java refers to the ability of objects to take
on many forms. In other words, it allows different objects to respond
to the same method call in multiple ways.
◻ Polymorphism allows coders to write code that can work with objects
of multiple classes in a generic way without knowing the specific
class of each object.
Types
3

1. Compile time polymorphism


- Static polymorphism

- Achieve by Method Overloading

2. Run Time Polymorphism


- Dynamic polymorphism

- Achieve by Method Overriding


Method overloading and Overriding
4

Method overloading Method Overriding


◻ Same name ◻ Same name
◻ Same class ◻ Different class (Inheritance)
◻ Different arguments ◻ Same arguments
No of args No of args
Sequence of args Sequence of args
Type of args Type of args
Compile-time Polymorphism (Concept)
5

• Compile-time polymorphism, often referred to as static


polymorphism or method overloading, is one of the fundamental
aspects of Java development.

• Multiple methods having the same name but different


parameters.

• can be with a different number of arguments or different data


types of arguments

• The appropriate method is chosen by the compiler based on the


parameters provided at compile time.
Compile-time Polymorphism (Program)
6
Compile-time Polymorphism (Program using
scanner class)
7
r; e
ann
.Sc
java ude
.util
Incl
ort
imp
Run-time Polymorphism: Method Overriding (Concept)
8

If subclass (child class) has the same method as declared in the


parent class, it is known as method overriding in Java.

In other words, If a subclass provides the specific


implementation of a (general) method that has been declared
by its parent class, it is known as method overriding.

In Java, we can override methods only, not the variables(data members), so runtime polymorphism
cannot be achieved by data members.
Run-time Polymorphism: Method Overriding (Concept)
9

• Method overriding is used to provide the specific


implementation of a method which is already provided
by its superclass.

• Method overriding is used for runtime polymorphism.


Java Runtime Polymorphism Example: Animal Sounds
10

class Animal { class Elephant extends Animal {


void makeSound() { void makeSound() {
System.out.println("Elephant trumpeting...");
System.out.println("Animal making a sound...");
}
} }
}
class TestPolymorphism2 {
class Dog extends Animal { public static void main(String args[]) {
void makeSound() { Animal animal;
System.out.println("Dog barking...");
} animal = new Dog();
animal.makeSound();
}
animal = new Cat();
class Cat extends Animal { animal.makeSound();
void makeSound() {
System.out.println("Cat meowing..."); animal = new Elephant();
} animal.makeSound();
} }}
Rules for Java Method Overriding
11

• The method must have the same name as in the


parent class

• The method must have the same parameter as in the


parent class.

• There must be inheritance.


Method Overriding (Syntax)
12

class Vehicle
{
void run()
{}
}
class Bike2 extends Vehicle
{
void run()
{}
}
Method Overriding (Example)
13

//Java Program to illustrate the use of Java Method


Overriding
//Creating a parent class.
class Vehicle
{ public static void main(String args[])
//defining a method
{
void run()
{System.out.println(“Any type of Vehicle is running");} Bike2 obj = new Bike2();//creating object
} obj.run();//calling method
//Creating a child class }
class Bike2 extends Vehicle
{
//defining the same method as in the parent class
void run()
{System.out.println("Bike is running safely");}
}
Method Overriding (Example)
14

class Human
{
//Overridden method
public void eat()
{
System.out.println("Human is eating");
}
public static void main( String args[])
}
class Boy extends Human
{
{ Boy obj = new Boy();
//Overriding method //This will call the child class version of eat()
public void eat(){ obj.eat();
System.out.println("Boy is eating"); }
}
Method Overriding (Example)
15
class Bank
{
int getRateOfInterest() {return 0;}
}
//Creating child classes.
class HBL extends Bank
{
int getRateOfInterest() {return 8;}
}
class UBL extends Bank
{
int getRateOfInterest() {return 7;}
}
class ABL extends Bank
{
int getRateOfInterest() {return 9;}
}
Method Overriding (Example)
16

class Test2
{
public static void main(String args[])
{
HBL h=new HBL();
UBL u=new UBL();
ABL a=new ABL();
System.out.println(“HBL Rate of Interest: "+h.getRateOfInterest());
System.out.println(“UBL Rate of Interest: "+u.getRateOfInterest());
System.out.println(“ABL Rate of Interest: "+a.getRateOfInterest());
}
}

Java method overriding is mostly used in Runtime Polymorphism which


we will learn in next lecture.
Method Overriding
17

Can we override static method?

Why can we not override static method?

Can we override java main method?


Method Overriding
18

Can we override a static method?


No, a static method cannot be overridden.

Why can not we override a static method?

It is because the static method is bound with class whereas the instance method
is bound with an object. Also, static methods are resolved at compile-time based
on the class in which they are defined, rather than being dynamically resolved,
the concept of method overriding does not apply to static methods in Java

Can we override Java main method?

No, because the main is a static method.

You might also like