OBJECT ORINTED PROGRAMMING 2
OBJECT ORINTED PROGRAMMING 2
REG_NO: BIT/2021/94797
CAT 1
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.
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.
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.