0% found this document useful (0 votes)
24 views5 pages

Cat 2

Assignment 2

Uploaded by

gladyswendy571
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)
24 views5 pages

Cat 2

Assignment 2

Uploaded by

gladyswendy571
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

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

class C(A, B):


pass

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)

a) Here's an example in C++ demonstrating the principle of polymorphism:

#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) 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 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) Five main objectives of abstraction in OOP:

1. Simplify complex systems: Abstraction enables us to represent complex systems using


simpler interfaces.
2. Encourage modularity: By separating concerns, abstraction allows different parts of a
system to be developed and maintained independently.
3. Reduce code complexity: Abstraction helps manage the complexity of code by focusing
on essential features and hiding implementation details.
4. Enhance reusability: Abstract interfaces can be used across multiple implementations,
promoting code reuse.
5. Improve adaptability: Abstraction allows code to be more flexible and adaptable to
changes in requirements or implementation.

d) Here's an example in Java demonstrating the difference between a parameterized constructor


and a default constructor:

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

public class ConstructorExample {


public static void main(String[] args) {
Person person1 = new Person(); // Calls default constructor
System.out.println("Person 1: " + person1.name + ", " +
person1.age); // Output: Person 1: John Doe, 30

Person person2 = new Person("Alice", 25); // Calls parameterized


constructor
System.out.println("Person 2: " + person2.name + ", " +
person2.age); // Output: Person 2: Alice, 25
}
}
In this example, Person class has both a default constructor and a parameterized constructor.
The default constructor initializes the object with default values, while the parameterized
constructor takes arguments to initialize the object with specific values.

Connected to the web for real-time data

You might also like