Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Students frequently turn to Computer Class 12 GSEB Solutions and GSEB Computer Textbook Solutions Class 12 Chapter 6 Object-Oriented Concepts for practice and self-assessment.

GSEB Computer Textbook Solutions Class 12 Chapter 6 Object-Oriented Concepts

Question 1.
List the features supported by object-oriented programming language.
Answer:

  • In the “real” world, objects are the entities of which the world is comprised.
  • Some objects can be concrete things like person, car or a coffee cup.
  • Other objects may be abstract that do not represent things which can be touched or seen; for example, concepts like date and time.
  • All objects have unique identity and are distinguishable from each other.
  • For example, every person has characteristics like name, city, gender, birth-date, profession.
  • In object-oriented terminology, such characteristics are known as properties or attributes.
  • To uniquely distinguish one person from other, we use the value of these characteristics.
  • The name ‘Ram’ and ‘Shyam’ specify two different persons.
  • When two persons have same name, they may be distinguished using other attribute like birth-date.
  • In this way, to identify the objects, we use the value of these attributes.
  • These values are called state.
  • Additionally, there is a behaviour associated with objects.
  • For example, person takes birth, gets name, changes location.
  • The behaviour is also known as method.
  • State of the object can change due to its behaviour.
  • Thus, any real object can be described in terms of what it is called (identity), what it is (its state) and what it does (its behaviour).
  • In object-oriented programming, attributes that describe the object are also referred to as data fields.
  • The data attributes and behavioural methods associated with an object are collectively referred to as its members or features.
  • When any software application is designed, objects to be considered should be meaningful to the application.
  • For example, in railway reservation application, meaningful objects are train, passenger, ticket or station.
  • Objects like car, computer and watch may be irrelevant here.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Question 2.
Distinguish between class and object.
Answer:
Object

  • In the “real” world, objects are the entities of which the world is comprised.
  • Some objects can be concrete things like person, car or a coffee cup.
  • Other objects may be abstract that do not represent things which can be touched or seen; for example, concepts like date and time.
  • All objects have unique identity and are distinguishable from each other.
  • For example, every person has characteristics like name, city, gender, birth-date, profession.
  • In object-oriented terminology, such characteristics are known as properties or attributes.
  • To uniquely distinguish one person from other, we use the value of these characteristics.
  • The name ‘Ram’ and ‘Shyam’ specify two different persons.
  • When two persons have same name, they may be distinguished using other attribute like birth-date.
  • In this way, to identify the objects, we use the value of these attributes.
  • These values are called state.
  • Additionally, there is a behaviour associated with objects.
  • For example, person takes birth, gets name, changes location.
  • The behaviour is also known as method.
  • State of the object can change due to its behaviour.
  • Thus, any real object can be described in terms of what it is called (identity), what it is (its state) and what it does (its behaviour).
  • In object-oriented programming, attributes that describe the object are also referred to as data fields.
  • The data attributes and behavioural methods associated with an object are collectively referred to as its members or features.
  • When any software application is designed, objects to be considered should be meaningful to the application.
  • For example, in railway reservation application, meaningful objects are train, passenger, ticket or station.
  • Objects like car, computer and watch may be irrelevant here.

Class

  • From the example of the object person, it can be easily seen that various objects possessing same characteristics and behaviour differ from each other only in the state (value of the data) that they hold.
  • Object-oriented system used the concept of class that enables to express the set of objects that are abstractly equivalent, differing only in the values of their attributes.
  • 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 behaviour.
  • Objects in the same class share a common semantic purpose.
  • Thus, class is a general concept used to embody all the common features of a particular set of objects.
  • For example, we have a class named ’Person’ describing common attributes and behaviour of all persons.
  • Individual persons are then objects and are identified by the state; that is the value of their attributes.
  • Figure shows difference between the class and its objects

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 1

