Lecture 2 Flashcards

1
Q

What is a generalised class?

A

A super class that is general:
Car

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a specialised class?

A

Subclass of an abstract class:
(super) Car
(sub) ElectricCar and GasCar

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is polymorphism?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give an example of polymorphism.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does method overriding do?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is method overloading?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What visibility modifier are attributes usually assigned?

A

(-) Private (few cases where (+) public and (#) protected make sense)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What visibility modifier are methods usually assigned?

A

(+) Public (internal use can either be private or protected)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does super do?

A

Super accesses ancestor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What should you do, if you want to grant external access to private attributes?

A

Define getter and
setter methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a getter method?

A

Provides read access of private or protected attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a setter method?

A

Provides write access of private or protected attributes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you make a getter method?

A

public int getAge() {
return age;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

When to use a setter?

A

Use a setter when you want to change a value (more than once)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How do you make a setter method?

A

public void setAge(int age) {
this.age = age;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is enum?

A

Represent a fixed set of elements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

When should a constructor be used over setters?

A

When the attribute is not likely to change. (like breed/color)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is an abstract class?

A

A class that can’t be instantiated, because it doesn’t make logical sense. E.g. Pet has to always be either a Cat or a Dog, never just a Pet.

19
Q

What is an abstract method?

A

A method that doesn’t have an implementation yet, so the subclasses have to implement the method themselves

20
Q

How do you make a constructor?

A

public Animal(String breed, String color) {
this.breed = breed;
this.color = color;
}

21
Q

What is a concrete class?

A

The opposite of an abstract class. A “normal” class/default class

22
Q

Is it possible to have multiple constructors in one class?

A

Yes

23
Q

What is a static type?

A

Something that is known during development.
Pet somePet = new Dog(…);
PET IS STATIC

24
Q

What is a dynamic type?

A

Something that is known during execution.
Pet somePet = new Dog(…);
DOG IS DYNAMIC

25
Q

What are generics in collections?

A

Already made method to create List arrays, Integer arrays, etc.
E.g.: List<Pet> allPets = new ArrayList<Pet>();</Pet></Pet>

26
Q

What are the behaviours of generic collections?

A

If you insert a wrong element type to a list, it will get rejected.
You know all the elements in the list is of the same type.
If no generic argument is specified, Object is assumed, and all types are treated as Obejcts.

27
Q

What does “instanceof” do?

A

Checks whether object is an instance of the specified type.
if (pet instanceof Dog) {
//…
}

28
Q

What are lists good for?

A

Dynamically changing things, easy to access

29
Q

What are arrays good for?

A

Fixed number of values required, and when speed of access is more important than convenience of access

30
Q

What does classes in OOP do?

A

Combine coherent data and operations on that data

31
Q

What does abstraction mean?

A

Focusing on essentials and leaving out unnecessary details

32
Q

What does encapsulation mean?

A

Protects data and operations from unwanted external access.

33
Q

What does inheritance mean

A

Concept for code reuse as common data and operations can be shared
by multiple classes.

34
Q

What does “this” keyword mean?

A

Refers to the current instance of the class it is used in (“super()” if used in subclass)

35
Q

What does “super” keyword mean?

A

Used to call a previous method from the parent/super class, when a previous method has been overwritten.

36
Q

What is an interface?

A

Specify how a class’ public methods should look like (signature) but has no implementation. Defines a contract specifying a set of method signatures that classes must implement, enabling multiple inheritance of behavior.

37
Q

Difference between interface and abstract class?

A

Abstract class’ can have concrete methods or abstract methods that the subclasses must implement

Interface will contain methods that the class’ must implement themselves.

38
Q

What is the advantage of interfaces?

A

They have multiple inheritance. Makes sure you don’t forget to implement any methods in the implemented class.

39
Q

What does an interface look like?

A

// Interface
interface Animal {
public void animalSound(); // interface method (does not have a body)
}

// Pig “implements” the Animal interface
class Pig implements Animal {
public void animalSound() {
// The body of animalSound() is provided here
System.out.println(“The pig says: wee wee”);
}

40
Q

How does inheritance work for classes?

A

They have single inheritance, meaning a subclass can only have one parent

41
Q

What is a constructor?

A

A special method that is used to initialize objects (do not have a return type, only done once and are immutable). The constructor is called when the object is created.

All classes have constructors by default: if you do not create a class constructor yourself, Java creates one for you. However, then you are not able to set initial values for object attributes.

42
Q

Can software ever be done?

A

No, software is never “done”, change is a reality of life

43
Q

What is software development?

A

Software development is iterative and incremental