0% found this document useful (0 votes)
43 views33 pages

OOP-Chapter 4

The document discusses polymorphism in object-oriented programming. Polymorphism allows the same action to be performed in different ways and is achieved through inheritance and method overriding. There are two types of polymorphism: compile-time polymorphism using method overloading, and runtime polymorphism using method overriding.

Uploaded by

beshahashenafe20
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)
43 views33 pages

OOP-Chapter 4

The document discusses polymorphism in object-oriented programming. Polymorphism allows the same action to be performed in different ways and is achieved through inheritance and method overriding. There are two types of polymorphism: compile-time polymorphism using method overloading, and runtime polymorphism using method overriding.

Uploaded by

beshahashenafe20
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/ 33

1

POLYMORPHISM

4/6/2024 OOP- CH-4- Polymorphism


Contents
1. Introduction
2. Demonstrating polymorphism concepts with super
classes and subclasses
3. Abstract classes and methods
4. Multiple inheritance and interfaces

4/6/2024
Contents
1. Introduction
2. Demonstrating polymorphism concepts with super
classes and subclasses
3. Abstract classes and methods
4. Multiple inheritance and interfaces

4/6/2024
Introduction
4

 Polymorphism in Java is the ability of an object to


take many forms.
 Itallows us to perform the same action in many
different ways.
 Polymorphism occurs when there is inheritance,
i.e. there are many classes that are related to each
other.
 Inheritance is a powerful feature in Java. Inheritance
lets one class acquire the properties and attributes of
another class.

4/6/2024
5

 Polymorphism in java allows us to use these


inherited properties to perform different tasks.
 Thus, allowing us to achieve the same action in many
different ways.
 There are two types of polymorphism in java:
 Compile-time polymorphism and
 Runtime polymorphism.

4/6/2024
6

 Compile-time polymorphism
 Itis also known as Static Polymorphism.
 In this process, the call to the method is resolved at
compile-time.
 Compile-Time polymorphism is achieved
through Method Overloading.
◼ This type of polymorphism can also be achieved
through Operator Overloading. However, Java does not
support Operator Overloading.

4/6/2024
7

 Method Overloading is when a class has multiple


methods with the same name, but the number, types
and order of parameters and the return type of the
methods are different.
 Java allows the user freedom to use the same name for
various functions as long as it can distinguish between
them by the type and number of parameters.

4/6/2024
8

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);
}
}
4/6/2024
9

 Runtime polymorphism
 Itis also known as Dynamic Binding or Dynamic
Method Dispatch.
 In this process, the call to an overridden method is
resolved dynamically at runtime rather than at compile-
time.
 Runtime polymorphism is achieved through Method
Overriding.

4/6/2024
10

 Method Overriding is done when a child or a subclass


has a method with the same name, parameters and
return type as the parent or the superclass, then that
function overrides the function in the superclass.
◼ In simpler terms, if the subclass provides its definition to a
method already present in the superclass, then that function
in the base class is said to be overridden.
 It
should be noted that runtime polymorphism can only
be achieved through methods and not data
members.

4/6/2024
11

class Animal {
public static void main(String args[]) {
Animal animal = new Animal();
Dog dog = new Dog();
animal.print();
dog.print();
}
void print() {
System.out.println("Superclass Animal");
}
}
public class Dog extends Animal {
void print() {
System.out.println("Subclass Dog");
}
}
4/6/2024
Contents
1. Introduction
2. Demonstrating polymorphism concepts with super
classes and subclasses
3. Abstract classes and methods
4. Multiple inheritance and interfaces

4/6/2024
Inheritance v/s Polymorphism
13

 Inheritance allows programs to model relationships


in the real world
 Ifthe program follows the model it may be easier to
write
 Inheritance allows code reuse
◼ Complete programs faster (especially large programs)
 Polymorphism allows code reuse in another way.
 Inheritance and polymorphism allow programmers
to create generic algorithms.

4/6/2024
Demonstrating polymorphic behavior
14

 A superclass object cannot be treated as a subclass object,


because a superclass object is not an object of any of its
subclasses.
 The is-a relationship applies only up the hierarchy from a
subclass to its direct (and indirect) super classes, and not down
the hierarchy.
 The Java compiler does allow the assignment of a superclass
reference to a subclass variable if you explicitly cast the
superclass reference to the subclass type
 A technique known as downcasting that enables a program to
invoke subclass methods that are not in the superclass.

4/6/2024
Up-casting v/s Down-casting
15