Question 3.
Differentiate between ‘Encapsulation’ and ‘Data Abstraction’.
Answer:
Encapsulation

  • For any computer program, two or more elements are data and functions.
  • Structured/Procedural programming views these two core elements as two separate entities; whereas object-oriented programming views them as single entity.
  • In procedural programming, data can be altered by any component of the program. It is not protected from modification.
  • In object-oriented programming, this problem pan be solved using encapsulation.
  • Here, data and the methods that manipulate data are guarded against modification or misuse by other components of the program.
  • This mechanism of providing protection to data and methods of a program is called encapsulation.
  • This is possible by wrapping data and methods into a single unit known as class and declaring them as private.
  • Private members of the class are not available directly to outside world.
  • If necessary, the data is made available via public methods.
  • Thus, encapsulation provides data hiding capability.
  • Encapsulation keeps the data safe from unintended actions and inadvertent access by outside objects.
  • Unlike procedural programming where common data areas are often used for sharing information, object-oriented programming discourages direct access to common data (other than the use of global variables) by other programs.
  • Only the object that “owns” the data can change its content.
  • Other objects can view or change this data by sending message to the “owner”.

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; it says what it does, but not how it is done.
  • Take one real example of a television set. The television set can be turned off and on, channel can be changed, volume can be adjusted. But the user does not know the internal details like how it receives signals over the air or through a cable, how it translates them and finally displays them on the screen.
  • There is a clear separation of internal implementation of television set from its external interface.
  • One can play with it via external interfaces like the power button, channel changer and volume control without having any knowledge of its internals.
  • Data abstraction thus is a technique that relies on the separation of interface and implementation.
  • It is not a new concept in programming.
  • Most people already practice it up-to some degree, probably without realizing it. For example, when the programmer user function like sqrt(25) and printf(“Hello world”), in C programming, one never thinks how they are implemented.
  • A user defined function with necessary input data parameters also provides data abstraction.
  • Consider a C program using structure for declaring ‘date’ type of data.
  • Here date is composed of three elements; day, month and year. Use primitive integer data type for these elements.
  • Consider a function named ‘dateDiff that takes two dates as parameters and returns the number of days between two dates.
  • User of this function is not supposed to know how this difference is calculated.
  • It is necessary to know only the signature of the function; that is the name of the function, number and type of parameters and the return type of parameters.
  • If there is any change in the process of finding difference, it has no effect on the part of the program using this function.
  • Thus data abstraction provides the skeleton or templates for our use. The system hides certain details of how data is stored, created and maintained.
  • All that is visible to the rest of the world is the abstract behaviour of the data type; details of how behaviour is implemented are hidden, so that they can be changed as per need without impacting others.
  • Abstract Data Types (ADT) or structures (struct) in C/C++, classes / lava are examples for data abstraction.
  • In ADT, it is simply defined a data type and a set of operations on it. The implementation of operations is not seen.
  • The basic difference between data encapsulation and data abstraction is: Encapsulation protects data by making them inaccessible from outside and abstraction enables to represent data in which the implementation details are hidden (abstracted).

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Question 4.
What do you mean by polymorphism ? Name two type of overloading that may be supported to achieve polymorphism.
Answer:

  • Polymorphism means ‘many forms’. There may be different forms of single method of operation.
  • Assume that there is a written function named ‘max’ that finds maximum of two numbers.
  • It takes two integers as parameters and returns maximum as integer value.
  • Now suppose the user wants to add one more function named ‘max’ that finds the maximum of integer elements stored in an array. It takes array and its size as parameters and return maximum integer.
  • In some programming languages, it is not possible to define more than one function with the same name.
  • But in object-oriented programming it is possible as long as the method differs in signatures (number and type of parameters).
  • Object-oriented programming also allows defining more than one method having same name but different signatures in a single class. This feature is known as function or method overloading.
  • Object-oriented programming also allows writing expression using operators on objects.
  • For example, the expression ‘datel-date2’ can be used where both operands are objects of class ‘date’. Here operation ‘-‘ is performed in a way different than performing subtraction of two numbers; say nl-n2. Here same operation is given different meanings depending upon the data type of operands used.
  • This type of polymorphism is achieved through operator overloading.
  • Thus, polymorphism is achieved using two types of overloading: function overloading and operator overloading.
  • In general, the capability of using same names to mean different things in different contexts is called overloading.

