Classes and Objects 2 Flashcards
What are getters and setters?
Methods that generally initialising/updating/accessing instance variables
aka Accessor/Mutator
What are the default values when you create a new object?
The initial value of the instance variables are set to default values based on the data type
What are the cons of initializing objects with default values?
You would need 100 lines to call create value for each attribute
What is the definition of constructor?
A method used to create and initialise an object
What are the features of a constructor?
- The right hand side calls a class’ constructor
- Constructors are methods
- Constructors are used to initialize objects
- Constructors have the same name as the class
- Constructors cannot return values
- A class can have one or more constructors, each with a different set of parameters
What is the definition of method overloading?
Ability to define methods with the same name but with different signatures (argument types and/or numbers)
- This is a form of polymorphism (same method - different behaviour)
What is the definition of polymorphism?
Ability to process objects differently depending on their data type or class
How are methods with the same name distinguished?
- number of arguments
- type of arguments
- position of arguments
What is the definition of “this”?
A reference to the calling object, the object that owns/is executing the method
What is the definition of instance variable?
A variable in an instance of an object
example: CentreX, CentreY
What is the definition of static variables?
A variable that is shared among all objects of the class; a single instance is shared among all objects of the class. Such an attribute using the class name
example: numCircles
What is the definition of static members?
Methods and attributes that are not specific to any object of the class
What is the definition of Static Method?
A method that doesn’t depend on (access/modify) any instance variables of the class. Such a method is invoked (called) using the class name.
What are the constraints of static methods?
- Static methods can only call other static methods
- Static methods can only access static data
- Static methods cannot refer to Java keywords such as, this or super because they are related to objects (class instances)
- Do not make all methods and attributes in your classes static
How to decide to make an attribute or a method static?
Consider whether it is a class level member or an instance specific member