Cat 2
Cat 2
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
c) State two advantages for the application of each of the following principles in
object oriented programming:
i. Encapsulation (2 marks)
ii. Inheritance (2 marks)
iii. Abstraction (2 marks)
iv. Polymorphisms (2 marks)
d) Using any OOP Language write code to implement multi-level inheritance and
multiple inheritance in a program. (2 marks)
QUESTION TWO [20 Marks]
a) Write instruction code in Object Oriented Programming language (Java or C++)
to declare and implement the principle of “Polymorphism” in a program. (5 marks)
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)
c) State the five main objectives of abstraction in OOP.(5 marks)
d) Using coding examples, explain the difference between a Parametrized
constructor and a default constructor in OOP. (5 marks)
Multi-level inheritance:
class Grandparent:
def greet(self):
print("Hello from Grandparent")
class Parent(Grandparent):
pass
class Child(Parent):
pass
c = Child()
c.greet() # Output: Hello from Grandparent
Multiple inheritance:
class A:
def greet(self):
print("Hello from A")
class B:
def greet(self):
print("Hello from B")
c = C()
c.greet() # Output: Hello from A
Please let me know if you need further clarification or examples for the remaining questions.
G
a) Write instruction code in Object Oriented Programming language (Java or C++) to declare and
implement the principle of “Polymorphism” in a program. (5 marks)
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)
c) State the five main objectives of abstraction in OOP.(5 marks)
d) Using coding examples, explain the difference between a Parametrized constructor and a
default constructor in OOP. (5 marks)
#include <iostream>
class Shape {
public:
virtual void draw() = 0; // Pure virtual function (abstract method)
};
int main() {
Shape *shapes[] = {new Circle(), new Rectangle()};
return 0;
}
b) Here's an example in Java illustrating single-level and multi-level inheritance:
// Single-level inheritance
class Parent {
void greet() {
System.out.println("Hello from Parent");
}
}
class Person {
String name;
int age;
// Default constructor
Person() {
name = "John Doe";
age = 30;
}
// Parameterized constructor
Person(String name, int age) {
this.name = name;
this.age = age;
}
}