WEEK 3 - INHERITANCE AND DEBUGGING PROGRAMS Flashcards

1
Q

What is the subclass?

A

It is a class based on an existing class (the superclass). It is more specific than the superclass.

Example.
“circle” is more specific than “shape”

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

What is the superclass?

A

It is an existing and generalized class compared to the subclass.

Example.
“shape” is more specific than “circle”

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

What does a subclass inherit from the superclass?

A

All fields and methods.

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

How can methods be overridden?

A

The subclass can create a method with the same name as another method in the superclass. This version of the method will override/replace the method from the superclass.

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

What keyword is used to create/declare a subclass in Java?

A

extends.

Example.
public class Circle extends Shape {
private Point _center;
private double _radius;
public void draw() {…}
}

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

What are some of the benefits of inheritance?

A
  1. One superclass can be used to make many subclasses.
  2. Reusability within the code.
  3. Improves the structures of large programs (hierarchical).
  4. Modular programming (encapsulation).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a subclass capable of?

A

Inheriting from a superclass, it can override methods and add new properties and new methods.

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

How many superclasses can there be?

A

One.

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

How do you call the superclass version of an overridden method?

A

Using the super keyword.

Example.
super.draw()

**You can NOT override private methods

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

Can you override field variables?

A

No, as it causes confusion and nasty bugs.

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

A subclass has access to what?

A

All non-private fields and methods.

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

What does protected mean?

A

A field or variable is visible to subclasses without making it public.

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

Are superclass’s Constructor inherited?

A

No. They are invoked explicitly (using the super keyword) or implicitly (automatically invoked).

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

You must use the keyword super to call the superclass
constructor. Invoking a superclass constructor’s name in a subclass causes which error?

A

Syntax.

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

What signature must be added on all overrides?

A

@Override

It is usually put on a separate line. Compiler will give a syntax error if an override doesn’t occur.

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

What is the final modifier?

A

The final modifier can be used on classes and class members (data and methods) except that the final modifier can also be used on local variables in a method. A final local variable is a constant inside a method.

FINAL CLASSES AND METHODS CANNOT BE EXTENDED AND OVERRIDDEN.

17
Q

Why is the String class final?

A

The String class is immutable and that means that the object state (field variables) cannot be changed after creation.

18
Q

What are the two major types of errors?

A

Syntax and runtime.

19
Q

What are syntax errors?

A

Always caught by the compiler, they are almost always easier to fix than runtime errors.

20
Q

What are runtime (logic) errors?

A

Discovered by testing, can be much harder to tell why the error/bug is happening.