Features of OOP


Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user. For example, when you login to your bank account online, you enter your user_id and password and press login, what happens when you press login, how the input data sent to server, how it gets verified is all abstracted away from the you. 


Encapsulation simply means binding object state(fields) and behavior(methods) together. If you are creating class, you are doing encapsulation.

Encapsulation example in Java

How to
1) Make the instance variables private so that they cannot be accessed directly from outside the class. You can only set and get values of these variables through the methods of the class.
2) Have getter and setter methods in the class to set and get the values of the fields.


The process by which one class acquires the properties and functionalities of another class is called inheritance. Inheritance provides the idea of reusability of code and each sub class defines only those features that are unique to it, rest of the features can be inherited from the parent class.

  1. Inheritance is a process of defining a new class based on an existing class by extending its common data members and methods.
  2. Inheritance allows us to reuse of code, it improves reusability in your java application.
  3. The parent class is called the base class or super class. The child class that extends the base class is called the derived class or sub class or child class.

Note: The biggest advantage of Inheritance is that the code in base class need not be rewritten in the child class.
The variables and methods of the base class can be used in the child class as well.

Syntax: Inheritance in Java

To inherit a class we use extends keyword. Here class A is child class and class B is parent class.

class A extends B

Inheritance Example

In this example, we have a parent class Teacher and a child class MathTeacher. In the MathTeacher class we need not to write the same code which is already present in the present class. Here we have college name, designation and does() method that is common for all the teachers, thus MathTeacher class does not need to write this code, the common data members and methods can inherited from the Teacher class.

class Teacher {
String designation = “Teacher”;
String college = “universitymcqs.com”;
void does(){
public class MathTeacher extends Teacher{
String mainSubject = “Maths”;
public static void main(String args[]){
MathTeacher obj = new MathTeacher();







Note: Multi-level inheritance is allowed in Java but not multiple inheritance

Types of Inheritance:
Single Inheritance: refers to a child and parent class relationship where a class extends the another class.

Multilevel inheritance: refers to a child and parent class relationship where a class extends the child class. For example class A extends class B and class B extends class C.

Hierarchical inheritance: refers to a child and parent class relationship where more than one classes extends the same class. For example, class B extends class A and class C extends class A.

Multiple Inheritance: refers to the concept of one class extending more than one classes, which means a child class has two parent classes.


Polymorphism is a object oriented programming feature that allows us to perform a single action in different ways. For example, lets say we have a class Animal that has a method animalSound(), here we cannot give implementation to this method as we do not know which Animal class would extend Animal class. So, we make this method abstract like this:

public abstract class Animal{

public abstract void animalSound();

Now suppose we have two Animal classes Dog and Lion that extends Animal class. We can provide the implementation detail there.

public class Lion extends Animal{

public void animalSound(){

public class Dog extends Animal{

public void animalSound(){

As you can see that although we had the common action for all subclasses animalSound() but there were different ways to do the same action. This is a perfect example of polymorphism (feature that allows us to perform a single action in different ways).

Types of Polymorphism
1) Static Polymorphism
2) Dynamic Polymorphism