You are on page 1of 12

PRINCIPLES OF OBJECT

ORIENTED PROGRAMMING

Learning Objectives
" Types of Computer Languages " Programming Paradigms
. Principles of Object-Oriented Programming

Communication is the medium through which we can express our ideas to others. To communicate with
other people, we need a language like Hindi, English, Bengali, etc. Similarly, a programmer needs a language
to communicate with the computers. These are called computer languages. Computer language is an interface
between acomputer and human beings. It is a set of words, symbols and codes that the computer can understand.
Itis usedto write a computer program.

OTYPES OF COMPUTER LANGUAGES USER


Mhere area number of computer languages worldwide. They are classified into three major
High-Level Languag
categories:
1. Machine Level language Assembly Languaga

2. Assembly Level Language Machine Languaaa


B. High Level Language
HARDWARE
Machine Language (1 GL)
(tisa low-level language and consists of binary digits i.e., zeros and ones (0's and 1's) only, put together in agroup
o8 bits)The form of binary instruction is called machine code that is directly understood by computer hardware.
Al text, graphics, audio, etc. are converted to binary format using compiler and interpreter. So, first generation
language is machine dependent language.
eample:Character 'A' is represented as 01100001, Character 'B' is represented as 01000010, etc. So, to write
ul name we have to combine the binary formats of the all the characters together. This becomes difficult to
remembe.
Note: Agroup of 8 bits represents a character.

Principles of Object-Oriented Programming


Advantages of Machine Level Language
1. Programs in machine language is executed faster as it directly interacts with the computer.
2. Code conversion is not required if the program is written in machine language.
Disadvantages of Machine level Language9
1. Machine language is machine dependent. Hence, It will differ from machine to machine, making lt impo
to create a reliable program.
2. Code in machine language is in the form of binary, which makes it difficult to debug.
3. Machine language is machine dependent. Hence, It won't perform consistently over different machines.
CAssembly Language (2 GL)
(It is also a low-level language. It consists of mnemonics (symbols and abbreviations) and op-codes (numbersi
instead of binary digits) To convert it to machine language, a translator known as "Assembler" is used. MASM
(Macro Assembler) and SNOBOL implementation Language (SIL) are the examples of Assembly Language.
Note: An Assembler is atranslator program that converts codes written in an assembly language to
machine language code i.e., O's and 1's.
Advantages of Assembly Language
1. It uses statements instead of binary digits, so it can be easily
understood by the programmer.
2. Codes can be developed faster as this language uses
statements instead of binary digits.
3. It is easy to debug.
Disadvantages of Assembly Languageluo ceoqxs n6 ew ifbirtw tauoi ueon
1. This language is machine dependent so; it needs a translator program.
2. The programmers should have good knowledge of
mnemonics. 2911 9tuqmo3 sris i i
o)esbo bns elodme abvowo 92
High-Level Language
To overcome the inconvenience of Low Level Languages (machine
language and Assembly language), High Level
Language was developed.
rThis language is closer to human languages because programs are written in English
language or
operators, punctuators. Writing codes in High level language are easier to develop and modify symbols such as
Since it is amachine independent language, it has to be converted to machine
or interpreter for execution.
language with the help of a compiler
There are three main categories of high-level languages which are third
generation languages, fourth generation
languages and fifth-generation languages.
Third Generation Languages (3GL) bns eoss
In third generation languages, the programmer writes a series of English
like words that tell the computer what to
accomplish and how to do it.
Many third generation languages also use arithmetic operators such as * for
These English like words and arithmetic notations make it easy for a multiplication and + for addition.
of 4GL are BASIC, PASCAL, C, FORTRAN C++, Java, and C#.6moiprogrammer
to write the programs. Exampies
pen
Fourth Generation Languages (4GL)
The fourth generation languages also use English like
statements. They specify what the
without explaining how. A4GL is faster and takes less time for the program should accon
programmer to code. In fact, 4GLs are so e
Touchpad Computer Applications-IX
to usethat programmerss with very little programming knowledge can develop programs. Examples of 4GL are SQL,
Perl,Python,etc.

