Java OCA: Inheritance Flashcards
Fix terrible brainscape features
Which base class members AREN’T inherited by a class?
- private and default access members
* constructors of the base class
Can a base class without abstract methods be defined as an abstract class?
Yes
Can you ever create objects of an abstract class?
No
What happens if a derived class doesn’t implement all the abstract methods of its base class?
It will fail to compile UNLESS it is defined as an abstract class.
What are the implicit modifiers of an interface and its components?
- interface: public
* interface variables: public, static, final.
What is the explicit way of writing interface Cheetah{ double distance = 21; int height(); }
interface Cheetah{
public static final double distance = 21;
public abstract int height(); }
Can interfaces include class names?
No, an interface can never extend any class.
Can you define a top-level, protected Interface?
No, only possibilities are
• public
• no modifier (default access)
[ But inner or nested types can have any access level ]
Which, if any, lines fail to compile,
- interface MyInterface {
- private int number = 10;
- protected void aMethod();
- interface interface2{}
- public interface interface4{}
- }
2+3: “illegal combination of modifiers: public and private” All members of an interface are inherently public.
What are valid, non-access modifiers for an interface?
- abstract
* strictfp
Which of the following compile successfully?
- final interface MyInterface{}
- static interface MyInterface{}
- transient interface MyInterface{}
- synchronized interface MyInterface{}
- volatile interface MyInterface{}
None of them. Valid top-level interface modifiers include
• no modifier, public (access)
• abstract, strictfp (nonaccess)
What kinds of methods can be defined in an interface?
- abstract
- default
- static
What kind of method is eatPotato()?
interface Interviewer { void eatPotato(); }
• abstract
Which of these is a valid default method?
- interface Interviewer { default void submitInterviewStatus(); }
- interface Interviewer { default void submitInterviewStatus(); { return 0; } }
- Declaration of a default method must be followed by the method body marked using {}.
How can a developer add methods to an interface without breaking existing implementations?
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.