Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

JAVA PROGRAMMING

Assessment-1
NAME: Asmit Gupta
REG. NO.:18BCE0904
SLOT: C1
FACULTY: ASIS KUMAR TRIPATHY

Link:-
https://youtu.be/l3spx9HmhiY
Contents

• Definition-polymorphism
• Types :- 1)Compile Time 2)Run Time
• Compile time polymorphism and its examples.
• Run rime polymorphism and its examples.
• Difference overloading and overriding.
• Advantages of polymorphism.
What is polymorphism?

• Polymorphism is the concept of one entity providing multiple implementations


or behaviors.Thus polymorphism is the ability of an object to make more than
one form.
• Java is an object-oriented programming language that supports the concept
of polymorphism.
• Alternatively, it is defined as the ability of a reference variable to change
behavior according to what object instance is holding.
• This allows multiple objects of different subclasses to be treated as objects
as a single parent class, while automatically selecting the proper methods to
apply to an object based on the child class it belongs.
Two types of polymorphism

Compile -Time
Polymorphism

Polymorphism In
Java

Run-Time
Polymorphism
• Compile-time polymorphism refers to
behaviour that is resolved when your Java
class is compiled.
• Method overloading is an example of
compile-time polymorphism. Method
overloading is how you can use method with
same name to do different things based on
the parameters passed.
Different ways to overload the method:

There are two ways to overload the method in java

•By changing number of arguments

•By changing the data type


Example of By changing number of arguments:
class Calculation{
void sum(int a,int b)
{
System.out.println(a+b);
Output:
} 30
void sum(int a,int b,int c){ 40
System.out.println(a+b+c);
}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(10,10,10);
obj.sum(20,20);

}
}
Example of By changing the data type:

class Calculation{
void sum(int a,int b){
System.out.println(a+b);
}
void sum(double a,double b){
System.out.println(a+b);
}

public static void main(String args[]){


Calculation obj=new Calculation();
obj.sum(11.5,11.5);
obj.sum(30,20);

}
Illustration – Method Overloading
class MultiplyFun {
int Multiply(int a, int b)
{
return a * b;
Output:
} 8
int Multiply(int a, int b, int c) 42
{
return a * b * c;
}
}
class Main {
public static void main(String[] args)
{
System.out.println(MultiplyFun.Multiply(2, 4));

System.out.println(MultiplyFun.Multiply(2, 7, 3));
}
}
Illustration – Method Overloading
class OperatorOVERDDN {
• void operator(String str1, String str2)
• {
• String s = str1 + str2; System.out.println("Concatinated String - "+ s);
• }
• void operator(int a, int b)
• {
• int c = a + b; System.out.println("Sum = " + c);
• }
}
class Main {
public static void main(String[] args)
{
OperatorOVERDDN obj = new
OperatorOVERDDN(); obj.operator(2, 3);
obj.operator("joe", "now");

} } Output:
Sum = 5
Concatinated String - joenow
• Run-time polymorphism refers to
behaviour that is resolved when your Java
class is run by the JVM. Method overriding
by the sub-class is an example of run-time
polymorphism.
• Method overriding allows child
classes to provide their own
implementation of a method also
defined in the parent class.
• The JVM decides which version of the
method (the child’s or the parent’s) to call
based on the object through which the
method is invoked.
EXAMPLE
class Parent {
void show()
{
System.out.println("Parent's show()");
}}
class Child extends Parent {
void show()
{
System.out.println("Child's show()");
}}
class Main {
public static void main(String[] args)
{
Parent obj1 = new Parent();
obj1.show();
Parent obj2 = new Child();
obj2.show();
}}
Illustration – Method Overriding
class Parent { class TestPolymorphism3 {
void Print() public static void main(String[] args)
{ {
System.out.println("parent class"); Parent a;
} a = new subclass1();
} a.Print();
class subclass1 extends Parent { a = new subclass2();
a.Print();
void Print() }
{ }
System.out.println("subclass1");
}
}
class subclass2 extends Parent { Output:
void Print() subclass1
{
subclass2
System.out.println("subclass2");
}
}
Advantages Of Polymorphism

Code Ease Of
Cleanliness Implementation

Aligned With Overloaded


Real World Constructors

ReusabilityAnd
Extensibility

You might also like