0% found this document useful (0 votes)
5 views

1.4. Object-oriented programming

The document provides an overview of object-oriented programming (OOP) and its principles, including encapsulation, inheritance, polymorphism, and abstraction. It contrasts OOP with procedural programming, highlighting the advantages of OOP such as modularization and reusability. Additionally, it explains the concepts of classes and objects, illustrating how they are used in programming languages like Java and Python.

Uploaded by

Alistair Frame
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

1.4. Object-oriented programming

The document provides an overview of object-oriented programming (OOP) and its principles, including encapsulation, inheritance, polymorphism, and abstraction. It contrasts OOP with procedural programming, highlighting the advantages of OOP such as modularization and reusability. Additionally, it explains the concepts of classes and objects, illustrating how they are used in programming languages like Java and Python.

Uploaded by

Alistair Frame
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

FOUNDATIONS

OF
COMPUTATIONAL
THINKING AND PROGRAMMING
Object-oriented programming
Revision
• How do you perform multi-lines comments in
Java?
• What is semantic error?
• What keyword do you use in Python to define a
function?
Class, Object and Scope
Learning Objectives
This session will cover:
1. An overview of programming paradigms;
2. Procedural vs Object Oriented Programming;
3. Class;
4. Object.
Overview

Procedural

Declarative
Programming Functional
Paradigms

Object
Oriented
Overview
Different programming languages allow and encourage the use of
different programming models.

In Procedural Programming model a list of instructions are


written that tells the computer to follow a set of steps. Procedural
languages include C, MATLAB and Python.

In Object Oriented Programming (OOP) instructions are


written to manipulate a collection of objects. These have states,
i.e. holds data, and behaviours which are functions that use data.
Java and C++ are examples of strongly object-oriented
programming languages. Python allows but does not require
objects, however, when used it can be a powerful and easy tool for
handling classes and objects.
Object Oriented vs
Procedural
Procedural Object-Orientated (OO)
• Data held in separate • Objects are responsible for
primitive variables such as their own data and
integer or data structure such operations on the data
as array • Program code in an OOP
• Data made be locally or creates the objects and
globally available allow them to
• Changes in global variable communicate with each
Naturally,
may affect other parts of the
Top-down other
the world is
programapproach
made up of
Calls to procedures objects that
and functions, each interact
of which may call with each
other procedures or other
functions
Advantages of Object-Oriented
Programming over Procedural
Programming

Clear program Avoids


Faster
structure repetitions

Easier to Reusable Promotes


execute applications modularization
Object-oriented
programming
Object-Oriented Programming is a programming style based on the concept
of objects, which combine data (attributes) and behavior (methods).
It focuses on organizing code into reusable, modular components using
classes and objects.

Key principles include:

•Encapsulation: Keeping data safe inside objects


•Inheritance: Reusing code by creating subclasses
•Polymorphism: Using the same interface for different types
•Abstraction: Hiding complex details and exposing only what's needed
Classes and Objects

Classes and objects make up the two fundamental


concepts of object-oriented programming.

A class is a blueprint or template that describes how to


create a specific kind of object.

Applications are designed at class level.

An object is a unique programming entity that can have


methods and attributes and can react to events.

An object is an instance of a class.


Classes and Objects

Object:
Audi
Object:
Object:
Aston
Bugatti
Martin

Class:
Object: Car Object:
Ferrari BMW

Object:
Object:
Range
Jeep
Rover
Classes and Objects

Object:
Desk

Object: Object:
Sofa Chair

Class:
Furniture
Object: Object:
Bed Stool

Object: Object:
Bench Cabinet
OOP - Inheritance

Allows programmers to create new classes based on an


existing class.

Methods and attributes (variables) from the parent class are


inherited by the newly-created class.

New methods and attributes can be created in the new class,


but don’t affect the parent class’s definition.
OOP - Encapsulation

Each objects methods manage its own attributes.

This is also known as hiding.

An object A can learn about the values of attributes of


another object B, only by invoking the corresponding
method (message) associated to the object B.
OOP - Polymorphism

Creating methods which describe the way to do some general


function.

Polymorphic methods can adapt to specific types of objects.

The same method will behave differently when it is applied to


the objects of different classes.

Example: an object can send a message PRINT to several


objects, and each one will use its own PRINT method to execute
the message.
Creating Classes and Objects in Java
The class
public class ClassName{ keyword creates
public static void main(String[] args){ the class which in
Student student1 = new Student(); this case we have
Student student2 = new Student(); named it
Student. We
student1.firstName = “John”; then use the
student2.mark = 75; Student class
} inside the main (
} ) method to
class Student{ create different
String firstName; instances
String surname; (objects) of it
int mark; which are called
} student1 and
student2.
Creating Classes and Objects in Java
Just like in Java,
the class
class my_class:
keyword creates
var1 = "Class variable for use with my first
the class which in
object"
this case we have
var2 = 3.14
named it
my_class. We
obj1 = my_class()
then use the
obj2 = my_class()
class to create
different
print(obj1.var1)
instances
print(obj2.var2)
(objects) of it
which are called
obj1 and obj2.
Summar
y
• Different programming languages allow and
encourage the use of different programming
models.

• Classes and objects make up the two fundamental


concepts of object-oriented programming.

• A class is a blueprint or template that describes how


to create a specific kind of object.
Further
reading
Goodrich, M. T. et al. (2022) Data Structures and Algorithms in
Java. Wiley

Liang, D. Y. (2021) Introduction to Java Programming and Data


Structures, Comprehensive Version. 12th edn. Pearson

Hunt, A. et al. (2000) The Pragmatic Programmer: From


Journeyman to Master. 1st edn. Addison-Wesley

You might also like