Question 5.
Explain the use of aggregation and composition.
Answer:

  • 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. For example, motherboard is a part of computer.
  • We all know that a computer is composed of many parts like motherboard, screen, keyboard and mouse. A screen itself may be a class with attributes like length, width and model. Similarly motherboard may be defined as a class with attributes like model and company. When we define class ‘computer’ it will contain attributes that are objects of class ‘motherboard’ and screen’. Thus it implies containment.
  • For example, modify the class ‘Person’ discussed earlier. First of all, define two new classes ‘Name’ and ‘Address’ as shown in figure Class ‘Name’ has attributes first name, middle name and last name. Class ‘Address’ has attributes house (that refers to details of house), street, city, state and pin code.
  • Modify class ‘Person’ and change the name of the attributes name and address to nm and addr respectively. The data type of attributes nm and addr is class ‘Name’ and ‘Address’ respectively. Thus class ‘Person’ contains objects of class ‘Name and ‘Address’.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 2

Question 6.
When should one use inheritance ? Give example.
Answer:

  • Inheritance is generally referred to as ’is-a-kind-of relationship between two classes.
  • It is appropriate when one class is ’ a kind of other class. For example, teacher is a kind of person.
  • So, all the attributes and methods of class ’Person’ are applicable to class ’Teacher’ also. In other words, class ’Teacher’ inherits all attributes and behaviour of class ’Person’. Class ’Teacher’ may have additional
  • attributes like subject and methods like taking lectures of the subject.
  • In such scenario, class ’Teacher’ can be defined using ’Person’.
  • 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.
  • 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 method can be reused without redefining.
  • Additionally new data and method members can be added to the sub class as an extension.
  • It also allows the methods to be refined in sub class as per need.
  • In class diagram, inheritance is represented using an arrow pointing to super class as shown in figure In this example, ’Person’ is a super class and ’Teacher’ is a sub class.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 3

  • Generalization is another name for inheritance or ‘is a’ relationship.
  • It refers to a relationship between two classes where one class is a specialized version of another. Common attributes and methods are defined in super class.
  • Sub class is a specialized version with additional attributes and methods.
  • Sometimes, there may be a classical hierarchy of inheritance between classes. For example, class ‘Employee’ can be derived from class ‘Person’ then class ‘Teacher’ can be derived from ‘Employee’. Here employee is a kind of person and teacher is a kind of employee.
  • Such type of inheritance is known as multilevel inheritance. An example of multilevel inheritance is

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 4

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Question 7.
Explain different types of inheritance.
Answer:

  • Inheritance is generally referred to as ’is-a-kind-of relationship between two classes.
  • It is appropriate when one class is ’ a kind of other class. For example, teacher is a kind of person.
  • So, all the attributes and methods of class ’Person’ are applicable to class ’Teacher’ also. In other words, class ’Teacher’ inherits all attributes and behaviour of class ’Person’. Class ’Teacher’ may have additional attributes like subject and methods like taking lectures of the subject.
  • In such scenario, class ’Teacher’ can be defined using ’Person’.
  • 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.
  • 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 method can be reused without redefining.
  • Additionally new data and method members can be added to the sub class as an extension.
  • It also allows the methods to be refined in sub class as per need.
  • In class diagram, inheritance is represented using an arrow pointing to super class as shown in figure 6.8. In this example, ’Person’ is a super class and ’Teacher’ is a sub class.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 3

  • Generalization is another name for inheritance or ‘is a’ relationship.
  • It refers to a relationship between two classes where one class is a specialized version of another. Common attributes and methods are defined in super class.
  • Sub class is a specialized version with additional attributes and methods.
  • Sometimes, there may be a classical hierarchy of inheritance between classes. For example, class ‘Employee’ can be derived from class ‘Person’ then class ‘Teacher’ can be derived from ‘Employee’. Here employee is a kind of person and teacher is a kind of employee.
  • Such type of inheritance is known as multilevel inheritance. An example of multilevel inheritance is

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 4

