Professional Documents
Culture Documents
18bce0904 VL2020210504604 Da
18bce0904 VL2020210504604 Da
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?
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:
}
}
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);
}
}
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
ReusabilityAnd
Extensibility