Вы находитесь на странице: 1из 5

Assignment

Course Code : IT 203


Course Title : Object Oriented Programming Language
Assignment Topics
1. Inheritance
2. Polymorphism

Submitted To :TanzirMehediShawon (Lecturer, Department of


Information Technology at UITS)

Submitted By
Name : Aysha Akhter
ID No : 1914555010
Batch : 45
Semester : Spring 3rd
Department : Information Technology ( IT )

Polymorphism:
Poly means many and morphism means forms therefore polymorphism
means many forms. It is the skill of an entity to be in many forms.
Polymorphism works through a reference variable in order to achieve an
objects you can communicate the methods through reference variables
which is only in one type and cannot be changeable. It is also be assigned
as a class or as an interface.

Types of Polymorphism:
There are several types of polymorphism which are given as following:
1. Method overloading.
2. Constructor overloading.
3. Method overriding.

Method overloading:
It is a concept of object oriented programming where it is allowed in one
class that contains many methods having same name but different
parameters.
Parameters are also known as arguments. An example of method
overloading is shown as Cat (int a, string b), Cat (int a, int b, string c) same
methods name having different arguments.

Constructor Overloading:
It is a built-in method of a class to instantiate a class object and it can be
many but different parameters is called constructor overloading.
You can initialize constructorwith a same name of a class otherwise your
constructor will not be created properly. Public Animal (), Public Animal (int
a) is the example of constructor overloading.

Example:

class Doctor{
public void treatPatient(){
// treatPatient method

}
class Surgeon extends Doctor{
public void treatPatient(){
// treatPatient method
}
}
Class run{
public static void main (String args[]){
Doctor doctorObj = new Doctor()
// treatPatient method in class Doctor will be executed
doctorObj.treatPatient();

Surgeon surgeonObj = new Surgeon();


// treatPatient method in class Surgeon will be executed
surgeonObj.treatPatient();
}
}

Difference between Overloading and Overriding


Method Overloading Method Overriding

Method overriding is when one of the


Method overloading is in the same class,
methods in the super class is redefined in th
where more than one method have the same
sub-class. In this case, the signature of the
name but different signatures.
method remains the same.
Ex:

class X{
public int sum(){
// some code
Ex:
}
}
void sum (int a , int b);
void sum (int a , int b, int c);
class Y extends X{
void sum (float a, double b);
public int sum(){
//overridden method
//signature is same
}
}
 Inheritance:
Inheritance is a mechanism in which one class acquires the property of
another class. For example, a child inherits the traits of his/her parents. With
inheritance, we can reuse the fields and methods of the existing class. Hence,
inheritance facilitates Reusability and is an important concept of OOPs.

Types of Inheritance
There are Various types of inheritance in Java:

Single Inheritance:
In Single Inheritance one class extends another class (one class only).

Multiple Inheritance:
In Multiple Inheritance, one class extending more than one class. Java does
not support multiple inheritance.

Multilevel Inheritance:
In Multilevel Inheritance, one class can inherit from a derived class. Hence,
the derived class becomes the base class for the new class.

Hierarchical Inheritance:
In Hierarchical Inheritance, one class is inherited by many sub classes.

Hybrid Inheritance:
Hybrid inheritance is a combination of Single and Multiple inheritance.

Example:
class Doctor {

void Doctor_Details() {
System.out.println("Doctor Details...");
}
}

class Surgeon extends Doctor {


void Surgeon_Details() {
System.out.println("Surgen Detail...");
}
}

public class Hospital {


public static void main(String args[]) {
Surgeon s = new Surgeon();
s.Doctor_Details();
s.Surgeon_Details();
}
}

Вам также может понравиться