Object Oriented Programming Question and Answers
Object Oriented Programming Question and Answers
Why OOP?
The main advantage of OOP is better manageable code that covers following.
1) The overall understanding of the software is increased as the distance between the language
spoken by developers and that spoken by users.
2) Object orientation eases maintenance by the use of encapsulation. One can easily change the
underlying representation by keeping the methods same.
OOP paradigm is mainly useful for relatively big software. See this for a complete example that
shows advantages of OOP over procedural programing.
What is encapsulation?
Encapsulation is referred to one of the following two notions.
1) Data hiding: A language feature to restrict access to members of an object. For example,
private and protected members in C++.
2) Bundling of data and methods together: Data and methods that operate on that data are
bundled together.
What is Polymorphism? How is it supported by C++?
Polymorphism means that some code or operations or objects behave differently in different
contexts. In C++, following features support polymorphism.
Compile Time Polymorphism: Compile time polymorphism means compiler knows which
function should be called when a polymorphic call is made. C++ supports compiler time
polymorphism by supporting features like templates, function overloading and default
arguments.
Run Time Polymorphism: Run time polymorphism is supported by virtual functions. The idea is,
virtual functions are called according to the type of object pointed or referred, not according to
the type of pointer or reference. In other words, virtual functions are resolved late, at runtime.
What is Abstraction?
The first thing with which one is confronted when writing programs is the problem. Typically we
are confronted with “real-life” problems and we want to make life easier by providing a program
for the problem. However, real-life problems are nebulous and the first thing we have to do is to
try to understand the problem to separate necessary from unnecessary details: We try to obtain
our own abstract view, or model, of the problem. This process of modeling is called abstraction.
What is OOPS?
Object Oriented Programming System is the programming technique to write programs based on
the real world objects. The states and behaviors of an object are represented as the member
variables and methods. In OOPS programming programs are organized around objects and data
rather than actions and logic.
What is Abstraction?
Abstraction is an OOPS concept to construct the structure of the real world objects. During this
construction only the general states and behaviors are taken and more specific states and
behaviors are left aside for the implementers.
What is Encapsulation?
Encapsulation is an OOPS concept to create and define the permissions and restrictions of an
object and its member variables and methods. A very simple example to explain the concept is to
make the member variables of a class private and providing public getter and setter methods.
Java provides four types of access level modifiers: public, protected, no modifier and private.
What is Inheritance?
A subclass can inherit the states and behaviors of it’s super class is known as inheritance.
Dynamic or late or virtual binding is resolved at run time. Method overriding is an example of
dynamic binding.
What is Association?
Association is a relationship between two objects with multiplicity.
What is Aggregation?
Aggregation is also known as “HAS-A” relationship. When class Car has a member reference
variable of type Wheel then the relationship between the classes Car and Wheel is known as
Aggregation. Aggregation can be understood as “whole to its parts” relationship.
Car is the whole and Wheel is part. Wheel can exist without the Car. Aggregation is a weak
association.
What is Composition?
Composition is a special form of Aggregation where the part cannot exist without the whole.
Composition is a strong Association. Composition relationship is represented like aggregation
with one difference that the diamond shape is filled.
What is Dependency?
When one class depends on another because it uses that at some point in time then this
relationship is known as Dependency. One class depends on another if the independent class is a
parameter variable or local variable of a method of the dependent class. A Dependency is drawn
as a dotted line from the dependent class to the independent class with an open arrowhead
pointing to the independent class.
What is a Class?
A class is the specification or template of an object.
What is an Object?
Object is instance of class.
Define a constructor?
Constructor is a method used to initialize the state of an object, and it gets invoked at the time of
object creation. Rules forconstructor are:
Constructor Name should be same asclass name.
Constructor must have no return type.
Define Destructor?
Destructor is a method which is automatically called when the object ismade ofscope or
destroyed. Destructor name is also same asclass name but with the tilde symbol before the name.
What is an interface?
An interface is a collection of abstract method. If the class implements an inheritance, and then
thereby inherits all the abstract methods of an interface.
An object is an instance of a class. Objects hold any information , but classes don’t have any
information. Definition of properties and functions can be done at class and can be used by the
object.
Class can have sub-classes, and an object doesn’t have sub-objects.