Fifth-generationlanguages (5GL)
Thefifth-generation languages, also known as natural languages are the programming languages that have visual
tools todevelop a program. Examples of fifth generation language include Mercury, OPSS, and Prolog. Natural
to use.
language is very easy
Natural languages are often associated with expert system and artificial intelligence. These systems are popular
inthe medical and scientific fields, but are not widely used in business applications.
For example, if a program is written in 4GL to get the names of students whose marks exceed 95, it might be
written as:
ertBCT LAST-NAME, FIRST-NAME FROM STUDENT WHERE MARKS > 95.
Anatural language version ot the same program might be written as:
TELL ME THE NAME OF STUDENTS WITH MARKS OVER 95.
Advantages of High-Level Languages
High-level language is user friendly.
High-level language is similar to English language, therefore it is easier to use.
High-level language requires less time to code.
High-level language is easier to maintain.
High-level language is problem oriented.
Programs written ina high-level language can be translated into machine language and therefore can run on
any computer with an appropriate translator.
It is independent of the machine on which it is used, i.e., programs developed in high-level languages can be
executed on any computer i.e.,they are machine independent.
Disadvantages of High-Level Languages
Ahigh-level language has to be translated into the machine language by a translator and thus it wastes a lot of
the computer time.
The object code generated by a translator might be inefficient as compared to an equivalent assembly language
program.

Comparision between Three Types of Computer Languages


Machine-Level Language Assembly Language High-Level language
| Uses binary digits for coding Uses mnemonics for coding Uses english like language and
symbols for coding.
|Directly understood by Computer Assembler is required to convert Compiler is used to convert codes
codes to machine language. tomachine language
Example: character 'A' is Example: to execute addition, it Example: for adding two numbers
uses the symbol 'Add' 10and 20
represented as 01100001
Add A B int s=a + b;

Source Code and Object Code


machine language using a compiler or
ne program written by a programmer in a text editor which is converted to
object
Interpreter is called the source code)It is also known as source program(The converted program is called
code. It is also known as object program.)
Principles of Object-Oriented Programming 15
Compiler
source code (high-level language program) into obiers
Acompiler is a translator program that converts the entire the whole program together.
the errors for
code (machine language) at once. It displays
Interpreter source code (high-level language program) inte
translator program that converts the entire
An interpreter is also a it. It translatess and executes one statement
at a time. It displave
(machine language) before executing
object code line only after the error is corrected.
goes to the next
the errors one line at a time and it
PROGRAMMINGPARADIGMS on their features.
refers to an approach used to classify the programming languages based
Procedure-Oriented
Programming paradigm
programming languages, there are two types of programming paradigms:
In high level Programming (0OP).
Programming (POP)and Object-Oriented
Procedure-Oriented Programming top-down
model for the beginners to learn programming. It follows a
Procedure-Oriented Programming is a data values.
emphasises on the methods or procedures rather than the
approach and mainly
programming code into Definition
Procedure programming splits the or methods
functions
events or procedures, also known as
a series of steps or instructions to be carried The programming language that breaks down
which contain
another. a programming assignment into a group of
out oneafter subprograms
known variables, data structures and
(FORTRAN, COBOL, BASIC are some commonly is called procedure-oriented
programming
Procedure-Oriented Programming languages.)
language.
Programming
Principles of Procedure-Oriented
principles:
POP depends on the following
approach.
1. It follows a top-down methods.
program into smaller programs known as functions or
2. It divides a large
instructions for execution.
3. It gives step by step
Programming
Advantages of Procedure-Oriented
advantages of Procedure-Oriented Programming:
Following are the algorithms.
reliable and tested
1. There are a lot of understand.
2. Program flow is
easy to followand languages.
is required as compared to other programming
memory
3. A smaller amount of
4. It uses functions. processors.
programs can be executed on different types of
5. The
Programming
Disadvantages of Procedure-Oriented
Procedure-Oriented Programming: method is used.
Following are the disadvantages of debugging is reguired in programs wherever that source code. t
globally, hence more exposing the data to the entire
1. Methods are shared operation rather than data, thus
on the
2. It emphasises more
reduces the data security.
objects.
3. It is hard to correlate it with real-world
emphasises
Object-Oriented Programming Bottom-Up. approach and
follows a
Object-Oriented programming is an another way of programming. It
more on on data rather than functions.

16 Touchpad Computer Applications-IX


programminto
number of entities, known as objects. It
It splits
the
increases the ability to deal with complex
softwareproblemS-particularly for developing and maintaining large real-life applications and data arrangements.
Usingthistechnique of writing source code makes it easy to create different objects from a common structure.
Thiscommon structure is usually called a blueprint or class and the objects that are created are called instances.
Java, C++, C#, Python are some commonly known Object-Oriented Programming languages.
Principles of Object-Oriented | Programming
Definition
poOPdepends the following principles:
On

11. Data Abstraction A programming model that depends on


