1. Introduction to Object Oriented Programming Concepts

Lesson 1

Elementary Concepts of Objects and Classes

1. Two Programming Styles
2. Concepts Related to Objects and Class
3. Principles of OOP
4. Descriptions on Objects and Class

1. Two Programming Styles

Programs can be written in two programming styles:
Procedure Oriented Programming and Object Oriented Programming.

Procedure Oriented Programming

POP focuses on writing instructions that manipulate data. Programs are written using procedures / functions. Examples: C and Pascal.

An example for Procedure Oriented Program in C to compute area of two rectangles using two functions.

Object Oriented Programming

It organizes code into classes, variables and methods. Programs are done using objects. Common object-oriented languages includes Java, C++, and Python.

An example for Object Oriented Program in Java to compute area of two rectangles using two objects

2. Concepts Related to Objects and Class

Definitions, examples and software terms are given.

Class

Definition: A class represents a set of objects that share common characteristics and behaviour.
E.g.: Rectangle is a class and its objects are different rectangle shapes. Their common characteristics are length and breadth. One of the behaviour is finding area.
Software term: Class. A class is defined with instance variables and Member methods.

Object

Definition: Object is an identifiable entity with some characteristics and behaviour.
E.g.: A rectangle object is with its characteristics length and breadth and its behaviour is finding area.
Software term: Object. It is created using its class with a name rect, s, ob etc.

Characteristics

Definition: Characteristics is defined as the unique things that an object have.
E.g.: Length and breadth are characteristics that a rectangle object have.
Software term: Software terms of characteristics are instance variables and data members.

State

Definition: State of an object can be defined as the values of its characteristics at a given point of time.
E.g.: If a rectangle object’s length is 10 and breadth is 8 then the states of the characteristics are 10 and 8. If these are changed to 15 and 12, then the states are 15 and 12.
Software term: Data, literal, constant and value.

Behaviour

Definition: Behaviour is action performed by an object with its characteristics.
E.g.: Finding area is one of the behaviour of an object of Rectangle class.
Software term: Method and function. E.g.: findArea().

Message

Definition: Message is a mechanism to communicate to an object.
E.g.: To activate a behaviour of an object we have to call the behaviour using object name.
Software term: Method call. E.g.: rect1.findArea().

3. Principles of OOP

Principles of OOP are abstraction, encapsulation, inheritance and polymorphism.
(Definitions, examples and uses are given)

Abstraction

Definition: Abstraction refers to the act of representing essential features without including the background details or explanations in a class.
E.g.: A student object has so many characteristics like roll, name, marks, caste, address etc. To find result all these characteristics are not required. The essential features are roll, name and marks. All other background details can be avoided.
Use: Memory can be saved. Programming effort can be lessened.

Encapsulation

Definition: Encapsulation refers to the act of wrapping up of data and functions that operate on data, into a single unit called class.
E.g.: The student class is defined with the essential variables like roll, name, mark etc. and methods to calculate average, find result etc. as single unit using braces.
Use: Each problem can be classified and problems can be solved easily without interference of data and methods of other problems. So data are secure.

Inheritance

Definition: Inheritance is the process by which objects of one class acquire properties of another class. Class that acquire properties is known as sub class or derived class. Class that has the acquired properties is known as base class or super class.
E.g.: A class named Area is defined to find area of rectangle with data members l and b and method findArea() as base class and another class Volume is defined as sub class with data member h and method findVolume(). To find volume the l and b can be acquired from the Area class. Using an object of Volume class area and volume can be found.
Use: Repetition of variable declarations and method definitions can be avoided. So memory can be saved and programming effort can be lessened.

Polymorphism

Definition: Polymorphism is the ability to behave differently in different contexts by methods, constructors and operators.
E.g.: Methods with same name like area() can be defined to find area of circle, square, rectangle etc. with different method definitions for different results. But it requires different parameters.
Use: Programmer need not to bother about the method name.

4. Some Descriptions on Class and Objects

Following are some descriptions on classes and objects.
Note: The contents of all following descriptions are almost same but the presentations are different.

1. Class is a modelling for entity /
Class is a template to create objects

A class is defined with characteristics and behaviours of its objects (entities) as a model. Using this model (class) so many objects can be created. When objects are created, all objects get the characteristics and behaviour defined in the class.
E.g.: A rectangle object’s characteristics are length and breadth, and its behaviour is finding area. Using the behaviour (method) area of it can be found.

2. Class is an object factory /
Class is a specification for objects

An object has two features – attributes and behaviours. A class is a mould with attributes and behaviours to create objects. We can create any number of objects using this mould, the class. So a class is an object factory.
E.g.: After defining a Rectangle class with l and b as its characteristics and area() as its behaviour we can create so many rectangle objects and find area of all of them.

3. Class is a blueprint for objects

In a class the essential characteristics and behaviours of an object are defined as a blueprint (plan). Using this blueprint we can create so many objects as we require. All these objects will have the characteristics and behaviours which are defined in the class.
E.g.: Using a blueprint of a car we can create so many cars.

4. Object encapsulates state and behaviour

A class is defined with characteristics and behaviours of its objects. When an object is created, the object gets all characteristics and behaviours defined in the class. These characteristics hold certain values, which are states. So the object encapsulates state and behaviour.
E.g.: A rectangle object’s characteristics are length and breadth, and its state can be 10 and 8. One of its behaviour is finding area. All these are defined in a class named Rectangle.

5. Class as user-defined data type

A class is defined with characteristics and behaviours as its members by a user. This class is used as data type to create objects. Objects are varaiables of this data type (class).
E.g.: Rectangle rect = new Rectangle(); The Rectangle is a data type defined by the user and the object rect is its variable. The = initialize 0 to l and b of rect.

6. Class is a composite data type

A class is composed with various characteristics and behaviours as its members. So this class is a composite data type to create objects. Objects are varaiables of this data type (class).
E.g.: Rectangle rect = new Rectangle(); The Rectangle is a data type composed with length, breadth and findArea() method. The object rect is its variable.

7. Objects are instances of a class

A class is an explanation about the characteristics and behaviours of an object. Object is an example for this explanation. That means class is a concept about the objects, and these objects are the real things. So objects are instances of a class.
E.g.: We can say that the concept of a rectangle is that it is a shape with length and breadth. When we draw one with length and breadth as 10 cm and 8 cm as an example then the real rectangle object happens.

8. Class is an abstraction for sets of objects

An object can have many characteristics and behaviours. For a particular purpose all these characteristics and behaviours need not to be defined. So a class is defined with essential characteristics and behaviours for a particular purpose without including background details. With this abstract (concept) of class definition, sets of objects can be created. So a class is abstraction for sets of objects.
E.g.: A student object has so many attributes like roll, name, marks, caste, gender, address etc. To find result we need only roll, name and marks. So we can define a Student class with these essential features.