Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

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 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

Public class Grandfather

Public void eat( )


{

System.out.println("eating...");

Public class Father extends Grandfather

Public void walk( ){System.out.println("walking...");

Public class Child extends Father

Public void speak(){System.out.println("speaking...");

Public class TestInheritance2

public static void main(String args[ ])

Child=new Child( );

f.eat();

f.walk();

f.speak();

}
}

Multiple inheritance:
class A:

def greet(self):

print("Hello from A")

class B:

def greet(self):

print("Hello from B")

class C(A, B):

pass

c = C()
c.greet() # Output: Hello from A

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)

#include <iostream>

class Shape {

public:

virtual void draw() = 0; // Pure virtual function (abstract method)

};

class Circle : public Shape {


public:

void draw() override {

std::cout << "Drawing a circle" << std::endl;

};

class Rectangle : public Shape {

public:

void draw() override {

std::cout << "Drawing a rectangle" << std::endl;

};

int main() {

Shape *shapes[] = {new Circle(), new Rectangle()};

for (Shape *shape : shapes) {

shape->draw(); // Polymorphism in action

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() {

System.out.println("Hello from Parent");

class Child extends Parent {

// Multi-level inheritance

class Grandparent {

void greet() {

System.out.println("Hello from Grandparent");

class Parent2 extends Grandparent {

class Child2 extends Parent2 {

public class InheritanceExample {

public static void main(String[] args) {

Child child = new Child();

child.greet(); // Output: Hello from Parent


Child2 child2 = new Child2();

child2.greet(); // Output: Hello from Grandparent

}
}

c) State the five main objectives of abstraction in OOP.(5 marks)

 To hide the underlined complexity of data in a program


 To avoid repetition of the code
 To present the overview of the internal functionality
 To give flexibility to programmers so that they can change the implementation of the
abstract class behaviour when required.
d) Using coding examples, explain the difference between a Parametrized
constructor and a default constructor in OOP. (5 marks)
Parameterized constructor has specific number of parameters and it is used for the purpose of
providing different values to distinct objects in a program while a default constructor does not
have parameters and is used for the purpose of providing the default values to the object
depending on the type pf object.

You might also like