Section 12: Object-Oriented Programming and Functional Programming Flashcards
Chapter 67:
What is Object-Oriented Programming.
Where the world is looked at as a collection of objects.
Objects are represented as complex data structures consisting of different attributes and methods.
Objects are instances of Classes; Classes define attributes and methods.
Chapter 67:
What is an attribute?
What would be an attribute of a car object?
An object-bound variable.
Brand / Colour / Engine Size.
Chapter 67:
What is a method?
What would be a method of a car object?
An object-bound function or procedure.
Accelerate / Break.
Chapter 67:
What are the 4 main concepts in OOP?
Instantiation,
Encapsulation,
Inheritance,
Polymorphism.
Chapter 67:
What is Encapsulation?
Making attributes hidden, and creating messages to access them.
Chapter 67:
What are messages in OOP?
Getters and Setters.
Methods that return the value of a private attribute or change the value of a private attribute.
Chapter 67:
What is Inheritance?
Where one class copies the attributes and methods of another class into itself. Other methods and attributes can then be added to personalise the subclass.
Chapter 67:
What do we call the different types of classes involved in Inheritance?
Super Class - Parent Class
Sub Class - Child Class
Chapter 67:
What rule do we use to determine whether Inheritance is appropriate?
The “is an X a Y” / “is a” rule
For Example:
Is a Cat an Animal?
Yes, therefore Cat should be a subclass of Animal.
Chapter 68:
What is Association?
Related but independent classes.
“X has a Y” / “has a” rule.
For Example:
A teacher has a student.
A student has a teacher.
They are Associated, but there is no Ownership between them.
Chapter 68:
What is Aggregation?
When one class is contained by another (e.g. A player is part of a team), and the object (player) is not destroyed when the container (team) is destroyed. (The player does not cease to exist if the team disbands).
This can be represented with a hollow diamond on a graph. (With the diamond by the container).
Chapter 68:
What is Class Composition?
When one class is contained by another (e.g. A room is part of a hotel), and the object (room) is destroyed when the container (hotel) is destroyed. (When a hotel is destroyed, the rooms are as well).
This can be represented with a solid diamond on a graph. (With the diamond by the container).
Chapter 68:
Association can be Unidirectional or Bidirectional.
What is the difference? Give examples.
Unidirectional is one-way.
Bidirectional is two-way.
Examples:
Unidirectional: Customer places an Order.
Bidirectional: A is married to B, B is married to A.
Chapter 68:
What is Polymorphism?
As an extension of Inheritance, Polymorphism is where methods are changed for a specific subclass.
For example: With a superclass Animal, you may have a method moveLeft() which decrements the posX attribute for the object by 3 spaces. If you have subclasses Cat and Mouse, the mouse may not be able to move 3 spaces at once, so Polymorphism may be used to override the moveLeft() method, so that the posX attribute is decremented by 2 spaces.
Alternatively, you could define a specialMove() method, and have a subclass that completely changes the contents of the method, just keeping the same name.
Chapter 68:
How is Polymorphism achieved?
By overriding methods.
By defining a method of the same name and parameters as the inherited method.
Chapter 68:
Inheritance or Composition;
Which is preferable, and why?
Composition.
With Composition, you are not bound by the tree structure of Inheritance.
If you have a specific object [for example a robot dog] you would need to inherit certain methods and attributes from a subclass [dog might be a subclass of animal]. If you then don’t want your specific object to inherit from the superclass, you would have to either duplicate code, or give the specific object more methods than they need.
With Composition, you define the methods that you need, and then define your classes with access to certain methods.
Chapter 68:
What are the 3 Access Modifiers / Access Specifiers?
Public
Private
Protected
Chapter 68:
What is the Public Access Modifier / Access Specifier?
The member is accessible from outside of the class.
Chapter 68:
What is the Private Access Modifier / Access Specifier?
The member is only accessible inside of the class.
Chapter 68:
What is the Protected Access Modifier / Access Specifier?
The member is accessible within the class, and within subclasses.
Depending on the programming language, the member may or may not be accessible by classes in the same package.
Chapter 68:
What is the structure of a Class Diagram?
1x3 table.
Name in the top.
Attributes in middle.
Methods at the bottom.
Public indicated by ‘+’
Private indicated by ‘-‘
Protected indicated by ‘#’
Chapter 68:
What is an interface in OOP?
When multiple objects require the same set of methods, an interface could be used to collect all of the shared methods and then referenced as one call in the different objects.
For example SwitchOn() SwitchOff() SetTimer(time) GetTimer() ...
May be used by a microwave, a lighting system, a computer or many other objects. The listed methods could be placed in an interface called Switches, for example, and the objects could implement the interface, rather than repeating several method definitions.
Chapter 68:
What are the advantages of Object-Oriented Programming?
The methodology forces designers to plan thoroughly, making for better designs with fewer weaknesses.
Once an object is created, knowledge of the implementation is not required for use. This is especially important for collaborative projects.
New objects, slightly different from others can be created easily.
Easier to maintain, as the structure is enforced.
Chapter 68:
What are the disadvantages of Object-Oriented Programming?
It can be inefficient, as the structure can sometimes be more memory and CPU intensive than alternative methods.
Requires time and planning, which can slow down a project. This could lead to missing deadlines.
For less intensive projects, Object-Oriented structure can make the code look bloated, as efficiency may not be drastically improved, while complexity will drastically increase.
Chapter 69:
What is a programming paradigm?
Different styles of programming.