0% found this document useful (0 votes)
6 views

OBJECT ORINTED PROGRAMMING 2

Uploaded by

raphael muthini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OBJECT ORINTED PROGRAMMING 2

Uploaded by

raphael muthini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

OBJECT ORIENTED PROGRAMMING II

NAME: KING’OLA RAPHAEL

REG_NO: BIT/2021/94797
CAT 1

i) Define the following concepts used in OOP


a) Class
[2 marks]
In object-oriented programming (OOP), a class is a blueprint or a template that defines
the structure and behavior of objects. It acts as a blueprint for creating instances of
objects. It encapsulates data (attributes) and methods (functions) that define the
characteristics and actions of the objects. A class defines the common properties and
behaviors that objects of the same type will have.

b) Object [2 marks]
An object is an instance of a class. It represents a tangible entity created based on the
defined structure and behavior of its class. Objects have their own unique set of data
and state. They can interact with each other by invoking methods and accessing
attributes. Objects are the fundamental building blocks of an object-oriented program.

c) Constructor [2 marks]
A constructor is a special method within a class that is used for initializing objects. It is
called when an object is created from the class blueprint. The constructor typically sets
initial values to the object's attributes or performs any necessary setup operations. In
other words, it helps in the creation and initialization of objects.

d) Destructor [2 marks]

A destructor is a special method within a class that is called when an object is about to
be destroyed or deallocated from memory. It is responsible for cleaning up resources
used by the object before it is removed from memory. Destructors are optional in many
programming languages as the memory management is often handled automatically,
but they can be useful for performing specific cleanup tasks or releasing external
resources.

ii) Differentiate between the following terms as used in OOP


a) Primitive Built-in Types and Abstract Data Types(ADT)
[2 marks]

a) The Difference between primitive built-in types and ADTs is that primitive types are provided
by the language and have a fixed set of operations, whereas ADTs are defined by the programmer and
encapsulate both data and the operations that can be performed on that data.
b) Interface and Implementation
[2 marks]
Interfaces define a contract of methods that a class must implement, while implementation
refers to the actual coding and realization of the functionality within a class or an interface.

III. The four pillars of object-oriented programming (OOP) languages are

a. Encapsulation: Encapsulation is the principle of bundling data (attributes) and


the methods (functions) that operate on that data into a single unit called an
object. It promotes the idea of data hiding, where the internal details and
implementation of an object are hidden from the outside world.

a. b. Inheritance: Inheritance allows the creation of new classes (derived or child


classes) based on existing classes (base or parent classes). The derived classes
inherit the properties and behavior of the parent class, allowing code reuse and
the establishment of hierarchical relationships between classes.
b. c. Polymorphism: Polymorphism means “many forms” and refers to the ability of
objects to take on different forms or respond differently to the same message or
method call.
c. d. Abstraction: Abstraction is the process of simplifying complex systems by
modeling real-world entities as abstract classes or interfaces. It focuses on
capturing the essential features and behavior of an object while hiding the
unnecessary details.
d. a. Information Hiding: Information hiding, also known as data hiding, is the
practice of concealing the internal implementation details of an object and
exposing only the necessary information or functionality.
e. b. Data Abstraction: Data abstraction is the process of representing complex
real-world entities in a simplified manner, focusing on the essential properties
and behavior of the object while hiding the unnecessary details. It involves
creating abstract data types (ADTs) or classes that define the data and
operations performed on that data, without exposing the implementation
details.
f. c. Encapsulation: Encapsulation is the bundling of data (attributes) and the
methods (functions or behavior) that operate on that data into a single unit
called an object. It provides a mechanism to encapsulate related data and
operations within a class, ensuring that they are treated as a cohesive entity.
g. d. Access Modifiers: Access modifiers are keywords used in OOP languages to
control the visibility and accessibility of class members (attributes and methods)
from different parts of the program.
I IV. Explain the concepts of Information Hiding, Data Abstraction, Encapsulation and Access
modifiers in OOP

II V. Using a code snippet , show how Object Oriented Programming languages such as C++ or
Java implement the concept in (iv) above
Java
Copy code
Public class Car {
Private String brand;
Private String model;
Private int year;
// Constructor
Public Car(String brand, String model, int year) {
This.brand = brand;
This.model = model;
This.year = year;
}
// Getters and Setters
Public String getBrand() {
Return brand;
}
Public void setBrand(String brand) {
This.brand = brand;
}
Public String getModel() {
Return model;
}
Public void setModel(String model) {
This.model = model;
}
Public int getYear() {
Return year;
}
Public void setYear(int year) {
This.year = year;
}
// Method
Public void startEngine() {
System.out.println(“Engine started.");
}
}
In this code snippet, we have a Car class that demonstrates the aforementioned concepts:
Information Hiding: The class Car encapsulates the data (brand, model, year) and hides them from direct
access by declaring them as private. They can only be accessed or modified using the public getter and
setter methods.
Data Abstraction: The Car class abstracts the concept of a car by encapsulating its properties (brand,
model, year) and behavior (startEngine method) within the class. Users of the Car class interact with it
through its public methods, without needing to know the implementation details.
Encapsulation: The Car class encapsulates its data and behavior within a single unit. The private
attributes ensure that the internal state of the Car object can only be accessed or
modified through the public methods, maintaining data integrity and preventing unintended
modifications.
Access Modifiers: The private access modifier restricts direct access to the attributes (brand, model,
year) from outside the class. The public access modifier is used for the getter and setter methods,
allowing controlled access to the encapsulated data.
I VI. Abstraction simplifies complexity, manages complexity, enhances modularity, and provides
flexibility and extensibility to your codebase. It allows you to work with higher-level concepts, hide
implementation details, and create modular, reusable, and maintainable software systems.
VII. a. Method Overloading: Method overloading occurs when multiple methods within a class
have the same name but different parameter lists. The compiler differentiates between these methods
based on the number, type, and order of the parameters. Method overloading enables you to define
multiple variations of a method that perform similar operations but on different data or with different
argument types.
b. Operator Overloading: Operator overloading allows you to redefine the behavior of existing
operators (+, -, *, /, etc.) for user-defined classes or data types. It enables you to define how operators
should work with objects of a particular class, extending their functionality beyond the built-in
operations.

You might also like