Sunday, 21 February 2016

This blog will guide you the basic theoretical concepts of OOPs i.e from a tester point of view.
Further I would be including some interview questions being asked to a QE/QA/Tester around OOPs.

1. What is OOPs ?

Oops is object oriented programming language which is an extension of the modular approach of programming. Four concepts of OOPs are -
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
We will be discussing each of the concept in detail, but before that we need to understand few basic terms like Class,object,module.

2. Class, Objects, Module

Class 

Class is a blueprint of what we want to do. Let me explain with the help of a simple day-to day life example.
EXAMPLE - Consider an urban planner Mr. X, who is given the contract to plan and build two bedroom flats.
Untitled drawing
Here, blue box is a class containing blueprint of how 2 bed room house should look like.By using blueprint we created three objects of the class.

 

Objects

Objects are basically comprised of three things -
State + Behavior + Identity.
EXAMPLE - Consider an example of the hammer in the below picture.
Untitled drawing (1).jpg

Module

Module can be considered as the collection of the blueprint/classes.
EXAMPLE - Lets say you have blueprints of 2 bedroom flat,3 bedroom flat and 4 bedroom flat. We would be having separate classes for all three types of bedrooms.
Collection of all three blueprints(2 bedroom flat,3 bedroom flat and 4 bedroom flat) is called as Module.

3. OOPs Concepts

Abstraction

Abstraction is the technique for managing complexity by separating interface or interaction details away from lower level implementation details.
Above definition would become more clear with the help of the following example.Let us assume we have two classes - 'horses' and 'eagle'.
Untitled drawing (2).jpg
In above example, all four functions are global. Our compiler is not smart enough to detect that horse cannot fly() or hunt(). This would be an illegal call . Now what can be the solution, solution is ABSTRACTION.

We can use abstraction to solve this problem. Let's push the functions gallop() and canter() inside the envelope of class horse. Further push functions fly() and hunt() inside the envelope of class eagle.
Now gallop() and canter() belong to class horse only. And fly() and hunt() belong to class eagle only. Thus if the compiler try to use the function fly() with horse class, it would give an error.

Encapsulation

Encapsulation infers two meaning -
  1. Restricting access to certain components.
  2. Somehow combining/bundling/compiling data with functions that operate on that data.
EXAMPLE 1 - To protect a garden from the damage that can be caused by public/animals, owner hired a guard .
Untitled drawing (3).jpg
EXAMPLE 2 - Lets try to understand concept of encapsulation with another technical example. Let us consider we have a data(int x), which can be changed only with the help of write() function. No external function can access variable x, except for write().
Untitled drawing (4).jpg

 Inheritance

Inheritance is the ability to reuse the code. It can be considered as extending functionality of one class to other.
EXAMPLE - Suppose we have a class A, containing blueprint of 2 bedroom flat. Now we want a blueprint of 3 bedroom flat instead of 2. So what we can do ? We can use blueprint of 2 bedroom flat and can further extend it to add 1 more room.
Here,flat of 2 bed room can be considered as base class and flat of 3 bedroom can be considered as derived class.
Untitled drawing (6).jpg

Polymorphism

Polymorphism means many types or many behavior. There are two main concepts that comes under polymorphism -
  1. Function overloading : Consider we have a class -  class  A, like -
Untitled drawing (7).jpg
Here, when we declare an object as -
A obj = new A(); //initializing an object
obj.show();  //calling function show();
Now what will happen . Which show() function to be called??
So, obj.show() would try to search for the function show() first. It will found that there are two show functions. One is with parameter and other is no parameter.
Next obj.show() would search for non parameterised as we are  calling a function show() which is not having any parameter.
Hence it would call show() instead of show(int i).

This is called function overloading/method overloading.
2. Method Overriding/function overriding : At times we have faced a situation in which two classes are having same function i.e same name of the function , same parameter etc. How to deal with such situation. Here comes the concept of Method overriding.
EXAMPLE -  Let us consider a class A which is a base class with a function defined inside it as show().
Now let us consider another class B derived from class A(base class).Concept of inheritance says, that now derived class B contains function show() derived from class A .


Untitled drawing (9).jpg
Now when we initialize an object like -
B obj = new B(); //initializing an object
B.show();  //calling function show();
Now consider above picture, here on calling the function show() via an object of Class B , it will be calling show() defined inside class A because B has inherited the function s of class A.

Now what if I define one more function inside B with the the same name as show();
Now let us consider another class B derived from class A(base class) containing another function with the same name as show().
Concept of inheritance says, that now derived class B contains two functions with the same name as show(). One derived from class A and one of its own.
Untitled drawing (8)

Now when we initialize an object like -
B obj = new B(); //initializing an object
B.show();  //calling function show();
Now consider above picture, here on calling the function show() via an object of Class B , it will be calling show() defined inside class B instead A. that is here B.show() would be called.
This is called as method overriding.

4. Interview Questions

As I promised in the starting of the Blog that I would be listing few interview questions being asked to a tester around the topic - OOPs , so here we go.....
Preparation of these questions would give you a boost/confidence to answer the questions around OOps.
Most of the questions you would be able to answer after reading the above blog. But if not then I will give you answers along , below -
  1. What is OOPS?
Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic.
2. What are the basic concepts of the OOps?
3. What is class?
4. What is Object?
5. What is Inheritance?
6. What is polymorphism?
7. What is encapsulation?
8. What is constructor ?
Constructor is used for the correct initialization of the data. It is called at the time of the creation of the object.Constructor always have same name as that of the class and have no return type.
If we do not have defined a constructor explicitly then at the time of compilation , compiler define a default constructor with no parameters .
In case we have defined a constructor within a class explicitly then compiler would not defined any default constructor.
9. Define destructor ?
Destructor is to de-allocate the object once its work is done.A destructor is a method which is automatically invoked when the object is destroyed.
Its main purpose is to free the resources (memory allocations, open files or sockets, database connections, resource locks, etc.)
10. What is the difference between method overloading and method overriding?
11. What is the difference between encapsulation and abstraction?