2. Inheritance the theory of classes and objects, and gives
3. Polymorphism importance on data rather than functions .
4. Encapsulation is known as Object-Oriented Programming
They are also called the four pillars of Object-Oriented (oOP),)
Programming.
Advantages of Object-Oriented Programming
Following are the advantages of Object-Oriented Programming:
1 Each and every instance has properties that are exclusively for its own and cannot be used by other instances.
For example, if you have a Car blueprint, you can create many instances of car with different model names
such as Maruti Suzuki, Ford Figo and Ambassador, etc. Thus,a real-world picture can be explained.
2. It uses the concept of re-usability where code is written once and can be used multiple times using different
objects.
3. Maintaining and modifying existing code is much easier than trying to create new objects with minor changes.
4. Abstraction and Data hiding maintains the security of data.
5. Concept of Inheritance is a good feature for avoiding data redundancy.
Disadvantages of Object-Oriented Programming
Following are the disadvantages of Object-Oriented Programming:
1. Object-oriented program needs more lines of codes than Procedure-oriented program.
2. It needs more memory to execute at a high speed. Thus, its execution is slower than the conventional encoding
system.
3. It requires intensive testing.
4. Proper planning is necessary for Object-Oriented programming.
Difference between POP and 0OP
We have learnt about Procedure-Oriented programming and Object-Oriented programming. Now let us take a
look at the difference between them.

Procedure-Oriented Programming Object-Oriented Programming


Programs are divided into smallparts known as Programs are divided into small parts known as
functions or methods. objects.
It uses the concept of Top-Down approach. It uses the concept of Bottom-Up approach.
It deals with Algorithm It deals with data.
It is less secure It is more secure

Examples: C, Fortran Examples: Ct+,Java

Principles of Object-Oriented Programming


O
PRINCIPLES OFOBJECT-ORIENTED PROGRAMMING
As you know Encapsulation, Data Abstraction, Inheritance and Polymorphism are thefour pillars of
Programming. Let us learn about them in detail.
object-Oriente
Object oriented
Programming Concept
Encapsulation Polymorphism
Abstraction
Inheritance
Data

Encapsulation
Encapsulation is one of the basic concepts of
The wrapping up of data members and memberobject-oriented programming (0OP).
methods together into a single unit Methods Variables
is caled encapsulation.)lt means that data can be
accessed in the associated function CLASS
only and safe from outside interference and misuse thus leading to
the important Concept of Encapsulation
concept of 0OP known as data hiding.
Areal-life example is a capsule. In general, a capsule is a
small case or
shape which protects the drug inside it from getting contaminated withcontainer, especially round or cylindrical in
the dust particles from outside.
Similarly, Methods and Variables are enclosed within a unit called class.
Example of Encapsulation using class:
Both Data Members and Member Methods are
enclosed within the braces of the class country.
class country
Class name
String name;
String capital; Data Members
void input 0

name= "India" ; Member Methods


capital= "New Delhi";

void di splay )

System. out,println ("Name of the Country "+name):


System . out.println ("Name of Capital" +capital) :

18 Touchpad Computer Applications-iX


pataAbstraction
The word abstract means existing in thought as an idea but does not have atangiblee existence/Data Abstraction
isthe propertyby which the essential features of a class are represented without informingabout the background
detailss i.e., non-essential details are hidden from the user.)
For example, while travellingiin acar as a passenger, you think only of
reachingthe destination. But you never think of how the driver is
drivingthe car by pressing the clutch, break and accelerator as and
when required. Similarly, in case of the driver, he is only concerned
aboutthe steering wheel, accelerator, clutch and break. But he is least
interested in how they execute their functions
Similarly, in Object-oriented programming the key purpose of abstraction is to hide the
details that are not required from the users. Abstraction is like a query passed to a
database
which extracts the required data only leaving the rest. The main purpose is to reduce the
programming complexity. It is one of the most important concepts of 00Ps.
Let us understand with an another example, while using an Automated Teller Machine
(ATM), we only concentrate on executing the operations like cash withdrawal whiich
includes inserting the card, typing the pin number and amount of cash, etc. But we never
think of howexactly that operation is executed internally, so that we could get the service
correctly. Thus, it is a real-life example of abstraction as the detail functioning of the ATM
is hidden from the user.

Difference between Abstraction and Encapsulation


Abstraction Encapsulation
Issues at the design levelcan be solved. Issues at the implementation level can be
solved.
Abstraction is about hiding unwanted details In encapsulation the methods and data are
andshowing only the essential information kept in a single unit.
Abstraction focuses on what the information It hides the internal details of how an object
object must contain. does something for security reasons.
Inheritance
The procedure of generating a new class with the help of a class already created by using its properties and
functionality is said to be inheritance. It is one of the important pillars of Object-Oriented Programming.)
For example, in the real-world, a child inherits the properties (both materialistic and non-materialistic) of its
parents who intern has inherited from their parents.

