Oop Assignment
Oop Assignment
Oop Assignment
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.
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.
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.
Runtime Polymorphism:
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();
}
}