Java OCA: Inheritance Flashcards

Fix terrible brainscape features

1
Q

Which base class members AREN’T inherited by a class?

A
  • private and default access members

* constructors of the base class

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

Can a base class without abstract methods be defined as an abstract class?

A

Yes

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

Can you ever create objects of an abstract class?

A

No

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

What happens if a derived class doesn’t implement all the abstract methods of its base class?

A

It will fail to compile UNLESS it is defined as an abstract class.

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

What are the implicit modifiers of an interface and its components?

A
  • interface: public

* interface variables: public, static, final.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the explicit way of writing
    interface Cheetah{
    double distance = 21;
    int height(); }
A

interface Cheetah{
public static final double distance = 21;
public abstract int height(); }

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

Can interfaces include class names?

A

No, an interface can never extend any class.

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

Can you define a top-level, protected Interface?

A

No, only possibilities are
• public
• no modifier (default access)
[ But inner or nested types can have any access level ]

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

Which, if any, lines fail to compile,

  1. interface MyInterface {
  2. private int number = 10;
  3. protected void aMethod();
  4. interface interface2{}
  5. public interface interface4{}
  6. }
A

2+3: “illegal combination of modifiers: public and private” All members of an interface are inherently public.

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

What are valid, non-access modifiers for an interface?

A
  • abstract

* strictfp

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

Which of the following compile successfully?

  1. final interface MyInterface{}
  2. static interface MyInterface{}
  3. transient interface MyInterface{}
  4. synchronized interface MyInterface{}
  5. volatile interface MyInterface{}
A

None of them. Valid top-level interface modifiers include
• no modifier, public (access)
• abstract, strictfp (nonaccess)

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

What kinds of methods can be defined in an interface?

A
  • abstract
  • default
  • static
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What kind of method is eatPotato()?

interface Interviewer { void eatPotato(); }

A

• abstract

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

Which of these is a valid default method?

  1. interface Interviewer { default void submitInterviewStatus(); }
  2. interface Interviewer { default void submitInterviewStatus(); { return 0; } }
A
  1. Declaration of a default method must be followed by the method body marked using {}.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can a developer add methods to an interface without breaking existing implementations?

A

Only by adding default methods. When a class implements and interface with abstract methods, the class must implement all of the methods or else it won’t compile.

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

Given
interface Interviewer {
abstract void conductInterview();
default void submitInterviewerStatus() {
System.out.println(“Accept”);
}
static void bookConferenceRoom(LocalDateTime dateTime, int duration) {
System.out.println(“Interview scheduled on:” + dateTime);
System.out.println(“Book conference room for: “ + duration + “ hrs”);
}
}
Which of the following lines, if any, fail to compile?

  1. class Manager implements Interviewer {}
  2. class Project {
  3. public static void main(String [] args) {
  4. Interviewer inv = new Manager();
  5. inv.bookConferenceRoom(LocalDateTime.now(), 2);
  6. Manager mgr = new Manager();
  7. mgr.bookConferenceRoom(LocalDateTime.now(), 2);
    8.Interviewer.bookConferenceRoom(LocalDateTime.now(), 2);
    }
    }
A

5, 7: A static method in an interface can’t be called using a reference variable, it must be called using the interface name.

[Note: but it is fine to call a static variable defined in a CLASS using either the reference variables or by the name of the class.]

17
Q
Which of the following compile successfully?
1.  interface Interviewer {
         abstract int interviewConducted();
     }
     class Manager implements Interviewer {}
2. interface Interviewer {
         abstract int interviewConducted();
     }
     class Manager implements Interviewer {
          int interviewConducted();
          return 1;
     }
3. interface Interviewer {
         abstract Object interviewConducted();
     }
     class Manager implements Interviewer {
          public String interviewConducted() {
          return null;
          }
     }
4. interface Interviewer {
         abstract Object interviewConducted();
     }
     class Manager implements Interviewer {
          public Integer interviewConducted() {
          return 1;
          }
     }
A
  1. No: doesn’t implement interviewConducted()
  2. No: attempts to assign weaker access privilege to interviewConducted()
  3. Compiles successfully
  4. No: return type Integer isn’t compatible with int