Object-oriented programming (OOP)
A programming paradigm where the system is viewed as a set of objects, each with their own data (attributes) and procedures (methods), that can interact with each other. All processing is done by objects.
Programming paradigm
An overall approach to writing program code
Object
Partiular instance of a class
Attributes
Value held by an object
Instantiation
The process of making an instance of an object from the class (definition). The object is assigned to a reference variable. Multiple instances of objects can be created. Each must have its own reference variable.
Class (definition)
A template / blueprint for an object and defines the state (the properties of the objects, given by attributes) and behaviour (actions it can perform, given by methods) of an object
UML
abbreviation of Unified Modeling Language
Class diagrams
Used to plan what attributes and methods a class will have. Each class is a box with three sections:
Name of the class at the top
Attributes and their data types in the middle section
The methods including parameters and returned data types at the bottom
Methods
Actions an object can perform
Constructor
Special method that creates an instance of an object from a class
It carries out any actions that are needed for this, such as setting the initial values of the attributes
____ have specific names (depends on the language)
Reference variable
Holds the address of the location in memory at which the specific object can be found. Whenever an instance of an object is created, it must be assigned to a _______
Access modifiers
Specify which properties (attributes and methods) can be accessed from outside the class. One of:
Public
Private
Protected
Public (attributes or methods)
(Attributes or methods) that can be accessed/called from another part of the program (outside the class). Most methods are _____, but almost all attributes are not.
Private (attributes or methods)
(Attributes or methods) that can not be accessed/called from another part of the program (outside the class). Attributes are almost always ____, whereas most methods tend not to be.
How objects interect with each other
They use 'message passing'; (this just means that) they call each other's public methods.
Encapsulation
When attributes are declared as private so can only be accessed and modified by public methods. This hides the data (attributes), making programs less complex by protecting data from being accidentally edited by other parts of the program.
Accessors
Special methods that exist to get the value of an attribute. (aka getters)
Mutators
Special methods that exist to set or change the value of an attribute.(aka setters)
Designing classes to be stand-alone allows…
…allows class designers to change the implementation details without any adverse effect on the applications that make use of them.
Benefits of making classes self-contained
The class can be (re)used in other applications
You can change the internal workings of any class, as long as the public interface isn't changed
Inheritance
When a child class / subclass / derived class takes on attributes and methods of a parent class / superclass / base class
Overriding inherited methods
Inherited methods can be _____ to change their functionality and make the behaviour more appropriate to the subclass. Is a form of polymorphism.
Protected property
A property that can be directly accessed by a subclass
Extending a subclass
Giving a subclass additional attributes and methods
Polymorphism
Fundamental OOP concept. It is the ability of an object to specialise its behavior based on its type; when a method is called, the code that is executed will depend on the object type. Two examples (you need to know) of how this is achieved is:
Method overriding
Method overloading
Method overloading
Having multiple methods of the same name, but that differ in terms of their parameter list, in a class. When one of these methods is called, the one with the matching parameter list is executed. Is a form of polymorphism.
Method signature
Combination of a method identifier and its parameter list
Advantages of OOP
Usually easier for us to think in terms of objects (and their attributes & behavious) than to think procedurally
Prewritten classes promote and support code reuse
Classes are highly modular, and this means that they are easy to maintain
Encapsulation provides a high level of protection for data - ensures that the data can never be modified carelessly and that any associated actions also happen (e.g. when a goal is scored in a game, you can make sure the player's score is incremented)
Disadvantages of OOP
OOP design techniques can result in a much larger, more complex system being built in order to achieve a result
Potential performance degredation (higher latency and increased resource consumption)
OOP relies on message passing for communication between objects
Systems with high volumes of message passing can suffer from the overhead involved in message creation, transmission, and processing
This can intruduce latency and increase resource consumption
Inheritance can have unintended consequences - objects can acquire the ability to do things that don't make sense (e.g. flying pigs)
Objects can take up a relatively large amount of memory as they typically require memory allocation for metadata (like pointers to methods and class information) as well as their own attributes.
Inheritence diagram
Inheritance is shown with open-headed arrows that point up from the child class to the parent class
Purpose of a constructor
To create an instance of an object from a class