1/32
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
Each object that is instantiated from the same class will share the same attributes and methods.
Attributes
Values held by an object
Instantiation
The process of making an instance of an object from the class.
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 for an object that defines the attributes and methods an object should have. It is used to make objects.
UML is the abreviation of …
… abreviation 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 they can only be accessed and modified by public methods.
This protects the attributes from being accessed or accidentally altered by other objects
Advantages of encapsulation
Protects data - data can’t be changed accidentally
Reduces the chance of errors/inconsistences
Ensures objects can only be changed in the way intended/ ensures changes are consistent with how the object should behave
Getters
Special methods that exist to get the value of an attribute. (aka accessors)
Setters
Special methods that exist to set or change the value of an attribute.(aka mutators)
Difference between get and set methods
A get method allows the attribute to be accessed / returned
A set method allows the attribute to be changed (with parameters)
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
Allows you to change the internal workings of classes, as long as the public interface isn't changed
Inheritance
When a child class / sub class takes on the attributes and methods of a parent class / superclass.
The child class / sub class can also have its own extra attributes/methods.
Overriding inherited methods
When a method name is the same in a parent and sub class, then the method in the parent/super class will be overridden
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
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 (over a procedural paradigm)
Code can easily be reused…
…classes can be used in other programs
…inheritance can be to extend upon existing classes
…as a class can be based on an existing class
Easier to maintain….
….as classes can be modified or extended
…debugging can be easier as encapsulation limits how attibutes are changed.
Code can be more secure as access to attributes can be restricted to being via methods.
Better for coding as part of a team as classes can be distributed between team members
Disadvantages of OOP
OOP design techniques can result in a 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
Diagrams that show inheritance relationships
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