0% found this document useful (0 votes)
105 views34 pages

COS 202 Advanced Object Oriented Programming PDF

The document covers advanced concepts in Object-Oriented Programming (OOP) including polymorphism, abstract classes, and interfaces. It explains key principles such as inheritance, encapsulation, and the importance of code reusability and flexibility. Additionally, it highlights the differences between abstract classes and interfaces, providing examples and further reading resources.

Uploaded by

Nsikan Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views34 pages

COS 202 Advanced Object Oriented Programming PDF

The document covers advanced concepts in Object-Oriented Programming (OOP) including polymorphism, abstract classes, and interfaces. It explains key principles such as inheritance, encapsulation, and the importance of code reusability and flexibility. Additionally, it highlights the differences between abstract classes and interfaces, providing examples and further reading resources.

Uploaded by

Nsikan Gabriel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Computer programming ii

Advanced Object-Oriented
Programming
Msc. Computer Science
Samuel Ubaru
Computer programming ii

Learning objectives

At the end of this lesson, you should be able to:

• Understand the concept of polymorphism


and how to implement it.
• Comprehend the role and implementation of
abstract classes.
• Learn how to create and use interfaces.

• Apply advanced OOP principles to solve complex


problems.
Computer programming ii

Introduction

In programming, the concept of Object-Oriented


Programming (OOP) is a programming paradigm that uses
"objects" to design software. These objects can be thought
of as real-world entities that have both attributes
(properties) and behaviors (methods).
Computer programming ii

Key Concepts in Basic OOP

These concepts include:


Classes and Objects
Inheritance
Encapsulation and
Polymorphism
Computer programming ii

CLASSES and objects

A Class: A blueprint for creating objects. It


defines a type of object according to the
methods and properties that can be attributed
to it.
Object: An instance of a class. It contains real
values instead of variables.
Computer programming ii

inheritance

In C++, inheritance is a process in which one object


acquires all the properties and behaviors of its parent
object automatically.
In C++, the class which inherits the members of
another class is called derived class and the class
whose members are inherited is called base class.
Computer programming ii

Implementation of inheritance
Computer programming ii

encapsulation
The meaning of Encapsulation, is to make sure that
"sensitive" data is hidden from users. To achieve this,
you must declare class variables/attributes as private
(cannot be accessed from outside the class).
Computer programming ii

Concept of polymorphism
Computer programming ii

polymophism

Polymorphism is a key concept in Object-Oriented


Programming means "many forms",that allows objects of
different classes to be treated as objects of a common
superclass. It is the ability of different objects to respond, each
in its own way, to identical messages.
Computer programming ii

Types of Polymorphism

1. Compile-Time Polymorphism (Method Overloading): Method


overloading allows a class to have more than one method with
the same name, as long as their parameter lists are different.
2. Run-Time Polymorphism (Method Overriding): Method
overriding allows a subclass to provide a specific
implementation of a method that is already defined in its
superclass.
Computer programming ii

Run-Time Polymorphism (Method Overriding):


Computer programming ii

