OOP Flashcards
1
Q
What is a class
A
A class is a template for an object and defines the state and behaviour of an object
2
Q
How are the state and behaviour of an object defined
A
- State- given by attributes which give an object’s properties
- Behaviour - given by the methods associated with a class, which describe the actions it can perform
3
Q
What is an object
A
An object is a particular instance of a class
4
Q
What are setters and getters
A
- A setter is a method that sets the value of a particular attribute
- A getter is a method which retrieves the value of a given attribute
5
Q
Why are getters and setters used
A
- To make sure attributes cannot be directly accessed and edited by users
- Attributes are declared as private so can only be altered by public methods
- Every class must also have a constructor method, which is called ‘new’. A constructor allows a new object to be created
6
Q
What is encapsulation
A
A property of object-oriented programming which makes it so attributes cannot be directly accessed and edited by users
7
Q
What is Inheritance
A
- A property of object-oriented programming.
- Due to inhertaince a class can inherit from another class
- The subclass will possess all of the methods and attributes of the superclass
8
Q
What is Polymorphism
A
- A property of OOP that means objects can behave differently depending on their class.
- This can result in the same method producing different outputs depending on the object involved
9
Q
What is overriding and overloading
A
- Overriding - redefining a method within a subclass and altering the code so that it functions differently and produces a different output
- Overloading - passing in different parameters into a method
10
Q
What are the advantages of Object-Oriented Programming (OOP)
A
- Polymorphism and Inheritance - these properties allow OOP for a high level of reusability, which makes it useful for projects where there are multiple components with similar properties.
- Encapsulation - makes the code more reliable by protecting attributes from being directly accessed
- Faster - code for different classes can also be produced independently of others
- Adavanced Planning - OOP requires advance planning to determine how the problem will be broken down into classes and how these will link to each other. A thorough design can produce a higher-quality piece of software with fewer vulnerabilities. - The modular structure used in OOP makes it easy to maintain and update.
- Abstraction- it is not necessary for programmers to know details about how code is implemented. Once classes have been created and tested, they can be reused as a black box which saves time and effort
11
Q
What are the disadvantages of Object-Oriented Programming (OOP)
A
- This is a different style of programming and so requires an alternative style of thinking. This can be difficult for programmers accustomed to other paradigms to pick up.
- Small Projects - Generally unsuitable for smaller problems, OOP is not suited to all types of problems. Where few components are reused, OOP may in fact result in a longer, more inefficient program.