KEMBAR78
Unit I - Polymorphism | PDF | Class (Computer Programming) | Method (Computer Programming)
0% found this document useful (0 votes)
85 views14 pages

Unit I - Polymorphism

The document discusses polymorphism in object-oriented programming. Polymorphism allows the same message to be interpreted in different ways depending on the object that receives it. There are two types of polymorphism - compile-time polymorphism achieved through method overloading, and runtime polymorphism achieved through method overriding.

Uploaded by

RUDRAKSHI SAWANT
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)
85 views14 pages

Unit I - Polymorphism

The document discusses polymorphism in object-oriented programming. Polymorphism allows the same message to be interpreted in different ways depending on the object that receives it. There are two types of polymorphism - compile-time polymorphism achieved through method overloading, and runtime polymorphism achieved through method overriding.

Uploaded by

RUDRAKSHI SAWANT
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/ 14

Unit I

Polymorphism
Course Outcome: At the end of this course, students will be able to:
• Design and develop systems that are easily extensible to demonstrate
polymorphic behaviour
Polymorphism
• The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form.
• Real life example of polymorphism: A person at the same time can have different
characteristic. Like a man at the same time is a father, a husband, an employee.
So the same person posses different behavior in different situations. This is called
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.
• In Java polymorphism is mainly divided into two types:
- Compile time Polymorphism and Runtime Polymorphism
1. Compile-time polymorphism: It is also known as static polymorphism. This
type of polymorphism is achieved by function overloading or operator
overloading. But Java doesn’t support the Operator Overloading.
• Method Overloading: When there are multiple functions with same name
but different parameters then these functions are said to be overloaded.
Functions can be overloaded by change in number of
arguments or/and change in type of arguments.
Example1: By using different types of arguments

// Java program for Method overloading


class MultiplyFun
{ // Method with 2 parameter
static int Multiply(int a, int b)
{ return a * b;
}
// Method with the same name but 2 double parameter
static double Multiply(double a, double b)
{ return a * b;
}
}
Continued…
class Main
{ public static void main(String[] args)
{ System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
}
}
Output:
8
42
Example 2: By using different numbers of arguments

class MultiplyFun
{ // Method with 2 parameter
static int Multiply(int a, int b)
{ return a * b;
}

// Method with the same name but 2 double parameter


static double Multiply(double a, double b)
{ return a * b;
}
}
Continued…
class Main
{ public static void main(String[] args)
{ System.out.println(MultiplyFun.Multiply(2, 4));
System.out.println(MultiplyFun.Multiply(5.5, 6.3));
}
}

Output:
8
42
2. 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.
• Method overriding, on the other hand, occurs when a derived class has a
definition for one of the member functions of the base class. That base
function is said to be overridden.
// Java program for Method overriding
class Parent
{ void print()
{ System.out.println("parent class");
}
}
class subclass1 extends Parent
{ void print()
{ System.out.println("subclass1");
}
}
class subclass2 extends Parent {
void print()
{ System.out.println("subclass2");
}
}

class TestPolymorphism3 {
public static void main(String[] args)
{ Parent a;
a = new subclass1(); a.Print();
a = new subclass2(); a.Print();
}
}
Difference between Abstract Class and
Concrete Class in Java
• Abstract Class: An abstract class is a type of class in Java that is declared by
the abstract keyword. An abstract class cannot be instantiated directly, i.e.
the object of such class cannot be created directly using the new keyword.
An abstract class can be instantiated either by a concrete subclass or by
defining all the abstract method along with the new statement. It may or
may not contain an abstract method. An abstract method is declared by
abstract keyword, such methods cannot have a body. If a class contains an
abstract method, then it also needs to be abstract.
• Concrete Class: A concrete class in Java is a type of subclass, which
implements all the abstract method of its super abstract class which it
extends to. It also has implementations of all methods of interfaces it
implements.
abstract class DemoAbstractClass {
abstract void display();
}

public class JavaApplication {


public static void main(String[] args)
{
DemoAbstractClass AC = new DemoAbstractClass();
System.out.println("Hello");
}
}
Compile time error

Microsoft Windows [Version 10.0.19045.3208]


(c) Microsoft Corporation. All rights reserved.

C:\Users\sabalesm\Desktop\SMS\AJP\TW-Winter23>javac JavaApplication.java
JavaApplication.java:8: error: DemoAbstractClass is abstract; cannot be instantiated
DemoAbstractClass AC = new DemoAbstractClass();
^
1 error

• C:\Users\sabalesm\Desktop\SMS\AJP\TW-Winter23>

You might also like