2.2.1 Object Orientated Programming

0.0(0)
studied byStudied by 5 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

Class

A class forms a blueprint that we can use to create an object, build objects from/ base objects on.

2
New cards

Object

Objects are an instance of a class, they are created from classes: it is a representation of a real-world entity. 

3
New cards

Attribute

An attribute is a data element that represents the quality or state of the class or object: they are defined within a class and can be accessed and modified by both the class and its objects. 

The individual properties of an object.

4
New cards

Method

Methods are functions associated with objects or classes that define the behaviour and actions that objects can perform. There are two main types of methods: a function (performs a task and returns a value); a procedure (performs a task buts does not return a value). 

The associated actions of an object.

5
New cards

Instance

An instance is an individual object; a specific realization of any object created from the same class.

6
New cards

Inheritance

In inheritance, the subclass inherits all of the attributes and methods of the parent class. 

The subclass can have its own attributes and methods. 

7
New cards

Encapsulation

Encapsulation refers to the restriction of access to certain methods and attributes of a class e.g private and public variables

Could also refer to bundling of data, along with the methods that operate on that data, into a single unit; the process of combining methods and procedures to form an object in object-oriented programming.  

Attributes of an object are encapsulated within a class. Objects are self-contained.

8
New cards

Polymorphism

Polymorphism is when a subclass alters its inherited methods in two ways: by overloading or overriding;  a feature of object-oriented programming languages that allows a specific routine to use variables of different types at different times. 

Overloading is when a method is defined in the subclass that has the same name, but different arguments to a method defined in the superclass. 

Overriding (Virtual Methods) is when a method is redefined in the subclass with the same name and same arguments: changing the code/ implementation.

Objects can be created from the same class, but can differ individually. 

9
New cards

Constructor

A special method used to create a new object from a class. Its purpose is to initialise the attributes of an object.

10
New cards

Benefits of Inheritance

●Avoid repeating code

●Organise classes and objects into a hierarchy

●Share methods and attributes between different classes

11
New cards

Access Modifiers

Private: only the methods within the class can access these attributes. Used for encapsulating programs (a method will not have access to the data held in a private attribute if not needed).

Public: can be accessed from anywhere within the program. If the attribute does not contain any information that needs to be hidden from other classes or needs to limit the ability of other classes to alter the attribute, you might make an attribute public instead of using accessors and mutators.

12
New cards

Accessors/ Getters

When an attribute from another class is needed, instead of making that attribute public, you can create a public method that returns the value of the attribute.

Methods that return the value of a private attribute are known as accessors (or 'getters').

13
New cards

Mutators/ Setters

To change the value of an attribute, you can create a public method to change its value rather than directly altering it.

Methods that alter the value of a private attribute are known as mutators (or 'setters').

14
New cards

Abstract Method

A method in the parent class without body (no implementation) is known as abstract method. It is a method that cannot be called but can be overridden.

15
New cards

Abstract Class

●A class that contains one or more abstract methods is classified as an abstract class.

●Some of the methods can have implementation.

●A subclass always ‘extends’ an abstract class.

●A subclass can inherit from only one abstract class.

16
New cards

Interface

●A class where all of its methods are abstract methods is classified as an interface.

●A subclass always ‘implements’ an interface.

●A subclass can inherit from more than one interface (multiple-inheritance).

17
New cards

How to create abstract class and method in Python?

Import ABC module, available in Python’s standard library.

from abc import ABC, abstractmethod

Tthe class must have at least one abstract method: you need to decorate it with @abstractmethod decorator.

@abstractmethod

def method1(self):

return

18
New cards

Static Method

A static method in a class, is not bound to an object. It can be directly called using the class name.

Normally used when you write utility classes where overridden is not allowed.

19
New cards

UML Class Diagram Symbols

•Public (+)

•Private (-)

•Protected (#)

Static (underlined)

Abstract (italics)

20
New cards

UML Class Association Types

Association (general): a line connecting two classes

Inheritance: arrow pointing towards parent class

Composition is where one composite object is formed from a collection of different component objects, where each of these component objects can only exist as part of a composite object, depicted by a line where the end of the line with the filled-in diamond is the composite class, and the other end of the line is the component class.

Aggregation is very similar to composition, but the component objects can exist separately from the aggregated object. The end of each line with the hollow diamond is the aggregation class, and the other ends of the lines are the component classes.