You are on page 1of 5

Assignment

Course Code : IT 203


Course Title : Object Oriented Programming Language
Assignment Topic
1. Polymorphism
2. Multithreading

Submitted To : Tanzir Mehedi Shawon (Lecturer, Department of


Information Technology at UITS)

Submitted By
Name : Nazmul Islam
ID No : 1914555005
Batch : 45
Semester : Spring 3rd
Department : Information Technology ( IT )
Polymorphism in Java

The word polymorphism means having many forms.


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 prosses different behavior
in different situations. This is called polymorphism.
In Java polymorphism is mainly divided into two types:
 Compile time Polymorphism
 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. Example on compile time
polymorphism:
class MultiplyFun {
static int Multiply(int a, int b) {
return a * b;}
static double Multiply(double a, double b) {
return a * b; }
}
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: Sum = 5Concatinated
String - joenow

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. Here is an example on Runtime polymorphism:

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: subclass1subclass2
Multithreading in Java

Multithreading is a Java feature that allows concurrent execution of


two or more parts of a program for maximum utilization of CPU. Each
part of such program is called a thread. So, threads are light-weight
processes within a process.

Threads can be created by using two mechanisms :


1. Extending the Thread class
2. Implementing the Runnable Interface
Thread creation by extending the Thread class :
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
Output:thread is running...
Thread creation by implementing the Runnable
Interface:
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
Thread Class vs Runnable Interface
1. If we extend the Thread class, our class cannot
extend any other class because Java doesn’t support
multiple inheritance. But, if we implement the
Runnable interface, our class can still extend other
base classes.

2. We can achieve basic functionality of a thread


by extending Thread class because it provides some
inbuilt methods like yield(), interrupt() etc. that
are not available in Runnable interface.

You might also like