Chapter 9 Flashcards

1
Q

What is an abstract class

A

An abstract class is a class that cannot be instantiated and may contain abstract methods.

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

What is an abstract method

A

An abstract method is a method that does not define an implementation when it is declared.

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

True or false

An abstract class must have at least one abstract method.

A

False

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

True or false

An abstract method can only be defined in an abstract class.

A

True

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

True or false

An abstract class can include constructors

A

True

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

True or false

The final and abstract modifiers can be placed before or after the access modifier in classes and methods.

A

True

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

Why can an abstract class have a constructor?

A

Abstract classes are initialized with constructors the same way non-abstract classes are. The difference is that it can only be called through a super() method from a subclass.

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

Why does Java not permit a class or method be abstract and final?

A

With abstract, you intend for someone else to extend or implement it. With final you are preventing anyone from extending or implementing it.

These are in direct conflict with eachother.

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

Why does Java not permit a class or method be abstract and private?

A

With abstract, you intend for someone else to extend or implement it. with private, we restrict access to the class itself.

These are in direct conflict with eachother.

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

Why does Java not permit a method be abstract and static?

A

With abstract, you intend for someone else to extend or implement it. With static, it belongs to the class, not an instance of a class and thus cannot be overridden.

These are in direct conflict with eachother.

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

What are the abstract class definition rules?

A
  1. Abstract classes cannot be instantiated
  2. All top-level types, including abstract classes, cannot be marked protected or private
  3. Abstract classes cannot be marked final
  4. Abstract classes may include zero or more abstract and nonabstract methods.
  5. An abstract class that extends another abstract class inherits all of its abstract methods.
  6. The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.
  7. Abstract class constructors follow the same rules for initialization as regular constructors, except they can be called only as part of the initialization of a subclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the abstract method definition rules?

A
  1. Abstract methods may be defined only in abstract classes or interfaces
  2. Abstract methods cannot be declared private, static or final
  3. Abstract methods must not provide a method body/implementation in the abstract class in which they are declared.
  4. Implementing an abstract method in a subclass follows the same rules for overriding a method, including covariant return types, exception declarations, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Which access modifier is implicitely (automatically inserted by the compiler) used when declaring abstract methods or constant variables in an interface?

A

public.

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

What are the implicit (automatically inserted by the compiler) modifiers of interface variables?

A

public static final

all variables inside an interface are constant variables

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

What are the implicit (automatically inserted by the compiler) modifiers of interface methods?

A

public abstract

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

What implicit (automatically inserted by the compiler) modifier does an interface have?

A

abstract

17
Q

Can an interface be instantiated?

A

No, it is implicitely abstract

18
Q

Is this code valid?

interface Nocturnal {}
interface CanFly {}

interface HasBigEyes extends Nocturnal, CanFly {}

A

Yes.

Unlike classes, which can extend only one class, interfaces can extend multiple interfaces.

19
Q

Why is extending multiple interfaces in an interface allowed?

A

It is permitted because interfaces are not initialized as part of a class hierarchy. Unlike abstract classes, they do not contain constructors and are not part of instance initialization.

(page 378)

20
Q

Can an abstract interface method be declared private / protected?

A

No, as this will result in a conflict when the compiler tries to apply the public modifier.

21
Q

Does the following code compile?

abstract class Husky {
    abstract void play();
}
class Webby extends Husky {
    void play() {}
}
A

This compiles just fine :)

22
Q

Does the following code compile?

interface Poodle {
abstract void play();
}

class Georgette implements Poodle {
    void play() {}
}
A

This does not compile, as the play method in the interface is implicitely public. The play method implemented in Georgette is reducing the visibility of play().

(page 381 & 382)

23
Q

Can a class extend an interface?

A

Nope

page 383

24
Q

Can an interface extend a class?

A

No

page 383

25
Q

Can an interface extend an interface?

A

Yes.

An interface can extend one, or even mutliple, interfaces.

26
Q

Does the following code compile?

public interface Herbivore {
public void eatPlants();
}

public interface Omnivore {
public void eatPlants();
}

public class Bear implements Herbivore, Omnivore {
    ...
}
A

Yes.

As they have identical method declerations, they are also considered compatible.

(page 384)

27
Q

Does the following code compile?

public interface Dances {
public String swingArms();
}

public interface EatsFish {
public CharSequence swingArms();
}

public class Penquin implements Dances, EatsFish {
    ...
}
A

Yes.

One method will have to be implemented:

public String swingArms() {

}

String and CharSequence are covariant return types.

28
Q

What are the interface definition rules?

A
  1. Interfaces cannot be instantiated
  2. All top-level types, including interfaces, cannot be marked protected or private
  3. Interfaces are assumed to be abstract and cannot be marked final
  4. Interfaces may include zero or more abstract methods
  5. An interface can extend any number of interfaces
  6. An interface reference may be cast to any reference that inherits the interface, although this may produce an exception at runtime if the classes aren’t related.
  7. The compiler will only report an unrelated type error for an instanceof operation with an interface on the right side if the reference on the left side is a final class that does not inherit the interface.
  8. An interface method with a body must be marked default, private, static or private static.
29
Q

What are the abstract interface method rules?

A
  1. Abstract methods can be defined only in abstract classes or interfaces.
  2. Abstract methods cannot be declared private or final
  3. Abstract methods must not provide a method body/implementation in the abstract class in which it is declared.
  4. Implementing an abstract method in a subclass follows the same rules for overriding a method, including covariant return types, exception declarations, etc.
  5. Interface methods without a body are assumed to be abstract and public.
30
Q

What are the interface variable rules?

A
  1. Interface variables are assumed to be public, static and final (constant variables)
  2. Because interface variables are marked final, they must be initialized with a value when they are declared.