4/6/2024

16

 Polymorphism is the concept of allowing a


reference to a subclass to be used in a place where
a reference to its parent’s class would be
acceptable.
 An easy and generic example is as follows:
Vehicle v = new Car();
Superclass Subclass

 Since every class extends Object, any object can


be stored in an Object variable.

4/6/2024
17

 In the bird and parrot example, consider a bird method:


static void printCall(Bird bird) {
System.out.println(bird.call);
} Bird b = new Parrot();
printBirdCall(b)
Parrot p = new Parrot();
printBirdCall(p)

 Generic: printBirdCall expects a Bird, but any type of


bird is OK.
 Cannot write Parrot p = new Bird(); //there’s not enough info!
 However, without casting, b can only use bird methods.

4/6/2024
Casting and instanceof
 If we know that b is a Parrot, we can cast it
and use Parrot methods:
((Parrot)b).speak()
 At runtime, if b is just a Bird, the JVM will throw
a ClassCastException.
 To test this, use instanceof:
if (b instanceof Parrot) {
((Parrot)b).speak()) }
Contents
1. Introduction
2. Demonstrating polymorphism concepts with super
classes and subclasses
3. Abstract classes and methods
4. Multiple inheritance and interfaces

4/6/2024
Abstract class
20

 Abstract classes are classes with a generic concept,


not related to a specific class.
 Abstract classes define partial behavior and leave the
rest for the subclasses to provide.
 A class that is declared with the reserved word
abstract in its heading
 An abstract class can contain instance variables,
constructors, finalizers, and non-abstract methods
 An abstract class can contain abstract methods
◼ Abstract method contains no implementation, i.e. no body.

4/6/2024
21

 If a class contains an abstract method, the class must


be declared abstract
 You cannot instantiate an object of an abstract class
type; can only declare a reference variable of an
abstract class type
 You can instantiate an object of a subclass of an
abstract class, but only if the subclass gives the
definitions of all the abstract methods of the superclass
 If the subclasses does not override the abstract methods
of the abstract class, then it is mandatory for the
subclasses to tag itself as abstract.
4/6/2024
22

public abstract class AbstractClassExample


{
protected int x;
public abstract void print();

public void setX(int a)


{
x = a;
}

public AbstractClassExample()
{
x = 0;
}
} 4/6/2024
Abstract Method
23

 A method that has only the heading with no body


 Must be implemented in a subclass
 Must be declared abstract
public double abstract area();
public void abstract print();
 Why create abstract methods?
 To force same name and signature pattern in all the
subclasses
◼ Subclasses should not use their own naming patterns
 They should have the flexibility to code these methods with
their own specific requirements.

4/6/2024
Contents
1. Introduction
2. Demonstrating polymorphism concepts with super
classes and subclasses
3. Abstract classes and methods
4. Multiple inheritance and interfaces

4/6/2024
Interface
25

 An interface is a blueprint of a class.


 It has static constants and abstract methods.
 The interface in Java is a mechanism to
achieve abstraction.
 There can be only abstract methods in the Java interface,
not method body.
 Interface fields are public, static and final by default, and
the methods are public and abstract.
 It is used to achieve abstraction and
multiple inheritance in Java.
 In other words, you can say that interfaces can have
abstract methods and variables. It cannot have a method
body.
4/6/2024
26

 It cannot be instantiated just like the abstract class.


 Since Java 8, we can have default and static
methods in an interface.
 Since Java 9, we can have private methods in an
interface.
 Why use Java Interface?
 It is used to achieve abstraction.
 By interface, we can support the functionality of
multiple inheritance.
 It can be used to achieve loose coupling.

4/6/2024
27

 How to declare an interface?


 An interface is declared by using the interface keyword.
 It provides total abstraction; means all the methods in an
interface are declared with the empty body, and all the
fields are public, static and final by default.
 A class that implements an interface must implement all the
methods declared in the interface.
 Syntax:
interface <interface_name>{
// declare constant fields
// declare methods that abstract by default.
}
4/6/2024
28

 The relationship between classes and interfaces


 As shown in the figure given below, a class extends
another class, an interface extends another interface,
but a class implements an interface

4/6/2024
29 4/6/2024
30

 Multiple inheritance in Java by interface


 Ifa class implements multiple interfaces, or an interface
extends multiple interfaces, it is known as multiple
inheritance.

4/6/2024
31 4/6/2024
32

Questions?
4/6/2024
33

Thank You
4/6/2024

You might also like