1 of 30

Std: 12th Science

English Medium

Subject: Computer

2 of 30

Chapter-6

  • Object Oriented Concepts

Topics:

  • What is Object and class
  • Class Diagram
  • Encapsulation
  • Data Abstraction
  • Messageing
  • Polymorphism
  • Aggregation and Composition
  • Aggregation vs. Composition
  • Inheritance
  • Composition vs. Inheritance

3 of 30

  • Programming Language basic

For any computer program the core elements are: (1) data (2) functions.

For eg. in a library management software

Details about individual books in a library like book title, author name, price etc. can be stored in a database with row and column structure is considered as data.

Library software can have few functions like book_issue(), book_return(), new_member() etc…..

Name

Title

Author

Price

You can win

You can win

Shiv Khera

500

4 of 30

  • Programming Languages

Computer languages are used to create various types of programs.

The way of programming can be divided into 2 categories:

1. Structural/Procedural languages

2. object oriented languages

In procedural languages the main focus is on writing functions or procedures which operates on data.

In object oriented languages the main focus is on object which contains data and procedure both.

5 of 30

Object oriented programming

Object oriented programming concepts started originating in 1960s and since mid 1980s it became the main programming paradigm.

In object oriented languages the main focus is on object which contains both data and functionality.

The popular object oriented languages are C++, Java, C#, VB.net and PHP.

A computer language is object-oriented if they support 4 specific object properties called……

  1. Data encapsulation
  2. Data abstraction
  3. Polymorphism
  4. Inheritance

6 of 30

What is object

Object word is taken from the real world examples….

In real world, objects are the entities of which the world is comprised.

In software we can have objects like individual books in library software, individual students and teachers in school management software.

All object have unique identity and are distinguishable from each other.

Each and every object contains one or more properties(attributes) and methods(behaviors).

Object

Properties or Attributes

Methods or Behaviors

7 of 30

Object

Object contains various characteristics known as properties.

For eg. Every person has characteristics like name, address, city, gender etc…

Each and every property of an object contains a value known as its state.

For eg. Name = “Ram” city = “Surendranagar”

Value of a property can also be known as its state.

Objects differ from each other by their states.

Every object can have behaviors attached with them……

For eg. A book can be issued, so book_issue() is a property.

Book1

Book_title, author_name, price

Display(), book_issue()

The attributes and behavioral methods associated with an object are collectively referred to as its members or features.

Property names

Value of property

or its state

Objects should be meaningful to the application. For eg. In railway reservation application objects are trains, passengers, tickets, or station.

8 of 30

class

Class can be considered as a blueprint for various objects.

A class is a template for multiple objects with similar features.

It describes a group of objects with similar attributes and common behaviors.

For example we have a class named person describing common attributes and behavior.

Name

City

Gender

Birthdate

profession

Class Person

Ram

Snagar

Male

8/8/2001

Teacher

Shyam

Surat

Male

1/3/2000

Professor

Jam

Jamnagar

Male

1/4/2000

Doctor

object Person 1

object Person2

object Person3

9 of 30

Class Diagram

  • The class diagram presents a collection of classes, constraints and relationship among classes.
  • Unified modeling Language(UML) can be used to create models of object-oriented software to help with design of an application.
  • UML is a visual modeling language defined and maintained by the Object Management Group(OMG).
  • The purpose of the class diagram is to model the static view of an application.

10 of 30

Std: 12th Science

English Medium

Subject: Computer

Chapter-6

Previous video Lecture

  • What is Object and class

  • Class Diagram

11 of 30

Class Diagram representation

  • In class diagram, a class is represented with an icon using a rectangle split into three sections to present name, attribute and behavior.
    • Name of the class in the top section
    • Attributes or properties of the class in the middle section
    • Behavior or operations or methods of the class in the bottom section

Class Name

Visibility attribute: data type = initial value

Visibility operation (argument list): return type

12 of 30

Class

Person

name: string

city:string

gender: char = ‘M’

-birthdate: date

profession: string

setBirthdate(d:int, m:int, y:int): date

changeCity (newCity:string) : string

display()

13 of 30

Syntax of Attribute

[<visibility>] <attribute name> [: <attribute data type> [ =<initial value>]]

Examples

Name : string

-balance : float = 0.0

Gender : char = ‘M’

Here items written in pair of square3 brackets [] are optional

