Professional Documents
Culture Documents
CAT2 OOP
CAT2 OOP
a) Explain how the following concepts are applied in object oriented programming
paradigm.
i. Constructor (1mark)
It is a special method used to initialize an object in a program, when an object or a class is
created to set initial values for the object attributes.
ii. Specialization (1mark)
It refers to the conversion of a super-class into a sub-class that is going down from
general to a more specific from of a class.
iii. Method binding (1mark)
It refers to an association of method call to a method definition or connecting a method call
to a method body.
iv. Super class (1mark)
A superclass is the class from which a subclass is derived. It is also referred to as the base
class or parent class.
v. Message passing (1mark)
It is a technique used in OOP to communicate between objects by invoking methods or
accessing properties. It allows objects to interact and exchange information.
vi. Destructor (1mark)
It is a special method that automatically gets called when an object is no longer in use in
order to delete it.
vii. Method overriding (1 mark)
This occurs when a sub-class implements a method of the same class. It occurs when
declaring method in a sub-class which already exists in a super-class.
viii. Interface (1 mark)
They are used to describe the behaviours that a class or classes must implement. It allows
different classes to be used interchangeably, as long as they implement the same interface.
b) Using diagrams, explain the difference between multi-level inheritance and
multiple inheritance. (2 marks)
Multi-level Multiple
Grandparent Parent Parent
Parent
Child
Child
c) State two advantages for the application of each of the following principles in
object oriented programming:
i. Encapsulation (2 marks)
It enhances data hiding in a program.
It provides control over data in a program using either the setter method or
getter method.
ii. Inheritance (2 marks)
Code reusability where new classes reuse code from existing classes, reducing
duplication.
Flexibility of the code , where the code can be modified or undergo changes.
iii. Abstraction (2 marks)
Code reusability.
Code flexibility
User friendly.
iv. Polymorphisms (2 marks)
Flexibility: Polymorphism enables a single interface to be used with different types,
making code more adaptable.
Extensibility: New types can be added without modifying existing code that uses
polymorphic interfaces.
d) Using any OOP Language write code to implement multi-level inheritance and
multiple inheritance in a program. (2 marks)
. Multilevel Inheritance
System.out.println("eating...");
Child=new Child( );
f.eat();
f.walk();
f.speak();
}
}
Multiple inheritance:
class A:
def greet(self):
class B:
def greet(self):
pass
c = C()
c.greet() # Output: Hello from A
#include <iostream>
class Shape {
public:
};
};
public:
};
int main() {
return 0;
}
b) In inheritance principle of OOP, “there exist single level inheritance and
multilevel inheritance. With support of program code, explain these two
concepts.(5 marks)
// Single-level inheritance
class Parent {
void greet() {
// Multi-level inheritance
class Grandparent {
void greet() {
}
}