Class Definitions Flashcards
What is the main characteristic of object instances in a class?
Each object instance will have the same behaviour but different property values or ‘state’.
What are the three basic components of a class?
- Properties
- Constructors
- Methods
What is the process of creating an object from a class called?
Instantiation
Define the term ‘class’ in the context of object-oriented programming.
A class is the model or blueprint from which an object is created.
True or False: A class reserves memory space for data.
False
What are properties in a class?
Properties store data representing the object’s state.
What is the role of constructors in a class?
Constructors allow each object to be initialised when created.
What do methods implement in a class?
Methods implement the behaviour of the object.
Fill in the blank: An object is an instance of a _______.
[class]
List the properties of a TicketMachine class.
- Price
- Balance
- Total
What is an accessor method?
An accessor method returns information about the state of the object.
What is a mutator method?
A mutator method changes the state of the object by modifying its properties.
Describe the purpose of the ‘insertMoney’ method in a TicketMachine.
It updates the current balance by adding the amount of money inserted.
What is the default constructor?
A constructor with no parameters that Java automatically provides if none is defined.
What happens when a method is invoked?
The flow of control jumps to the method and executes its code.
What is the purpose of the ‘printTicket’ method?
It prints a ticket and updates the total collected while resetting the balance.
What check does the improved ‘insertMoney’ method perform?
It checks that the customer enters a positive amount of money.
What does the improved ‘printTicket’ method check before printing a ticket?
It checks that the balance is greater than or equal to the price.
What is instance data?
Instance data refers to the values of the properties that each instance of a class has.
Define formal parameters.
Formal parameters are the parameter names used inside a constructor or method.
Define actual parameters.
Actual parameters are the values supplied to a constructor or method.
What is the significance of the return type in a method?
The return type indicates what type of value the method will return to the caller.
What is the purpose of the ‘getPrice’ method?
It returns the price of a ticket.
What does the constructor of the TicketMachine class do?
It initializes the properties of the TicketMachine object.