Object Orientated Programming Flashcards
(13 cards)
What are classes?
- Classes are used as blueprints or templates that can be used to create objects in OOP
- This method allows for reusable and organised code in a modular way
What are objects?
- An object is a representation of a real world entity
- A class is a blueprint that describes the properties and behaviours of objects, while an object is a specific instance created using the blueprint
What are constructors?
- A special method within a class that is automatically called when an object of that class is instantiated (created)
How is a class set up called ItemForSale with 3 attributes: itemName, price, discount
class ItemForSale
private itemName
private price
private discount
How to add a new object to a class ItemForSale with 3 attributes
public procedure new (givenItemName, givenPrice, givenDiscount)
itemName = givenItemName
price = givenPrice
discount = givenDiscount
endprocedure
tank = ItemForSale new (“tank”, 20, 5)
what is a method and what are the 2 different types?
- A method is a function that is associated with objects or classes that define the behaviour and actions that an object can perform
- The two types are functions and procedures
- Functions: performs a task and returns a value
- Procedure: performs a task but doesn’t return a value
Describe a public method
- Accessible and can be invoked by any code within the same class or from any external classes
- Changes to public methods may effect other parts of the code
- Used when you want to provide accessibility of certain functionalities of an object or class to other parts of your program
Describe a private method
- Only accessed within the same class and cannot be invoked by external code or other classes
- Changes to private methods have a localised effect
- Used when you have internal implementation details that should not be accessed or used by external code
What is an attribute?
- Refers to a data memory or a property associated with an object or class
- They define the state of an object and can have different values for different instances of the same class
What is inheritence?
- Allows a class to inherit the properties and behaviours of another class
- Allows a derived class to inherit and utilise existing code from the base class
- The derived class “is a” base class
- E.G: the base class is vehicle and derived class is car. A car “is a” vehicle
What is encapsulation?
- The practice of grouping data (attributes) and methods (functions) within a class
- Using encapsulation ensures that data remains secure and is not accidentally modified or misused by controlling access to them using access modifiers (public, private)
- It also helps to organise code by keeping related methods and data together
What is abstraction in encapsulation?
- Reduces complexity by hiding the implementation details of the object, making it easier to work with and understand
- Programmers can use methods and classes from other parts of the program without having to understand how they are constructed internally
What is polymorphism?
- Allows objects to take on different forms or behaviours
- Different objects can share the same name or behaviour but can work in different ways
- It helps make code more flexible, reusable and easier to manage