Principles of Object Oriented Programming Solution
Principles of Object Oriented Programming Solution
2. An object is represented by two attributes, out of which one is characteristics and the other one is __.
a. Behaviour
b. Situation
c. Abstraction
d. Encapsulation
4. Name the programming technique that specifies a series of well-structured steps and procedures
within its programming context to compose a program.
a. Procedure Oriented Programming
b. Modular Programming
c. Object Oriented Programming
d. None of these
5. Name the characteristics of Object Oriented Programming that hides the complexity and provides a
simple interface.
a. Encapsulation
b. Polymorphism
c. Abstraction
d. Inheritance
12. Which of the following principle does not allow to access directly from outside the class premises?
a. data hiding
b. encapsulation
c. abstraction
d. all of the above
13. The process of combining data and functions that enables them to be together as a single entity is
called:
a. inheritance
b. encapsulation
c. classification
d. attributes
14. The process by which a class acquires the properties from another class is called:
a. polymorphism
b. inheritance
c. abstraction
d. object
16. A ……. is a set of objects that has common attributes and common behavior:
a. abstraction
b. encapsulation
c. class
d. function
18. The process of restricting the free flow of data from the outside world is known as:
a. encapsulation
b. inheritance
c. function
d. class
B. State whether the following statements are True (T) or False (F).
1. Encapsulation refers to the art of hiding the complexities and giving a simple interface. F
2. Procedure Oriented Language follows top down approach. T
3. Java is an example of Object Oriented Language. T
4. Hiding the complexity and giving a simple interface is called Inheritance. F
5. Abstraction is same as Encapsulation. F
6. A process according to which a class acquires the characteristics from another class is
Encapsulation. F
7. Procedure oriented program stresses on data. F
8. C++ is also an object oriented programming language. T
9. An object is identified by its characteristics. T
10. Function is a set of objects that share the common state and behaviour. F
11. Encapsulation keeps data safe from outside interference. T
8. State differences between Procedure Oriented Language and Object Oriented Languages.
Ans.
Procedure Oriented Programming Object Oriented Programming
A large program is divided into smaller segments or A program is represented as an object.
procedures.
The stress is put on function rather than data. The stress is put on data rather than functions.
It allows data to flow freely throughout the The data is restricted, to be used in a specific program
program. area.
It follows top-down programming approach. It follows bottom-up programming approach.
11. What are the elements of Object Oriented Programming? Explain each in one sentence.
Elements of Object Oriented Programming are:
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
If I must describe each of these concepts in just one sentence, then:
For Abstraction, I will say Eliminate the irrelevant & amplify the essential
Encapsulation means Hiding the unnecessary
Inheritance helps in Modelling the similarities
Polymorphism means Same function different behavior
12. List the features of Object Oriented Programming.
Some of the features of Object Oriented Programming are:
1. It gives stress on data items rather than functions.
2. It makes the complete program/problem simpler by dividing it into number of objects.
3. The objects can be used as a bridge to have data flow from one function to another.
4. The concept of data hiding enhances security in programs.
5. It is highly beneficial to solve complex programs.
13. Name any two Object Oriented Programming and Procedure Oriented Programming
languages.
Object Oriented Programming languages ⇒ C++, Java
Procedure Oriented Programming languages ⇒ BASIC, C
For the purposes of our discussion, we will look at both properties which are defined under
Encapsulation. First one being restricting direct access to some of the object's components.
Rephrasing it in a little simpler wording, it means hiding some of the implementation details from
the outside world. This is also known as Data hiding. The second point in the definition above on
facilitating bundling of data with methods means characteristics and behaviour are combined into a
single unit.
Let's look at an ATM. You go to the ATM, swipe your card, enter your pin and then enter the
amount of money you want to withdraw. ATM processes your request and gives you the money. If
you were to see ATM through object-oriented lens you will realize that ATM contains different
denominations of currency notes and all the operations to read your debit card, verify your pin, count
currency notes, dispense the money to you, etc. ATM combines all the different denominations of
currency notes and all the operations required to withdraw money into a single unit. You interact
with this one machine which is wrapping all this together and very conveniently withdraw cash. Here
ATM can be viewed as a class which is wrapping together the data which are the currency notes and
methods which are the operations required to withdraw money into a single unit. In other words, we
can say that ATM encapsulates money and operations required to withdraw it into a single unit.
You can interact with the ATM only through the numeric keypad and the onscreen options it
provides. You cannot access the details about internal data like how much money does this ATM
has, how many notes of each denomination are present, how much money has been withdrawn so far,
etc. This is the Data Hiding concept of Encapsulation.
Consider this example where a lady has brought her pet cat to a vet for treatment. Let's see the cat
from Vet's point of view, what are the essential features of the cat for the Vet?
Vet’s aim is to examine the cat and diagnose any health issues the cat is having so for the Vet the
essential thing is the body structure of the cat. The Vet is interested in the different body parts of the
cat like heart, lungs, stomach, kidney, intestine, liver bladder to ensure everything is functioning
perfectly and the cat is healthy. Some features irrelevant to the Vet are the favourite toy of the cat,
what food the cat likes the most or what is the color of the cat's bed. These things are of little h elp to
diagnose any health problems the cat is having so they are mostly irrelevant to the Vet.
Now let's see the cat from the lady's point of view
Things about the cat that matter to the lady are cat's favorite toy, favorite food, color of cat's bed.
Because cat is the lady’s pet, everything that makes the cat feel happy is essential to the lady. On the
other hand, things like the anatomy of the cat, scientific names of cat's body parts, etc are irrelevant
to the lady as these things doesn’t help the lady to make her cat feel happy.
So now I hope you have a better sense of how essential information depends upon the problem at
hand. Depending on the problem domain, we need to eliminate the irrelevant & amplify the
essential.
There were two core parts in the definition of Data Abstraction. We have already looked at and
understood the essential features part. Now, let's try to understand hiding the background details part
with the example of a switchboard.
How do you switch on a light or fan in your room at home? Obviously, you go to the switchboard,
press the required switch to turn ON the light or fan. You don't worry about how that switch is
turning the light ON, what is the internal wiring, etc. Switchboard is hiding all such background
details from you and providing you a very simple way to switch on or off the light.
You encounter such examples of Abstraction every day in your life. While driving a car the driver
presses accelerator to speed up the car and brakes to stop it. He doesn’t worry about all t he
background details regarding how the engine is working, how the pistons in the engine are firing up
at an increased RPM to speed up the car or how the brakes are working to slow down and stop the
car. All these details are hidden away from the driver and he is presented with two very simple
interfaces – accelerator and brakes to speed up and slow down the car. Such hiding of background
details through Abstraction is making our day to day lives simpler.
Java supports Abstraction through abstract classes & interfaces.
Question 9
Explain 'Inheritance' with a suitable example.
Inheritance enables new classes to receive or inherit the properties and methods of existing classes.
In the world around us, we are surrounded with entities that are connected to each other with "is-A"
relationship. The concept of Inheritance helps us to model such "is-A" relationships in software. I
know, right now you are a little confused with what "is-A" relationships I am talking about. So as
always, lets look at some examples to develop more intuition about these "is-A" relationships and
concretely understand Inheritance.
Take the example of traffic on the road, what this traffic consists of? There are Cars, Bikes, Scooters,
Auto rickshaw, Buses, Trucks, etc. All of these have certain common characteristics and behaviour
— they can move on the road and transport people and goods from one place to another. These
commonalities help us to categorize them under one common category called Vehicles.
Now we can say that Car is a vehicle, Bus is a vehicle, Bike is a vehicle, Auto is a vehicle and so
on. This is just one of the examples of "is-A" relationship around us. This helps us to conveniently
say that there are many different types of vehicles on the road rather than calling out each of the type
separately.
Building on it a little further, there are many different types of cars - i10, i20, XUV 500, Fortuner,
Baleno, etc. This is also an example of is-A relationship where each one of these is-A car. Similarly
for bikes, you may have a TVS Apache, Bajaj Pulsar, KTM Duke or Harley Davidson and each of
them is a Bike. You can extend this similarly for buses and trucks as well. I hope this helps you to
see how we can use is-A relationship to categorize entities based on common characteristics and
behaviours. Also, most of the times these relationships are hierarchical — there are vehicles then
cars, bikes then the different models of these cars and bikes, etc.
You can look around and find other such examples as well like the different kind of animals - dog is
an animal, cat is an animal, goat is an animal, elephant is an animal. Of course, you can make it more
hierarchical like this
We have a base class Vertebrates for all the animals with a backbone. From there we can derive a
mammals class to group animals that have mammary glands, give live birth to young ones, have 3
middle ear bones, etc. Mammal is-A vertebrate. Then from mammal we can derive specific examples
of mammals like humans, whales, dog, Monkey, etc. Each one of these human, whale, dog, Monkey
is-A mammal. This is another example of hierarchical is-A relationship where whale in addition to
its own unique characteristics and behaviour, inherits all the characteristics and behaviour of
mammals and vertebrates. Similarly, we can expand it for Birds, Reptiles, Fishes & Amphibians as
well.
Inheritance helps in adapting to changes in software:
It helps in avoiding code duplication. We can put the common code in a base class and share
it with derived classes.
It also reduces the need for code manipulation — to implement a new functionality we can
always create a new derived class rather than changing the code of the base class which may
impact the entire application