Question 8.
Choose the most appropriate option from those given below :

1) In Object-oriented methodology, the focus is on which of the following entities ?
(A) Data
(B) Functions
(C) Objects
(D) All of the above
Answer:
(C) Objects

2) Which of the following best suits to Java ?
(A) A procedural programming language
(B) An Object-oriented programming language
(C) A Query language
(D) All of the above
Answer:
(B) An Object-oriented programming language

3) Which of the following is used to distinguish objects from each other ?
(A) Attributes
(B) State
(C) Behaviour
(D) All of the above
Answer:
(B) State

4) Which of the following is used to define common features of similar objects ?
(A) Class
(B) Object
(C) Methods
(D) All of the above
Answer:
(A) Class

5) Which of the following is not a visibility symbol ?
(A) ~
(B) *
(C) #
(D) –
Answer:
(B) *

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

6) Which of the following is provided using encapsulation ?
(A) Data protection
(B) Data sharing
(C) Separation of data and methods
(D) All of these
Answer:
(A) Data protection

7) Which of the following is enabled by data abstraction ?
(A) Data protection
(B) Data hiding
(C) To hide implementation details of method manipulating the data
(D) All of these
Answer:
(C) To hide implementation details of method manipulating the data

8) With which of the following options polymorphism cannot be achieved ?
(A) Method overloading
(B) Operator overloading
(C) Data hiding
(D) All of these
Answer:
(C) Data hiding

9) An aggregation model refers to which of the following relationships ?
(A) ‘is-a’ relationship
(B) ‘is-like’ relationship
(C) ‘a-part-of’ relationship
(D) All of these
Answer:
(C) ‘a-part-of’ relationship

10) An inheritance model refers to which of the following relationships ?
(A) ‘is-a’ relationship
(B) ‘has-a’ relationship
(C) ‘a-part-of relationship
(D) All of these
Answer:
(A) ‘is-a’ relationship

11) In class diagram, composition is represented using which of the following symbols ?
(A) Empty diamond symbol
(B) Filled diamond symbol
(C) Empty triangle symbol
(D) All of these
Answer:
(B) Filled diamond symbol

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Computer Class 12 GSEB Notes Chapter 6 Object-Oriented Concepts

Introduction

  • Object-oriented programming concepts started originating in the 1960s.
  • Since mid 1980s, it had become the main programming paradigm used in the creation of new software.
  • It was developed as a way to handle the rapidly increasing size and complexity of software systems and to make it easier to modify these large and complex systems over time.
  • Some of the popular programming languages that support object-oriented programming are C++, Java, C#, VB.net, ASP.net and PHP.
  • The way of programming can be divided into two categories namely structure / procedural and object-oriented.
  • In procedural programming, the focus is on functions which operate on data.
  • For example, for library application software, there are all processes of library application and focus on the modules like student registration, book issue, book return, fine calculation.
  • In object-oriented paradigm, the focus is on objects which contain both data and functionality together.
  • For library application, our focus is on the objects involved in the application.
  • Here there are objects like student, book and librarian. Also there will be an association between such objects. For example, student returns book.
  • The power of object-oriented programming language enables the programmer to create modular, reusable and extendable code.
  • As a result, programmer can formulate a program by composition and modification of existing modules.
  • Flexibility is gained by being able to change or replace modules without disturbing other parts of code.
  • Software development speed is gained by reusing and enhancing the existing code.
  • Object-oriented programming uses as its fundamental building block.
  • Similar objects are classified using a concept of class.
  • A computer language is object-oriented if they support four specific object properties called abstraction, encapsulation, polymorphism and inheritance.

