Ch9 Advanced Class Design Flashcards

1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
Q

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

A

abstract

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
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().

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

Can an interface extend an interface?

A

Yes.

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
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.