Items written in pair of angle brackets <>, are compulsory.

14 of 30

Visibility

[<visibility>] <attribute name> [: <attribute data type> [ =<initial value>]]

Visibility can be of 4 types:

Private(-)

Protected(#)

public (+)

Package(~)

- balance : float = 0.0

15 of 30

Syntax of behavior

[<visibility>] <method name> (parameter list separated by comma): <return type>

Examples

setBirthdate( d:int, m:int, y:int) : date

16 of 30

Object Oriented programming features

  • Encapsulation

  • Data Abstraction

  • Polymorphism

  • Inheritance

17 of 30

Encapsulation

  • For any computer program, 2 core elements are data and functions.
  • Structural/procedural programming views these 2 core elements as two separate entities, whereas object-oriented programming views them as single entity.
  • This mechanism of wrapping data and method into a single unit is known as encapsulation.
  • Encapsulation allows to establish protection where private members of the class are not available directly to outside world.
  • If necessary, the data can be made available via public methods.
  • Thus encapsulation provides data hiding capability.
  • It keeps data safe from unintended actions.

18 of 30

Data Abstraction

  • Data abstraction is a process of representing the essential features of the objects without including implementation detail.
  • Abstraction is a concept that hides the complexity.
  • For example we can turn television set on and off, change the channel. But we do not know its internal details like how it receives signals. So there is a separation of internal implementation of tv set from its external interface.
  • So data abstraction is a technique that relies on the separation of interface and implementation.
  • It is usual in our daily work also. For example we use sqrt() function to find out square root of any number but how it is done internally that is hidden technique.

19 of 30

Messaging

  • In object-oriented terminology, a call to a method is referred to as a message.

abc() { …. Method code… }

abc()

20 of 30

Polymorphism

  • Polymorphism means ‘many forms’.
  • Polymorphism contains 2 things: method overloading and operator overloading.

Method overloading

  • Assume that we have written a function named ‘max()’ that finds maximum of two numbers. It takes 2 integers as parameters and returns maximum as integer value.
  • Structural programming will not allow to create another function with the same name as ‘max()’, where as an object oriented programming allows to create several functions with the same name but all the functions must have different signature. This is called method or function overloading.
  • So max(int x, int y) and max(int x, int y, int z) are functions with different signatures and can be created in an object oriented programming language. Signature means either number of parameters or their data types or return type must be different.

21 of 30

Polymorphism

Operator overloading

  • Object oriented programming also allows writing expression using operators on objects.
  • For example we can use expression ‘date1 – date2’ where both operands are objects of class ‘date’.
  • Here operation ‘-’ is performed in a way different than performing subtraction on two numbers. Like ‘n1 – n2’.
  • Here n1 and n2 are numbers.
  • This type of polymorphism is achieved through operator overloading.
  • Thus, polymorphism is achieved using two types of overloading: function overloading and operator overloading.
  • In short, the capability of using same names to mean different things in different contexts is called overloading.

22 of 30

Std: 12th Science

English Medium

Subject: Computer

Chapter-6

Previous video Lecture

Visibility options

Encapsulation

Data Abstraction

Polymorphism

23 of 30

Aggregation and Composition

  • When objects of one class are composed of objects of other class, it is called aggregation or composition.
  • It represents ‘has-a’ or ‘a-part-of’ relationship between classes.
  • To understand this let us take the previous example of person class.

Person

name: string

city:string

gender: char = ‘M’

-birthdate: date

profession: string

setBirthdate(d:int, m:int, y:int): date

changeCity (newCity:string) : string

display()

Previously

Created

class

Person

Nm: Name

Addr: Address

Gender: char = ‘M’

Birthdate: date

Profession: string

setBirthdate(d:int, m:int, y:int): date

changeCity (newCity:string) : string

display()

Address

house: string

Street: string

City: string

State: string

Pincode: int

fullAddress(): string

display()

Name

firstName: string

middleName: string

lastName: string

fullName(): string

display()

2 new classes name and address are created first. Object of person class are composed of

Object of name and address class.

24 of 30

Aggregation vs. Composition

  • Aggregation represents non-exclusive relationship between 2 classes.
  • In aggregation, the class that forms part of the owner class can exist independently.

P1:person

Nm: Name(Ram, Mohan, Joshi)

Addr: Address(10, Arun Society….)

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: professor

….

Address

house: 10, Arun…

Street: Dalmill

City: Snr

State: Guj

Pincode: 363001

n1:name

fname:Ram

mName:Mohan

lName:Joshi

n1:name

fname:Shyam

mName:sundar

lName:Dave

P2:person

Nm: Name(Shyam, Sundar, Dave)

addr: Address(10, Arun Society….)

Gender: char = ‘M’

-birthdate: 10-03-2001

Profession: professor

….

25 of 30

Aggregation vs. Composition

  • Aggregation is non-exclusive relationship between two classes. It means both classes are independent.
  • Here name and address classes are known as part class. Whereas person class is known as owner class.
  • Here Independent means life of an object of the part class is not determined by the owner class.
  • In our example the object address is shared by two different objects (p1 and p2) of person class.
  • So if we remove object: p1 ie. Of owner class person, it will not remove the object address related to the p1 because it is non-exclusive relationship that can prevail independently.
  • Composition is a strong type of aggregation.
  • Here both objects p1 and p2 have separate part objects n1 and n2.
  • If we remove p1 then it should remove n1 also.

26 of 30

Aggregation vs. Composition

  • Composition represents exclusive relationship between 2 classes.
  • Composition is a strong type of aggregation where the lifetime of the part class depends on the existence of the owner class.
  • If an object of aggregating class is deleted, its part class object also will get deleted.
  • For example when an object of class person is deleted, the object of class name is also deleted.
  • Name is associated exclusively with single person as shown between name and person. Where as address is non-exclusive because it can be shared.

P1:person

Nm: Name(Ram, Mohan, Joshi)

Addr: Address(10, Arun Society….)

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: professor

….

n1:name

fname:Ram

mName:Mohan

lName:Joshi

Address

house: 10, Arun…

Street: Dalmill

City: Snr

State: Guj

Pincode: 363001

Aggregated/

subject/

part class

Aggregating/owner/

Whole class

Aggregated/

subject/

part class

Composition

Aggregation

27 of 30

Aggregation vs. Composition

  • The class that contains objects of other class is known as owner class or whole class or aggregating class. For eg: person class.
  • The class that is contained in owner class is known as aggregated class or part class or subject class.
  • Aggregation is represented using blank diamond sign, whereas composition is represented using filled diamond sign.

P1:person

Nm: Name(Ram, Mohan, Joshi)

Addr: Address(10, Arun Society….)

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: professor

….

n1:name

fname:Ram

mName:Mohan

lName:Joshi

Address

house: 10, Arun…

Street: Dalmill

City: Snr

State: Guj

Pincode: 363001

Aggregated/

subject/

part class

Aggregating/owner/

Whole class

Composition

Aggregation

Aggregated/

subject/

part class

28 of 30

Inheritance

  • Inheritance is generally referred to as ‘is-a-kind-of’ relationship between two classes.
  • It is applicable when one class is ‘a-kind-of’ other class.
  • For eg: teacher is a kind of person.
  • So, all the attributes and methods of class person are applicable to class teacher also.
  • So ‘teacher’ class inherits all attributes and behavior of class ‘person’. ‘teacher’ class may also have additional attributes like subject and methods like taking lectures etc.

Person

Nm: string

City: string

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: string

Display()

Address

house: 10, Arun…

Street: Dalmill

City: Snr

State: Guj

Pincode: 363001

29 of 30

Inheritance

  • Inheritance refers to the capability of defining a new class of objects that inherits the characteristics of another existing class.
  • In object oriented terminology, new class is called sub class or child class or derived class, whereas the existing class is called super class or parent class or base class.
  • Here, person is a superclass and teacher is a subclass.
  • The data, attributes and methods of the super class are available to objects in the sub class without rewriting their declarations.
  • This feature provides reusability where existing methods can be reused without redefining.

Person

Nm: string

City: string

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: string

Display()

Teacher

Subject: string

Lecture(d:date,t:time)

Display()

30 of 30

Inheritance

  • In class diagram, inheritance is represented using an arrow pointing to superclass.

Person

Nm: string

City: string

Gender: char = ‘M’

-birthdate: 20-09-2003

Profession: string

Display()

Teacher

Subject: string

Lecture(d:date,t:time)

Display()

sub class or child class or derived class

super class or parent class or base class.