INHERITANCE

Father Son
All the properties of father are inherited by his son.

Principles of Object-Oriented Programming


Let's understand with another example, a 'Car' inherits its properties from the class 'Motor Driven' which inherite
some of its properties fronm another class 'Vehicle'

Motor Driven Car

VEHICLE

Pull-Push Vehicle) Cycle

Concept of Reusability Definition


While developing a code, there may be a requirement to
create more than one class. Under such situation, we may Inheritance in Java is the method which
share the data members and methods of one class with allows one class to inherit the properties (data
another class. This helps to reduce the lines of code as well members and member methods) of another
as the complexity of the programs. This is the concept of class. )
reusability.
Essential terms related to Inheritance SUPER CLASS
Vertebrate
Super Class: Class from where features are inherited to another
class. It is also known as base class or parent class.)
Sub Class: Class which inherits the features of another class. It
is also called derived class or extended class or child class.)Apart
from features of Base Class, it may have its own features (data
members and methods).
For example, "Vertebrate" is super class and "Fish" and "Mammal"
are sub class as both Fishes and Mammals have backbones which
are the properties of the Super class Vertebrate.
SUB CLASS SUB CLASS
Fish Mammal

Polymorphism
The word Polymorphism comes from the Greek word mearning "many forms" ("poly" means many and "morphe'
means form). It is the ability which allows some variables to exhibits one or more forms.
For example, CAR sometimes is used as a family car, a car used by general public such as Taxi and also used as
Pool Car used by students of aschool.
(In object-oriented programming, polymorphism
is the feature of being able to allot a dissimilar
PRIVATE CAR
meaning so that a variable, a method, or an object
can have more than one form)Thus it is the capability
of altering the characteristícs and behaviour of
a variable and use it in many forms according to
the need of an object. It allows multiple objects CAR PUBLIC CAR

of different sub classes to be treated as objects of


a single super class, while automatically selecting
the proper methods to apply to a particular object
based on the subclass it belongs to. POOL CAR

Touchpad Computer Applications-iX


advantages of Polymorphismare:
1. Codes can be reused which reduces the size of the program.
2 It makes the program run faster as it reduces the number of comparisons needed tolook for a method.

