Working with Inheritance Flashcards

1
Q
Consider the following interface:
public interface IInt{
	int thevalue = 0;
}
True/false: any class that implements the this interface inherits this field. Therefore, it can be accessed using s.thevalue or just 'thevalue' inside the class.
A

True:

- Also, since it is static, it can also be accessed using IInt.thevalue or “implementing class”.thevalue.

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

True/false: As a rule, fields defined in an interface are public, static, and final. (The methods are public and abstract.)

A

True

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

True/false: static method cannot be overridden by a non-static method and vice versa

A

True

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

True/false: a static method cannot be shadowed by a static method in the subclass.

A

False

- Static methods are shadowed like static variables and therefore can’t be overridden

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

True/false: Fields defined in an interface are ALWAYS considered as public, static, and final. (Methods are public and abstract.)

A

True

- In fact, you cannot even declare a field to be private or protected in an interface.

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

Which one of these is a proper definition of a class Car that cannot be sub-classed:

  1. class Car { }
  2. abstract class Car { }
  3. native class Car { }
  4. static class Car { }
  5. final class Car { }
A

5
- Notice the distinction. For classes, final means it cannot be extended, while for methods, final means it cannot be overridden in a subclass.
The native keyword can only be used on methods, not on classes and instance variables.

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

True/false: Fields in an interface are implicitly public, static and final.

A

True

- Although you can put these words in the interface definition but it is not a good practice to do so.

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

Which of these statements about interfaces are true:
1. Interfaces are abstract by default.
2. An interface can have static methods.
3. All methods in an interface are abstract although you need not declare them to be so.
4, Fields of an interface may be declared as transient or volatile but not synchronized.
5. interfaces cannot be final.

A

1, 3, 5

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

Which of the following statements are correct:

  1. An abstract class can be extended by an abstract or a concrete class.
  2. A concrete class can be extended by an abstract or a concrete class.
  3. An interface can be extended by another interface.
  4. An interface can be extended by an abstract class.
  5. An interface can be extended by a concrete class.
  6. An abstract class cannot implement an interface.
A

1, 2, 3

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

True/false: if(b instanceof Bird) will fail to compile if b is not a subclass of bird or any of birds subclasses.

A

True

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

True/false: a*10/4-30; generates an int which can be returned as a double without any cast.

A

True

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

True/false: The return type should always be the same for overridden and overriding method.

A
False
- This is true for primitives, but for Objects the overriding method can take the same or any subclass as return type.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

True/false: A super( ) or this( ) call must always be provided explicitly as the first statement in the body of the constructor.

A

False

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

True/false: If a subclass does not have any declared constructors, the implicit default constructor of the subclass will have a call to super( ).

A

True

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

True/false: super(…) can only be called in the first line of the constructor but this(…) can be called from anywhere.

A

False

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

True/false: You can either call super(…) or this(…) from a constructor but not both.

A

True

17
Q

True/false: An overriding method can be made less restrictive than the overridden method, but not more restrictive.

A

True

18
Q

True/false: it is possible to make the overriding method in SubClass invoke the overridden method in BaseClass by calling super.methodname

A
True
- If the class is static it is also possible to use BaseClass.methodname
19
Q

Which of these statements about interfaces are true:

  1. Interfaces permit multiple implementation inheritance.
  2. Unlike a class, an interface can extend from multiple interfaces.
  3. Members of an interface are never static.
  4. Members of an interface may be static.
  5. Interfaces cannot be final.
A

2, 4, 5
- An interface can extend any number of other interfaces and can be extended by any number of other interfaces. Variables in interfaces are always static and method prototypes in interfaces can never be static.

20
Q

Which of the following are correct about “encapsulation”:

  1. Encapsulation is same as polymorphism.
  2. It helps make sure that clients have no accidental dependence on the choice of representation
  3. It helps avoiding name clashes as internal variables are not visible outside.
  4. Encapsulation makes sure that messages are sent to the right object at run time.
  5. Encapsulation helps you inherit the properties of another class.
A

2, 3

- 4 is dynamic binding, an outcome of polymorphism.

21
Q

Which of the following statements is/are true:

  1. Subclasses must define all the abstract methods that the superclass defines.
  2. A class implementing an interface must define all the methods of that interface.
  3. A class cannot override the super class’s constructor.
  4. It is possible for two classes to be the superclass of each other.
  5. An interface can implement multiple interfaces.
A

3
- Interface cannot “implement” anything. It can extend multiple interfaces. The following is a valid declaration :
interface I1 extends I2, I3, I4 { } (5)

22
Q

True/false: A class implementing an interface must define all the methods of that interface.

A
False
- not if the class is abstract
23
Q

True/false: Subclasses must define all the abstract methods that the superclass defines.

A
False
- not if the subclass itself is abstract
24
Q

True/false: A super class must have a non private constructor.

A
True
- Since the constructor is private, the subclass cannot access it and therefore, it needs to be made public. protected or default access is also valid.
25
Q

Which of the following are valid declarations inside an interface:

  1. void compute();
  2. public void compute();
  3. public final void compute();
  4. static void compute();
  5. protected void compute();
A

1, 2

- static, final is not allowed - all methods must be public