Lecture 9 Flashcards

1
Q

What is inheritance?

A

Inheritance allows us to create classes that are subclasses of other classes.

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

What are three different ways of describing: “A inherits B”

A

A is a subclass of B, A is derived from B, A is the child of B.

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

What are two different ways of describing: “B is inherited by A”

A

B is a superclass of A, B is the parent of A.

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

What two things are true about an object of class A, if class A inherits class B?

A
Every object, that is an instance of the subclass has all the properties from the superclass
On such an object, you can call all methods of the superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the: “Liskov substitution principle”?

A

If A is a superclass of B, then everywhere in the program where an object of Class A is requested, an object of class B is also accepted.

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

How do you define in Java that A is a subclass of B?

A

In the signature of A add “extends B”

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

What can access something that is ‘public’?

A

everything

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

What can access something that is ‘private’?

A

Only things inside the class

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

What can access something that has no access type specified?

A

The class and the package

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

What can access something that is ‘protected’?

A

The class, the package, and subclasses.

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

How can you call the constructor of a parent?

A

super()

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

What is the concept of Polymorphism

A

Everywhere where a class X is allowed, a subclass Y of x is also allowed, but NOT the other way around.

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

When can you get a ‘ClassCastException’?

A

When you try to cast something to a class, which it can’t be cast to.

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

What is method overriding?

A

Creating a method with the same signature as an already existing method in the inheritance-architecture but in a different class of the inheritance-architecture.

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

What is method overloading?

A

Creating a method with the same name as another method inside the class (or inheritance-architecture), but with a different parameter list (different amount, types or order).

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

What is dynamic binding?

A

Dynamic binding means that an object can be an instance of multiple classes (of a certain inheritance-architecture), but that it is unknown to the compiler (a.k.a. depending on circumstances) of what class this object is an instance.

17
Q

If there is dynamic binding, who “decides” what class an object is an instance of?

A

the JVM

18
Q

What is the superclass of all classes in java?

A

Object

19
Q

What does the final keyword do if added to an attribute?

A

It turns the attribute into a constant

20
Q

What does the final keyword do if added to a class?

A

It prevents you from making subclasses

21
Q

What does the final keyword do if added to a method?

A

It prevents you from redefining the method in subclasses

22
Q

What do you use an abstract class for?

A

To “combine” classes, by creating a class for a category to which they all belong, even though this category itself can’t “exist” in the real world.

23
Q

What does the abstract keyword do if added to a class?

A

It prevents you from creating instances of this class

24
Q

What does the abstract keyword do if added to a method?

A

It forces you to implement this method, when creating a subclass.

25
Q

When can you use the abstract keyword on methods? How do you define these methods?

A

You can only use it on methods in abstract classes. You don’t define them at all. abstract exampleMethod(). NO {}

26
Q

What keyword do you add to a class signature to signify that it uses an interface?

A

implements InterfaceNameHere

27
Q

What are the two differences between an interface and an abstract class?

A
an interface cannot have attributes, an abstract class can.
an interface cannot have implementations for methods, an abstract class can.
28
Q

Why would we use an interface and not an abstract class?

A

A class can only inherit from one other class, (so if it already inherits something else, it can’t inherit an extra abstract class), but they can implement as many interfaces as you’d like.

29
Q

What keyword is “invisibly” added to all methods in an interface?

A

abstract, (YOU DON”T ADD THIS YOURSELF!!!)