Let's Revisit
.(Computer Languages can be claified in three different languages. They are Machine level Language, Assermbly
Level Language and High-Level Language.
(Procedure-oriented programming and Object-oriented prograrnming are two approaches of High-Level
languages. )
The programming language that breaks down a programming assignment into a group of variables, data
structures and sub programs is called Procedural oriented programming languages.
A
programming model that depends on the theory of classes and objects, and gives importance on data rather
than functions is known as Object-oriented programming.
(FORTRAN, COBOL, BASIC are some of the POP languages and Java, C++,C#, Python are some commonly
known OOP languages.)
Principles of 0OP's are Data Abstraction, Inheritance, Polymorphism and Encapsulation.
vThe wrapping up of data members and Member methods together into a single unit is called Encapsulation.
vbata Abstraction is the property by which the essential features are shown without knowing the background
details.

inheritance is the method which allows one class to inherit the properties (data members and member methods)
of another class.

WPolymorphism is the feature of being able to allot adissimilar meaning so that a variable, a method or an object
can have more than one form.

MIND DRILL
Solved Questions
A. Tick () the correct answer.
1. Aprogrammer needs a computer language to communicate with a
a. Another programmer b Computer
c. Animal d Anon-living thing
2. Which of the following is auser friendly computer language?
Machine Level language b Assembly Level Language
C High LevelLanguage d None of these

Principles of Object-Oriented Programming


3. Which of the following is known as natural language?
3 GL b 4 GL
Both a and b d. 5 GL
4. Which of the following paradigm follows the Top-down approach?
Procedural-Oriented Programming b Object-Oriented programming
C. Both a and b None of these
5. Which of the following is an example of Procedural Oriented language?
a. C++ b. Java
C. FORTRAN
d. None of these
6. Method and VariabBes are enclosed within a unit called
Class b data hiding
C. Inheritance d Data Abstraction
7. Which of the following principle shows the essential features without knowing the background details?
Encapsulation b Data Abstraction
C. Inheritance Polymorphismd
8. Wrapping up of data and Member functions together into a single unit is called
Encapsulation b Data Abstraction
C. Inheritance Polymorphism
d
9. Which of the following principle uses the concept of reusability?
a. Encapsulation b. Data Abstraction
C. Inheritance d. Polymorphism
10. Suppose data member of shape class is being used by Circle class. This is the concept of
a. Encapsulation b Data Abstraction
C Inheritance d. Polymorphism
Answers
1. b 2. c 3. d 4. a 5. c 6. a 7. b 8.a 9. d 10. c

B. Fill in the blanks.


1. Procedure-oriented program focuses on
2. The word Polymorphism comes from the Greek vocabulary meaning
3. An object is identified by its
needs more lines of code than procedural oriented program code.
5. means combining the code and data into a single unit.
6. Coding is written once and can be used multiple times using different objects is the concept of
7. Compilers and interpreters are referred to as
8. Object-oriented programming follows Approach.
9. Common structure is usually called a blueprint and the objects that are created called
10. The other name of Sub class is class.

Answers
5. Encapsulation
1. methods or procedures 2. many forms 3. characteristics 4.Object-oriented program
10. Derived
6. Re-usability 7.Language translator 8. Bottom-Up 9. instances

22 Touchpad Computer Applications-IX


Short Answer type questions.
What do you mean by Computer
1, language? Name any two computer languages.
AComputer language is the means by
which instructions and data are transmitted to the
computer languages are Java and C. computer. Two examples of
write any two advantages of Procedural Oriented
2. language
The two advantages of POP are as follows:
It iseasy to follow the path of
h A
program flow.
smaller amount of memoryis required than other types of
coding.
3 Write two Advantages of High-level language?
Ans. The two advantages of HLL are as follows:
It enables to write programs that are not
dependent to a particular type of computer hardware.
b. They are closer to human languages.
4 Write the difference between High Level Language and Low-Level
language.
Aar The difference between High Level
Language and Low-LevelLanguage are
High Level language Low Level language
High Level language is machine independent. Low Level language is machine dependent.
High Level language is human friendly so it is easy to Low Level language is machine friendly so it
is
understand for programmers. difficult to understand for programmers.
5. Name the four pillars of OOP.
Ans. Encapsulation, Data Abstraction, Inheritance and Polymorphism are the four pillars of
Object-Oriented Programming.
6 What is the difference between Super class and Sub class?
Ans. The difference between Super class and sub class are:

Super class Sub class


Class from where features are inherited to another class which inherits the features of another class.
class.

Super class is known as Base class. Sub class is also known as Derived clasS.w

7. Define source code.


Ans. The program code written by programmer in a text editor which is converted to machine language using compiler is
called source code. It is also known as source program.
8. What is meant by data abstraction?
Ans. Data Abstraction is the property by which the essential features of a class are represented without informing about the
background details i.e., non-essential units are hidden from the user.
9. Define the concept of Re-usability?
Ans. The concept of Re-usability is defined as the program is written once and it can be used multiple times.
10. Define Object-oriented programming.
Ans. Aprogramming model that depends on the theory of classes and objects, and gives importance on data rather than
functions is known as Object-oriented programming (OOP).
11. Define inheritance.
Ans. Inheritance in Java is the method which allows one class to inherit the properties (data members and member methods)
of another class.
12. Define Polymorphism.
Ans. In Object-oriented programming, polymorphism is the feature of being able to allot a dissimilar meaning so that a
variable, a method or an object can have more than one form.

Principles of Object-Oriented Programming


QUnsolved Questlons
A. Tick()the correct answer.
1. Which of the following is a pillar of Object-Oriented programming?
a. Super class b. Variable
C. Polymorphism d. Sub class
2. allows multiple objects of different subclasses to be treated as objects of a single super class.
a. Inheritance b. Method
C Polymorphism d. Super class
3. Abstraction and Data hiding maintains the of data as only necessary data is provided
a. Hacking b. Deleting
C. Security d. Breaking
4. Which of the folowing language is known as user friendly computer language?
Machine Level language b. Assembly Level Language
C High Level Language d. None of these
S. The wrapping up of data members and Member methods into a single unit is called Encapsulation.
separate b far
C. close d together

B. Fill in the blanks.


1. has global data sharing of functions.
2. converts source code into the object code.
3. divides the whole problem into smaller programs known as functions or methods.
4. Methods and are enclosed within a unit called class.
5. C++ is an example of

C. Short Answer type questions.


1. Write the difference between POP and OOP.
2. Define Polymorphism with a real-life example.
3. Write any 2 disadvantages of Object-oriented programming.
4. Write the difference between Polymorphism and Encapsulation.
5. Give a real-life example of a situation where polymorphism is used.
6. Write the difference between Assembly language and Machine level.
7. Write down two advantages of Polymorphism.
8. Write down any two disadvantages of Machine Level Language.

Touchpad Computer Applications-IX

You might also like