OOPS Interview Questions and Answers – Part One

 

Also read this – Full List Of Interview Questions and Answers on OOPS

What is OOPS ?

OOP is a programming design philosophy/concept. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable “objects”. Hence, you gain re-usability by means of four main object-oriented programming concepts.

Real World Example: ‘ In order to clearly understand the object orientation, take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled by a set of electrical signals sent through your shoulders (an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.

What is an Object ?

An object is a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life.

Real World Example: Look around right now and you’ll find many examples of real-world objects: your dog, your desk, your television set, your bicycle. Real-world objects share two characteristics: They all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.

What is a Class ?

Class is a blueprint/ template that describe the details of an object. A class is the blueprint from which the individual objects are created.  Class is composed of three things: a name, attributes, and operations. A class is a collection of objects of a similar type. Once a class is defined, any number of objects can be created which belong to that class

What is Encapsulation ?

Encapsulation is the process of combining data and functions into a single unit called class. In Encapsulation, data is not accessed directly; it is accessed through the functions present inside the class. Encapsulation makes the concept of data hiding possible. Encapsulation is the feature that provides security to the data and the methods of a class. In the above example we can’t access the private data “Balance” directly using an object instance. We can only access/manipulate the data using public method viz – Deposit() or Withdrawl().

What is Abstraction ?

Abstraction is a process of hiding unwanted details (complexity) from the user. Abstraction means to show only the necessary details to the client of the object.

Let’s say you have a method “CalculateSalary” in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?

So abstraction says expose only the details which are concern with the user (client) of your object. So the client who is using your class need not to be aware of the inner details like how you class do the operations? He needs to know just few details. This certainly helps in re-usability of the code.

What is the difference between Encapsulation & Abstraction ?

Encapsulation is the process of hiding irrelevant data from the user. To understand encapsulation, consider an example of mobile phone. Whenever you buy a mobile, you don’t see how circuit board works. You are also not interested to know how digital signal converts into analog signal and vice versa. These are the irrelevant information for the mobile user, that’s why it is encapsulated inside a cabinet.

Abstraction is just opposite of Encapsulation. Abstraction is mechanism to show only relevant data to the user. Consider the same mobile example again. Whenever you buy a mobile phone, you see their different types of functionalities as camera, mp3 player, calling function, recording function, multimedia etc. It is abstraction, because you are seeing only relevant information instead of their internal engineering.

What is an Abstract Class ?

Abstract Classes An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, then the class itself must be declared abstract, as in:

public abstract class GraphicObject {
    // declare fields 
    // declare nonabstract methods 
    abstract void draw();
}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract. Note: Methods in an interface (see the Interfaces section) that are not declared as default or static are implicitly abstract, so the abstract modifier is not used with interface methods. (It can be used, but it is unnecessary.)

Abstract Classes Compared to Interfaces

Abstract classes are similar to interfaces. You cannot instantiate them, and they may contain a mix of methods declared with or without an implementation. However, with abstract classes, you can declare fields that are not static and final, and define public, protected, and private concrete methods. With interfaces, all fields are automatically public, static, and final, and all methods that you declare or define (as default methods) are public. In addition, you can extend only one class, whether or not it is abstract, whereas you can implement any number of interfaces.

What is Inheritance ?

Inheritance help you to extend the functionality of a class. Whenever you create a class that inherits from another class, it will inherit all properties and method of the inherited class plus it can have few properties/methods of its own, specific to the class.

Example:

Inheritance

What is Polymorphism ?

Polymorphism is a greek word, can be broken into “poly-morph’ where ‘poly’ means many and ‘morph’ mean form. So any thing that exists in more than one form is called a ‘poly-morph’. In Object Oriented Programming any entity that exists in more than one form, this concept is called as Polymorphism. Function Overloading, Function Overriding, Operator Overriding all are examples of Polymorphism.

Example:

public void CalculateSalary(int employeeId);
public void CalculateSalary(int employeeId, int bonus);

Above code snippet is an example of Polymorphsim, here functions have same name but different parameters/signatures.

Also read this – Full List Of Interview Questions and Answers on OOPS


Need free resume templates ? Check out our latest collection of Resumes - View All Resume Templates

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Login

Lost your password?