Credits from:
Before objects, we had primitive data types that stores single, simple values (eg. Byte
, Int
, Boolean
, Float
, Double
, Char
). But, we need more complex data types to store values… For example, in a chess game, for each piece - eg. knight - we need to store the position, ifCaptured, color, movement etc.. It would be easier if we group all those values together as one entity. Additionally, we could group some knights together as they behave similarly.
In C or C++ programming language, we have:
Structures | Arrays |
---|---|
- Stores many pieces of data | - Stores many pieces of data |
- CAN store different types of data (eg. int and strings together) |
- CANNOT store different types of data |
- Can store other structures within it | |
- CANNOT store functions within one !!! (unlike objects) |
The structures are the precursor of the objects.
Definitions:
In Object Oriented Programming, we combine groups of related variables and functions into objects. We refer to variables as properties and to functions as methods within an object.
Encapsulation refers to bundling data with methods that can operate on that data within a class.
it is in the idea of hiding data within a class, preventing anything outside that class from directly interacting with that data.
members of other classes can interact with the attributes of another object through its methods (therefore, classes should not directly interact with other classes’ data)
the methods are the functions defined within the class
encapsulation is about getting (retrieving information) and setting (changing information) methods
eg. piece.getColor()
(checks the color of any given piece from anywhere in the program)
eg.
# Setters are helpful when changing multiple attributes values of an object based on conditions
class Player:
...
def setCurrentHealth(newHealth):
if newHealth > player.maxHealth:
player.currentHealth = player.maxHealth
else:
player.currentHealth = newHealth
Abstraction refers to only showing the essential details and hiding everything else. As long as we understand the outcome, the process is not very important.
Users of the classes created should not worry about the inner details of those classes.
It’s best to think of a program (big project) in terms of an Interface and Implementation
Inheritance is the principle that allows classes to derive from other classes.
With inheritance, we will have a class hierarchy that acts as a web of classes with different relationships to one another.
Access modifiers change which classes have access to other classes, methods or attributes. There are 3 types of access modifiers:
(members = properties/variables of a class and/or methods/functions of a class)
Inheritance will also help in eliminating redundant code (we will not have to redefine properties and methods for each different object… these objects could be simply be inherited from a generic object).
Polymorphism describes the methods that are able to take on many forms.
There are two types of polymorphism:
(subclasses could also be called: extended class or child class; superclasses could also be called parent class or base class)
Static polymorphism - occurs during compile-time (when the program is being compiled, rather than during runtime/execution). Static polymorphism refers to when multiple methods with the same name but with different arguments are defined in the same class.
.drive(int speed)
).drive(int speed, string dest)
).drive(string dest, int speed)
)Overall, polymorphism allow methods to take on many different forms, therefore allowing methods to exist in the same class, and also within different classes.