Compile-Time Polymorphism (Method Overloading


Computer programming ii

Why is Polymorphism Important?

• Code Reusability: Write more generic and


reusable code.
• Flexibility and Maintainability: Easier to manage
and extend systems.
• Dynamic Binding: Objects are bound to methods
at runtime, enhancing flexibility.
Computer programming ii

Polymorphism Example
Computer programming ii

ABSTRACT CLASSES
Computer programming ii

What are Abstract Classes?

Abstract classes in C++ are classes that cannot be instantiated


directly. This means you cannot create objects of an abstract class.
They are designed to be inherited by other classes to provide a
common interface and some shared implementation.

Pure virtual functions:


An abstract class must contain at least one pure virtual function. A
pure virtual function is a function declared in a base class that has
no implementation in that class.
Syntax for a virtual function:

The ‘ = 0’ syntax specifies that the function is pure virtual, making


the class abstract
Computer programming ii

Abstract classes: purpose and concrete methods

Concrete Methods: Abstract classes can also contain


concrete (non-abstract) methods, which have an
implementation in the abstract class.
These methods can be shared among all derived classes.

Purpose: Abstract classes are used to represent generic


concepts or base classes in an application.
They allow for a common definition of an interface for all
subclasses, promoting code reusability and consistency.
Computer programming ii

Abstract class: example

The ‘Animal’ class is an abstract


class because it contains a pure
virtual function ‘makeSound()’.
The ‘Dog’ class inherits from ‘Animal’
and provides an implementation for
the ‘makeSound()’ function.
The ‘Animal’ class also has a
concrete method ‘sleep()’, which can
be used by all derived classes.
Computer programming ii

When to use abstract classes

Use abstract classes when you want to provide a common


base class with some shared code.
Ideal when creating a class hierarchy where derived
classes have common behavior but also define specific
behavior.
Scenario: An abstract class for a vehicle with concrete
methods for fuel capacity and abstract methods for
vehicle type-specific behaviors.
Computer programming ii

interfaces
Computer programming ii

What are Interfaces in C++?


In C++, interfaces are implemented using pure abstract
classes.
An interface is a class that contains only pure virtual
functions and no member variables.
Interfaces define a contract that implementing classes
must follow, ensuring a consistent API.
Computer programming ii

Pure virtual functions

All functions in an interface are pure virtual, meaning


they are declared but not implemented in the interface.

Implementing Multiple Interfaces: C++ allows a


class to implement multiple interfaces by inheriting from
multiple pure abstract classes.
This is beneficial for creating flexible and modular designs.
Computer programming ii

interface: example
The ‘Animal’ class is an interface with a pure
virtual function ‘xmakeSound()’.
The ‘Movable’ class is another interface with
a pure virtual function ‘move()’.
The ‘Dog’ class implements both Animal and
Movable interfaces by providing
implementations for ‘makeSound()’ and
‘move()’.
In ‘main()’, we create an object of Dog and
call both ‘makeSound()’ and ‘move()’,
demonstrating that Dog follows the
contracts defined by both interfaces.
Computer programming ii

Differences Between Abstract Classes and Interfaces


Computer programming ii

MID-LESSON QUESTIONS

1. Which of the following is true about interfaces (pure abstract classes) in C++?

A Interfaces can have B Interfaces can provide


member variables. method implementations.

C A class can implement D Interfaces cannot be


multiple interfaces. inherited.
Computer programming ii

MID-LESSON QUESTIONS

1. In Java, which statement is true about access modifiers (public, private, protected)
within a package?

A Interfaces can have B Interfaces can provide


member variables. method implementations.

C A class can implement D Interfaces cannot be


multiple interfaces. inherited.
Computer programming ii

MID-LESSON QUESTIONS

1. Which of the following is true about interfaces (pure abstract classes) in C++?

A Interfaces can have B Interfaces can provide


member variables. method implementations.

C A class can implement D Interfaces cannot be


multiple interfaces. inherited.
Computer programming ii

MID-LESSON QUESTIONS

1. Which of the following statements about abstract classes is false?

A Abstract classes can have B Abstract classes can have


constructors. pure virtual functions..

C Abstract classes can be D Abstract classes can have


instantiated. concrete methods.
Computer programming ii

Summary
Computer programming ii

Summary
Polymorphism:
• Enables objects to be treated as instances of their parent class rather than their actual
class.
• Promotes flexibility and reusability by allowing one interface to be used for a general
class of actions.
Abstract Classes:
• Cannot be instantiated directly.
• Can contain both pure virtual functions (abstract methods) and concrete methods.
• Serve as a common base class for other classes, providing a shared interface and some
common implementation.
Interfaces:
• Pure abstract classes with only pure virtual functions and no member variables.
• Define a contract that implementing classes must follow, ensuring a consistent API.
• Support multiple inheritance, allowing a class to implement multiple interfaces.
Computer programming ii

FURTHER READING RESOURCES

● Bruce, E. (2002). Thinking in C++, volume 1.


● Weiss, M. A. (2006). Data structures and algorithm analysis
in C++.
● Lafore, R. (2018). Object-oriented programming in C++.
● Bloch, J. (2018). Effective Java (3rd ed.).
Computer programming ii

FURTHER READING RESOURCES

● "Clean Architecture: A Craftsman's Guide to Software


Structure and Design.
● "Working Effectively with Legacy Code" by Michael
Feathers (Prentice Hall, 2004).
● "Design Patterns: Elements of Reusable Object-
Oriented Software" by Erich Gamma, Richard Helm,
Ralph Johnson, and John Vlissides (Addison-Wesley
Professional, 1994).
● "The Pragmatic Programmer: From Journeyman to
Master" by Andrew Hunt and David Thomas (Addison-
Wesley Professional, 1999).
Computer programming ii

Thank
You

You might also like