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

Lecture 9

This document provides an overview of Polymorphism in Object-Oriented Programming with Java, detailing its two main types: Compile-time and Runtime Polymorphism. It explains concepts such as method overloading and method overriding, highlighting how they facilitate polymorphic behavior in Java. The document emphasizes the importance of dynamic method dispatch and the rules governing method overriding for achieving flexible and maintainable code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views17 pages

Lecture 9

This document provides an overview of Polymorphism in Object-Oriented Programming with Java, detailing its two main types: Compile-time and Runtime Polymorphism. It explains concepts such as method overloading and method overriding, highlighting how they facilitate polymorphic behavior in Java. The document emphasizes the importance of dynamic method dispatch and the rules governing method overriding for achieving flexible and maintainable code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Object Oriented Programming with Java

(Subject Code: BCS-403)

Unit 1
Lecture 9

Department of Computer Science ,ABES Engineering College


Lecture 9
• Polymorphism
• Overriding
• Overloading

Department of Computer Science ,AB


ES Engineering College
Polymorphism
• Polymorphism is considered one of the
important features of Object-Oriented
Programming.
• Polymorphism allows us to perform a single
action in different ways. In other words,
polymorphism allows you to define one
interface and have multiple implementations.
• The word “poly” means many and “morphs”
means forms, So it means many forms.
Types of Java Polymorphism
In Java Polymorphism is mainly divided into two
types
 Compile-time Polymorphism
 Runtime Polymorphism
Compile-Time Polymorphism in Java
It is also known as static polymorphism. This
type of polymorphism is achieved by function
overloading or operator overloading.
Note: Java doesn’t support the Operator
Overloading.
Method Overloading
When there are multiple functions with the
same name but different parameters then these
functions are said to be overloaded.
Functions can be overloaded by changes in the
number of arguments or/and a change in the
type of arguments.
class Overloading{
// Method with 2 integer parameters
static int Multiply(int a, int b)
{
// Returns product of integer numbers
return a * b;
}
// Method 2
// With same name but with 2 double parameters
static double Multiply(double a, double b)
{
// Returns product of double numbers
return a * b;
}
}
Java Method Overloading

class OverloadingExample
{
static int add(int a,int b)
{
return a+b;
}
static int add(int a,int b,int c)
{
return a+b+c;
}
}
Runtime Polymorphism
It is also known as Dynamic Method Dispatch. It
is a process in which a function call to the
overridden method is resolved at Runtime. This
type of polymorphism is achieved by Method
Overriding.
Dynamic method dispatch
Dynamic method dispatch is a feature in Java that
allows the JVM to determine at runtime which
version of a method to invoke, based on the type
of the object that the method is being called on.
This feature enables polymorphism, which is one
of the fundamental principles of object-oriented
programming.

Here's how dynamic method dispatch works in


Java:
1.Inheritance and Method Overriding:
Dynamic method dispatch is closely tied to inheritance and
method overriding. When a subclass overrides a method
that is also present in its superclass, the subclass provides
a specific implementation of that method.

2.Base Class Reference: In dynamic method


dispatch, you can use a reference variable of a superclass
type to refer to an object of a subclass type.
Java
superclass reference = new
subclass();
3.Method Invocation: When you call a method on
the reference variable, the JVM determines at runtime
which version of the method to execute based on the
actual type of the object that the reference variable
points to.
Java
reference.method(); // Invokes the method from the
subclass
reference.method(); // Invokes the method from the subclass

4.Actual Object Type: The method invoked is based on


the actual type of the object that the reference variable points to,
not the declared type of the reference variable.
java
superclass reference = new subclass();

reference.method(); // Invokes the method from the subclass, not

the superclass Invokes


3. the method from the subclass, not the superclass

5.Late Binding: Dynamic method dispatch involves late binding,


where the method to be invoked is determined dynamically at
runtime based on the actual type of the object.

6.Polymorphism: Dynamic method dispatch enables polymorphism,


allowing subclasses to provide their own implementations of methods
defined in the superclass and allowing for flexible and extensible
code.
Dynamic method dispatch is a powerful mechanism in Java that
allows for code reusability, flexibility, and polymorphic behavior,
enabling more modular and maintainable code structures. It is a key
feature of object-oriented programming that facilitates code
abstraction and encapsulation.
Method Overriding in Java
• If subclass (child class) has the same method
as declared in the parent class, it is known
as method overriding in java.
• Method overriding is used to provide specific
implementation of a method that is already
provided by its super class.
• Method overriding is used for runtime
polymorphism.
Rules for Java Method Overriding
• method must have same name as in the
parent class
• method must have same parameter as in the
parent class.
• must be IS-A relationship (inheritance).
class Vehicle{
void run()
{System.out.println("Vehicle is running");}
}
class Bike extends Vehicle{ void run()
{System.out.println(“Bike is running");}

public static void main(String args[]){


Bike obj = new Bike();
obj.run();
}
}

You might also like