Ch10-Global Supply Chain - Cadena de Suministro.pdf
Java(Polymorphism)
1. GANDHINAGAR INSTITUTE OF TECHNOLOGY
Computer Engineering Department
OOPJ (2150704)
Polymorphism
Prepared By:
Harsh Kothari
(170120107066)
Guided By:
Prof. Ketan Pandya
2. 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.
3. What is polymorphism?
• Polymorphism is the concept of one entity providing multiple implementations
or behaviors(something like an Avatar).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.
4. Peter Parker Is Polymorphic
• As a high school student, Peter
goes to school and hangs out with
friends. As Spider Man, he
bashes up baddies. Peter has
multiple behaviors based on the
context. Peter is polymorphic.
5. Two types of polymorphism
Compile -Time
Polymorphism
Run-Time
Polymorphism
Polymorphism In
Java
6. • 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.
A calculator can add 2 integers. It can also
add 2 floats. The addition method adds
differently based on the inputs.
7. Illustration – Method Overloading
class MultiplyFun {
int Multiply(int a, int b)
{
return a * b;
}
int Multiply(int a, int b, int c)
{
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));
}
}
Output:
8
42
8. 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
9. • 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. Consider a SeniorCitizenAccount class. It
will provide a calculateInterest method()
that overrides the calculateInterest()
method in the parent SavingsAccount class
10. Illustration – 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();
}
}
Output:
subclass1
subclass2