Introduction to Class Diagram

  • The class diagram presents a collection of classes, constraints and relationship among classes.
  • Unified Modelling 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).
  • UML specifies several diagrams for representing different aspects of a software application.
  • The purpose of the class diagram is to model the static view of an application.
  • The class diagrams are the only diagrams which can be directly mapped with object-oriented languages and thus widely used among software developers.
  • In class diagram, a class is represented with an icon using a rectangle split into three sections to present name, attributes and behaviour as follows :
    1. Name of the class in the top section.
    2. Attributes or properties of the class in the middle section.
    3. Behaviour or operations or methods of the class in the bottom section.
  • Figure shows the pictorial representation of a class using UML convention, while figure shows the class diagram of Person.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 5

  • The class Person is intended to provide information that is useful in understanding the role of the class in the context of class diagram.
  • It does not have to contain every attribute and operation of the class.
  • As seen in figure in UML notation, an attribute is declared using following syntax :
    [<visibility>]attribute name> (:<attribute data type> [=<initial value>]]
  • Here, items written in pair of square brackets [ ] are optional and user is supposed to specify the values for items enclosed in angle brackets < >. Visibility can be private, protected, public or package represented using symbols #, + and ~ respectively.
  • Attribute generally refers to a variable.
  • Data type and initial value identify the type of data stored and its value at the start of the program.
  • Only attribute-name is mandatory and all other items are optional.
  • Some examples of declaring attribute are as follows :
    name : string
    -balance : float = 0.0
  • As seen in figure In UML notation, an operation is declared using following syntax : [<visibility>]<method narme>(parameter list separated by comma):<return data type>
  • An example of a method in the previous example is setBirthdate(d:int, m:int, y:int) : date.
  • Here the parameter can be specified with data type and optional default value; for example gender: char = ‘M’.
  • Parameters can also be specified as input or output depending on whether it is read only or not.
  • UML diagrams are independent of the programming language used for coding an application.
  • Some software developers prefer to specify attributes and operations in the more familiar format of a programming language instead of using the standard UML notation.
  • Objects are presented using their state during execution of an application. Thus, objects are dynamic.
  • Corresponding to figure an object (also called an instance) of class person is represented as shown in figure.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 6

Messaging

  • In object-oriented terminology, a call to a method is referred to as a message. Due to encapsulation, all method calls are handled by objects that recognize the method.
  • Different classes may have same methods with same name. For example, class ‘date’ has method named ‘display’ that displays the date object in specific format.
  • Suppose there are other classes ‘time’ and ‘person’; both having method with same name ‘display’; to display time in specific format and to display the details of person respectively.
  • When method ‘display’ is invoked in the program, it is determined using the object that invokes the method. For example, if ‘display’ is called by an object of ‘person’ class, it executes the display method defined in ‘person’ class.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Aggregation vs. Composition

  • Aggregation represents non-exclusive relationship between two classes.
  • In aggregation, the class that forms part of the owner class can exist independently.
  • The life of an object of the part class is not determined by the owner class.
  • For example, although the motherboard is part of the computer, it can exist as a separate item independent of the computer.
  • The motherboard is not exclusively associated with computer.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 7

  • Basic aggregation is represented using an empty diamond symbol next to the whole class as shown in figure.
  • Composition represents exclusive relationship between two 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 gets 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 in figure .Composition relationships are represented using a filled diamond symbol next to the whole class as shown in figure.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts 8

  • In the above example, a relationship between class ’Person1 and class ’Name’ is composition relationship; whereas a relationship between class ’Person’ and class ’Address’ is aggregation relationship.
  • Address may be shared by more than one person. So, when a person is deleted, the corresponding name object is deleted but address cannot be deleted.
  • Note : The class that contains objects of other class is known as owner class or whole class or aggregating class. For example, the class ’Person’ shown in figure is an aggregating class.
  • The class that is contained in owner class is known as subject class or part class or aggregated class. For example, the classes ’Name’ and ’Address’ shown in figure are aggregated class.

Computer Class 12 GSEB Solutions Chapter 6 Object-Oriented Concepts

Composition vs. Inheritance

  • In inheritance, class inherits from other classes in order to share, reuse or extend functionality. Here, there exists ‘a kind of relationship between super class and sub class.
  • In composition, classes do not inherit from other classes, but are ‘composed of other classes. Class contains the attributes where some attributes are of objects of other class types.
  • Note : There are other types of relationships and also constraints that can be represented in class diagram.

Leave a Comment

Your email address will not be published. Required fields are marked *