Chapter 15 - Abstract Classes and Interfaces Flashcards

1
Q

What is an abstract class?

A

An abstract class is a class that cannot be used to create objects. It contains abstract methods.

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

How should constructors in abstract classes be created?

A

They should have the ‘protected’ modifier.

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

Can an abstract method be contained in a concrete class?

A

No. If there’s an abstract method, the class must be abstract.

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

What must the subclass do in regards to the abstract methods defined in the abstract superclass?

A

It must implement/override them all, or be defined as an abstract class itself.

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

Why would you want constructors in an abstract class, since you cannot instantiate the class?

A

Because they may be invoked by the constructors of concrete subclasses.

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

If GeometricObject is an abstract class, why can you do this?
GeometricObject[] objects = new GeometricObject[10];

A
Because you are not creating objects, you are creating 10 GeometricObject references inside the array. You can do that for the same reason you can do:
GeometricObject obj = new Circle();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is an Interface?

A

An interface is a class-like construct that contains only constants and abstract methods. It’s intent is to specify common behavior for objects of related classes or unrelated classes.

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

Where is the compareTo method defined?

A

It’s defined in the Comparable interface, and in all classes that implement the comparable interface.

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

What is the difference between abstract classes and interfaces in regards to variables?

A

Abstract classes has no restrictions in regards to variables, but interfaces can only have ‘public static final’ variables.

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

What is the difference between abstract classes and interfaces in regards to constructors?

A
An abstract class cannot be instantiated using the new operator, but subclasses can invoke the constructors through their own constructors using the super keyword.
Interfaces have no constructors.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between abstract classes and interfaces in regards to methods.

A

Abstract classes have no method restrictions. They may have abstract methods and concrete methods.
Interfaces can only have public abstract non-static methods.

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

What is the difference between abstract classes and interfaces in regards to inheritance?

A

Only single inheritance is allowed for abstract classes (a class may only extend one single class), but multiple inheritance is allowed for interfaces (a class may implement several interfaces).

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

How is an abstract class defined? What is the syntax?

A
An abstract class has the keyword 'abstract' in the header, like this:
modifier abstract class className.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How is an interface defined? What is the syntax?

A

An interface has the keyword ‘interface’ in the header, like this:
modifier interface interfaceName.

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

How does a class make use of an interface?

A
By using the keyword 'implements' in the header, like this:
class className implements interfaceName.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

When should you use interfaces, and when should you use abstract classes?

A

In general, interfaces are preferred over abstract classes because an interface can define a common supertype for unrelated classes. Interfaces are more flexible than classes. However, by using abstract classes, you can inherit the implementation of other (non-abstract) methods. You can’t do that with interfaces - an interface cannot provide any method implementations.

17
Q
Which of the following class definitions defines a legal abstract class?
A. class A { abstract void unfinished() { } }
B. class A { abstract void unfinished(); }
C. abstract class A { abstract void unfinished(); }
D. public class abstract A { abstract void unfinished(); }
A

C. abstract class A { abstract void unfinished(); }

18
Q
Which of the following declares an abstract method in an abstract Java class?
A. public abstract method(); 
B. public abstract void method(); 
C. public void abstract Method(); 
D. public void method() {} 
E. public abstract void method() {}
A

B. public abstract void method();

19
Q
Which of the following statements regarding abstract methods are true?
A. An abstract class can have instances created using the constructor of the abstract class.
B. An abstract class can be extended.
C. A subclass of a non-abstract superclass can be abstract. 
D. A subclass can override a concrete method in a superclass to declare it abstract.
 E. An abstract class can be used as a data type.
A

All but A are true.

20
Q
Which of the following statements regarding abstract methods are true?
A. Abstract classes have constructors.
B. A class that contains abstract methods must be abstract.
C. It is possible to declare an abstract class that contains no abstract methods. 
D. An abstract method cannot be contained in a nonabstract class. 
E. A data field can be declared abstract.
A

All but E are true.

21
Q
Suppose A is an abstract class, B is a concrete subclass of A, and both A and B have a default constructor. Which of the following is correct?
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();
A

B and D are true.

22
Q

What is the output of running class Test?

public class Test {
  public static void main(String[] args) {
    new Circle9();
  }
}
public abstract class GeometricObject {
  protected GeometricObject() {
    System.out.print("A");
  }
  protected GeometricObject(String color, boolean filled) {
    System.out.print("B");
  }
}
public class Circle9 extends GeometricObject {
  /** Default constructor */
  public Circle9() {
    this(1.0);
    System.out.print("C");
  }
  /** Construct circle with a specified radius */
  public Circle9(double radius) {
    this(radius, "white", false);
    System.out.print("D");
  }
  /** Construct a circle with specified radius, filled, and color */
  public Circle9(double radius, String color, boolean filled) {
    super(color, filled);
    System.out.print("E");
  }
}
A

BEDC

23
Q

Analyze the following code.

    Number[] numberArray = new Integer[2];
    numberArray[0] = new Double(1.5);
A. You cannot use Number as a data type since it is an abstract class.
B. Since each element of numberArray is of the Number type, you cannot assign an Integer object to it.
C. Since each element of numberArray is of the Number type, you cannot assign a Double object to it.
D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.
A

D. At runtime, new Integer[2] is assigned to numberArray. This makes each element of numberArray an Integer object. So you cannot assign a Double object to it.

24
Q

Which of the following is a correct interface?
A. interface A { void print() { }; }
B. abstract interface A { print(); }
C. abstract interface A { abstract void print() { };}
D. interface A { void print();}

A

D. interface A { void print();}

25
Q
Which of the following is incorrect?
A. An abstract class contains constructors.
B. The constructors in an abstract class should be protected.
C. The constructors in an abstract class are private.
D. You may declare a final abstract class.
E. An interface may contain constructors.
A

C, D and E are true.

26
Q
\_\_ is a reference type.
A. A class type
B. An interface type
C. An array type
D. A primitive type
A

A, B and C

27
Q

Show the output of running the class Test in the following code lines:

interface A {
}

class C {  
}
class B extends D implements A {
}
public class Test {
  public static void main(String[] args) {
    B b = new B();
    if (b instanceof A)
      System.out.println("b is an instance of A");
    if (b instanceof C)
      System.out.println("b is an instance of C");
  }
}
class D extends C {  
}
A

b is an instance of A followed by b is an instance of C.

28
Q
Suppose A is an interface, B is a concrete class with a default constructor that implements A. Which of the following is correct?
A. A a = new A();
B. A a = new B();
C. B b = new A();
D. B b = new B();
A

B and D are true.