Chapter 9 Flashcards

1
Q

What does inheritance mean?

A

Inheritance is the act of deriving a new class from an existing one.

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

What’s the difference between parent and child class?

A

The parent class inherits the methods and data of the parent class, except the costructor.

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

What’s the benifit of using inheritance?

A

Inheritance allows the reuse of already prexisting code. This doesn’t ease only the coding itself, but also the testing and the structure of the software.

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

What does the protected modifier do?

A
The protected modifier allows the child class access instance data and methods of the parent class, but it allows these to remain encapsulated.
The protected modifier allows access even for every class in the same package as the class it's used in.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the super reference do?

A

The super reference allows the child class to use the parent class’ constructor.

Example:
parent:
public Book (int numPages) {
...
}
...
child:
public Dictionary (int numPages, int numDefinitions) {
super(numPages);
...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Does the constructor of the child need always to call the constructor of the parent?

A

The constructor of a sublacc always calls the constructor of the superclass.

In case the superclass doesn’t have a redefined constructor, the default constructor (with no arguments) will be called.

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

What is multiple inheritance? How is it used in Java?

A

Multiple inheritance consists of a child class being derived from two or more parent classes.

Java doesn’t implement multiple inheritance, and it’s in general not even needed.

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

What does overriding mean?

What determines which method is called?

A

Overriding consists in dealing with a method of the parent and the child class that has the same name and signature.

The type of object executing the method determines which version of the method is invoked.

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

What is the concept of shadowing variables?

A

It consists in defining variables inside the child class with the same name.

Such practice should be avoided, as it generates confusion.

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

What is the purpose of creating a class hiearchy?

A

A class hiearchy works is put on the purpose to structure parents, their childs and the child of their own.

Common features should be put as high in the hiearchy possible.

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

What is the uniquity of the Object class?

A

The Object class has no parent. In fact, every other class is a child of this class.

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

What methods of all existing classes are inherited from the Object class?

A

toString(): returns the name of the object’s class and a hash code.

equals(): returns true if two references are alieses.

clone(): clones a reference.

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

What is an abstract class? What is its behaviour?

A

An abstract class is a placeholder in a class hierarchy that represents a generic concept.

An abstract class cannot be instantiated.

An abstract class is declared using the abstract modifier.

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

What happens to the child of an abstract class?

A

The child of an abstract class must override the abstract methods of the parent.

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

Can an abstract class contain instance data?

A

Although an abstract class cannot be initiated, it can contain instance data. This data will be instantiated when any non-abstract child class will be initiated.

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

Why can’t abstract methods be defined as final? Or static?

A

An abstract method cannot be declared final, as the purpose of an abstract method is to be redefined in all non-abstract child classes. If a method is declared final, how can it be redefned?

An abstract method cannot be static as Java doesn’t allow it.

17
Q

When are abstract classes used and when interfaces are?

A

Abstract classes are used if there are several closely related classes. Especially if these are have many common methods or fields.

Interfaces are used in case unrelated classes would implement it. They’re used to specify the behaviour of a particular data type.

18
Q

Can private members of a parent class be referenced in child class?

A

Child classes cannot reference directly private members of its parent class.
On the other hand, child classes can reference them indirectly by using the methods of the parent class.

19
Q

What does the ‘super’ modifier do?

A
The 'super' modifer is used inside a child class to reference its first parent class.
super(..,, ...) to invoke the parent's constructor, used often inside the child's constructor.
20
Q
What happens if a method is declared final?
What happens if a class is declared final?
A
A final method cannot be overriden in any derived classes.
A final class cannot be used to derive any children at all.
21
Q

What are dialog boxes?

A

Dialog boxes are used to interact with theuser by displaying messages or asking for input.

22
Q

What does the following do?

FileChooser chooser = new FileChooser();
File selectedFile = chooser.showOpenDialog(primaryStage);
A

This code allows the user to pick a file.