Chapter 12 Flashcards

1
Q

Abstract classes

A

-Classes that are not intended for creating objects but serve only as superclasses
-they are restricted and cannot be used to create objects
-may contain abstract methods

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

When to use abstract classes?

A

-you want to share code amont several closely related classes
-You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
-You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.

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

Abstract method

A

-A method declared w/o an implementation (without braces, and followed by a semicolon), like this:
abstract void moveTo(double deltaX, double deltaY);

-the definition consists of a method header w/o a method body. It is marked with the keyword abstract.

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

T/F: An abstract method can be executed

A

F- it has not body, so can never be executed

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

What purposes do declaring a class abstract serve?

A
  1. No instances can be created of abstract classes
  2. Only abstract classes can have abstract methods
  3. Abstract classes with abstract methods force subclasses to override and implement those methods declared abstract
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What happens if a subclass does not provide an implementation for an inherited abstract method?

A

-it is itself abstract and no instances may be created- for it to be concrete it must provide implementations for all inherited abstract methods.

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

Java Interface

A

a specification of a type (in the form of a type name and a set of methods). It often does not provide an implementation for most of its methods.

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

What are the most significant features of interfaces?

A
  1. keyword interface is used instead of class in the header of the declaration.
  2. No constructors
  3. No instance fields
  4. Only fields that are constant class fields (static and final) with public visibility are allowed in an interface. The public, static, and final keywords may be omitted, therefore; they are assumed automatically.
  5. Abstract methods do not have to include the keyword abstract in their header.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

A class is said to implement an interface if it includes an ___ in its class header

A

-implements clause
ie.
public class Fox extends Animal implements Drawable
{

}

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

Default methods in interfaces

A

-Methods that can have a body
- provides additional functionality to a given type without breaking down the implementing classes.
-it should be clear that the functionality possible in a default method is strictly limited, since there is no state that can be examined or manipulated directly by them.

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

2 Main purposes of using inheritance

A
  1. Inherit code (code inheritance)- useful for code reuse
  2. Inherit type (subtyping)- for polymorphism and specialization
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Describe what we inherit from concrete classes, interfaces and abstract classes

A
  1. inherit from concrete- both implementation and type
  2. inherit (implement) from interfaces- we separate the 2, inherit the type but usually no implementation
  3. inherit from abstract- inherit type and partial implementation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Java allows any class to extend at most __ other class/es

A

one
-however, it allows a class to implement any number of interfaces (in addition to possibly extending one class)

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

How would we define class Hunter to implement Actor and Drawable as interfaces

A

public class Hunter implements Actor, Drawable
{

}
-it inherits the methods of the interfaces as abstract methods
-now it must provide method definitions for both of them by overriding the methods, or the class itself must be declared abstract.

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

T/F: Where a class implements multiple interfaces and two or more of the interfaces have a default method with the same signature then the implementing class must override that method—even if the alternative versions of the method are identical.

A

T

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