T4 - Interfaces and Inheritance

Bearbeiten

1 What is true of an interface?

It cannot be instantiated.
It is a type.
It contains both static and object method signatures.
It cannot be extended.
Its type can be used to declare a variable.
It contains no method implementations.

2 Given the following class declarations:

public interface Inter {...}
public class Class1 implements Inter {...}
public class Class2 extends Class1 {...}


Which of the following statements are illegal?

Class1 var = new Inter();
Inter var = new Class2();
Class1 var = new Class2();
Inter var = new Class1();
Inter var = new Inter();
Class2 var = new Class1();
Class2 var = new Inter();

3 If a class has no default constructor, what are the consequences?

It cannot be extended.
Any subclass must explicitly call one of the superclass constructors in its constrctors.
It is illegal to define a class without default constructor.
It cannot be instantiated.

4 Given the following class definition:

public class A {

protected int b = 10;

}


What is wrong with the following subclass:

public class AB extends A {

void count() { b++; }
abstract int lapse();

}


AB must be marked abstract.
b cannot be used in count, because it is protected.
A subclass must not add abstract methods.
A class without methods cannot be extended.

5 There are empty interfaces. They are used to make a class member of a set and are called marker interfaces, e.g. interface Marker { }.

What is wrong with the following class?

class MyClass extends SomeClass implements Marker { }


A class must not have an empty body.
It is formally ok, but the class is uselessbecause it is empty.
Nothing, it is ok.
A class cannot extend a class AND implement an interface.
It does not implement Marker because it does not contain any method implementations.