You are on page 1of 5

Assignment

Course Code: IT 203


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

Submitted To: Tanzir Mehedi Shawon (Lecturer, Department of


Information Technology at UITS)

Submitted By
Name : MD. Zahidul Islam
ID No : 1844455002
Batch : 44
Semester : Spring 3rd
Department: Information Technology ( IT )

INHERITANCE
Inheritance can be defined as the process where one class acquires the
properties (methods and fields) of another. With the use of inheritance the
information is made manageable in a hierarchical order.
The class which inherits the properties of other is known as subclass
(derived class, child class) and the class whose properties are inherited is
known as superclass (base class, parent class).

Types of inheritance:
1.Single Inheritance: In single inheritance, subclasses inherit the
features of one superclass. In image below, the class A serves as a base
class for the derived class B.

2.Multilevel Inheritance: In Multilevel Inheritance, a derived class will


be inheriting a base class and as well as the derived class also act as the
base class to other class.

3.Hierarchical Inheritance: In Hierarchical Inheritance, one class


serves as a superclass (base class) for more than one sub class.

4.Multiple Inheritance (Through Interfaces): In Multiple


inheritance, one class can have more than one superclass and inherit
features from all parent classes. Please note that Java
does not support multiple inheritance with classes. In java, we can achieve
multiple inheritance only through Interfaces.

5.Hybrid Inheritance (Through Interfaces): It is a mix of two or


more of the above types of inheritance. Since java doesn’t support multiple
inheritance with classes, the hybrid inheritance is also not possible with
classes. In java, we can achieve hybrid inheritance only through Interfaces.

Extends Keyword:
extends is the keyword used to inherit the properties of a class. Following
is the syntax of extends keyword.
Syntax:
class Super {
.....
.....
}
class Sub extends Super {
.....
.....
}

Super keyword:
The super keyword is similar to this keyword. Following are the scenarios
where the super keyword is used.
 It is used to differentiate the members of superclass from the
members of subclass, if they have same names.
 It is used to invoke the superclass constructor from subclass.
Inheritance and Method Overriding:
When we declare the same method in child class which is already present
in the parent class the this is called method overriding. In this case when
we call the method from child class object, the child class version of the
method is called.

Inheritance program example:

class Employee{  

 float salary=40000;  

}  

class Salary_Info extends Employee{  

 int bonus=10000;  
 public static void main(String args[]){  
   Salary_Info p=new Salary_Info();  
   System.out.println("Employee salary is:"+p.salary);  
   System.out.println("Bonus of Employee is:"+p.bonus);  
}  
}  

Polymorphism
Polymorphism in Java is a concept by which we can perform a single
action in different ways. Polymorphism is derived from 2 Greek words: poly
and morphs. The word "poly" means many and "morphs" means forms. So
polymorphism means many forms.

Types of Polymorphism:
There are two types of polymorphism in Java.
1.Compile-time(or static) Polymorphism
2.Runtime(or dynamic) Polymorphism

Compile-time Polymorphism:
Polymorphism that is resolved during compiler time is known as static
polymorphism. Method overloading is an example of compile time
polymorphism.

Method Overloading: This allows us to have more than one method


having the same name, if the parameters of methods are different in
number, sequence and data types of parameters. 

Runtime Polymorphism:

Runtime polymorphism is a process in which a call to an overridden


method is resolved at runtime rather than compile-time.

In this process, an overridden method is called through the reference


variable of a superclass. The determination of the method to be called is
based on the object being referred to by the reference variable.
Polymorphism program example:

class Bike{  

void run(){System.out.println("running");}  
}  
class Splendor extends Bike{  
void run(){System.out.println("running safely with 60km");}  
  
public static void main(String args[]){  
Bike b = new Splendor();  
b.run();  
  }  
}  

You might also like