Lecture 2 Flashcards
What is a generalised class?
A super class that is general:
Car
What is a specialised class?
Subclass of an abstract class:
(super) Car
(sub) ElectricCar and GasCar
What is polymorphism?
When there are one or more classes or objects related to each other by inheritance (a task that performs a single action in different ways).
You make some specialisation of a generalisation.
Give an example of polymorphism.
Say we have a super class Car that has the subclasses GasolineCar, ElectricCar etc. Let’s say we have another super class called CarGarage that has the method repair(Car). This means that this single action, repair(Car), will repair any subclass inherited from the super class Car.
What does method overriding do?
It will change up the method implementation of the super class, but it will keep the method signature. When calling a super method, you say:
super.drive();
// alternative method implementation
What is method overloading?
It provides additional method implementation, and will change the method signature. Methods of the same name need to have different parameters: drive(Direction direction) and drive(Location location)
What visibility modifier are attributes usually assigned?
(-) Private (few cases where (+) public and (#) protected make sense)
What visibility modifier are methods usually assigned?
(+) Public (internal use can either be private or protected)
What does super do?
Super accesses ancestor
What should you do, if you want to grant external access to private attributes?
Define getter and
setter methods.
What is a getter method?
Provides read access of private or protected attributes.
What is a setter method?
Provides write access of private or protected attributes.
How do you make a getter method?
public int getAge() {
return age;
}
When to use a setter?
Use a setter when you want to change a value (more than once)
How do you make a setter method?
public void setAge(int age) {
this.age = age;
}
What is enum?
Represent a fixed set of elements
When should a constructor be used over setters?
When the attribute is not likely to change. (like breed/color)