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

0

NAME : ALI HAMZA


ROLL NO : 2019-SE-219 SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
SECTION : E

ASSIGNMENT

Object Oriented Programming

Figure out the type of polymorphism is method overloading and method overriding
respectively.
Is constructor overloading polymorphism? If Yes or No, Justify your Answer.

MISS
FIZZAH SOHAIL

OBJECT ORIENTED PROGRAMMING(SWE-103)


1
NAME : ALI HAMZA
ROLL NO : 2019-SE-219 SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
SECTION : E

QUESTION # 01:
Figure out the type of polymorphism is method overloading and method
overriding respectively.

ANSWER : Polymorphism in Java has two types: Compile time polymorphism (static binding)
and Runtime polymorphism (dynamic binding). Method overloading is an example of
static polymorphism, while method overriding is an example of dynamic polymorphism.
Polymorphism means more than one form, same object performing different operations according
to the requirement.

Polymorphism can be achieved by using two ways, those are

1. Method overriding
2. Method overloading
Method overloading means writing two or more methods in the same class by using same method
name, but the passing parameters is different.
Method overriding means we use the method names in the different classes,that means parent class
method is used in the child class.
In Java to achieve polymorphism a super class reference variable can hold the sub class object.

To achieve the polymorphism every developer must use the same method names in the project.

OBJECT ORIENTED PROGRAMMING(SWE-103) MISS : FIZZAH SOHAIL


2
NAME : ALI HAMZA
ROLL NO : 2019-SE-219 SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
SECTION : E

Example of Method Overloading by changing the number of arguments :


In this example, we have created two overloaded methods, first sum method performs addition of
two numbers and second sum method performs addition of three numbers.
package com.example;

public class OverloadingEx {


void sum(int a, int b) {
int sum = a + b;
System.out.println("sum of no is " + sum);

void sum(int a, int b, int c) {


int sum = a + b + c;
System.out.println("sum of no is " + sum);
}

public static void main(String args[]) {


OverloadingEx obj = new OverloadingEx();
obj.sum(5, 10, 10);
obj.sum(10, 10);

}
}
OUTPUT
sum of no is 25
sum of no is 20

Example of Method Overloading by changing data type of argument


In this example, we have created two overloaded methods that have different data types in
argument. The first sum method receives two integer arguments and second sum method receives
two double arguments.
package com.example;

public class OverloadingEx {

OBJECT ORIENTED PROGRAMMING(SWE-103) MISS : FIZZAH SOHAIL


3
NAME : ALI HAMZA
ROLL NO : 2019-SE-219 SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
SECTION : E
void sum(int a, int b) {
int sum = a + b;
System.out.println("sum of no is " + sum);

void sum(double a, double b) {


double sum = a + b;
System.out.println("sum of no is " + sum);
}

public static void main(String args[]) {


OverloadingEx obj = new OverloadingEx();
obj.sum(5, 10);
obj.sum(5.0, 2.0);

}
}
Output
sum of no is 15
sum of no is 7.0

Example of Method Overriding by changing sequence of data type of argument


class Parent
{
void show() { System.out.println("Parent's show()"); }
}
// Inherited class
class Child extends Parent
{
// This method overrides show() of Parent
@Override
void show() { System.out.println("Child's show()"); }
}

// Driver class

OBJECT ORIENTED PROGRAMMING(SWE-103) MISS : FIZZAH SOHAIL


4
NAME : ALI HAMZA
ROLL NO : 2019-SE-219 SIR SYED UNIVERSITY OF ENGINEERING & TECHNOLOGY
SECTION : E
class Main
{
public static void main(String[] args)
{
// If a Parent type reference refers
// to a Parent object, then Parent's
// show is called
Parent obj1 = new Parent();
obj1.show();

// If a Parent type reference refers


// to a Child object Child's show()
// is called. This is called RUN TIME
// POLYMORPHISM.
Parent obj2 = new Child();
obj2.show();
}
}
OUTPUT:
Parent’s show()
Child’s show()

QUESTION # 02:
Is constructor overloading polymorphism? If Yes or No, Justify yourAnswer.

ANSWER:
In runtime polymorphism, which function should be called is decided at runtime. In constructor
overloading, its parameter will be used at compile time to decide which instance should be called
Constructor overloading is compile time, but overriding is runtime polymorphism.

OBJECT ORIENTED PROGRAMMING(SWE-103) MISS : FIZZAH SOHAIL

You might also like