Misc Flashcards

1
Q

Rules for overriding a method

A
  1. The method in the child class must have the same signature as the method in the parent class.
  2. The method in the child class must be at least as accessible or more accessible than the method in the parent class.
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
  4. The if the method returns a value, it must be the same or a subclass of the method in the parent class, aka covariant.

Same signature
At least as accessible
No new or broader exceptions
covariant returns (Covariant return type works only for non-primitive return types)

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

Rules for hiding a method

A
  1. The method in the child class must have the same signature as the method in the parent class.
  2. The method in the child class must be at least as accessible or more than the method in the parent class.
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method.
  4. If the method returns a value, it must be the same or a subclass of the method in the parent class, aka covariant.
  5. The method defined in the child class must be marked as static if it is marked as static in the parent class. Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class.
Same signature
At least as accessible
No new or broader exceptions
covariant returns (Covariant return type works only for non-primitive return types)
marked static
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Abstract Class Definition Rules

A
  1. Abstract classes cannot be instantiated directly.
  2. Abstract classes may be defined with any number, including zero, of abstract and non abstract methods.
  3. Abstract classes may not be marked as private, protected, or final.
  4. An abstract class that extends another abstract inherits all of its abstract methods as its own abstract methods.
  5. The first concrete class that extends an abstract class must provide an implementation for all of the inherited abstract methods.

No direct instantiation
Zero or more methods
No private, protected, or final
A child abstract class inherits all parent abstract methods
First concrete class implements all abstract methods

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

Abstract Method Definition Rules

A
  1. Abstract methods may only be defined in abstract classes.
  2. Abstract methods may not be delcared private or final.
  3. Abstract methods must not provide a method body/implementation in the abstract class for which it is declared.
  4. Implementing an abstract method in a subclass follows the same rules for overriding a method. For example, the name and signature must be the same, and the visibility of the method in the subclass must be at least as accessible as the method in the parent class.

Only in abstract classes
No private or final
No body
Same rules for overriding a method

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

Rules for defining an interface

A
  1. Interfaces cannot be instantiated directly.
  2. An interface is not required to have any methods.
  3. An interface may not be marked final.
  4. All top level interfaces are assumed to have public or default access. They are assumed to be abstract whether this keyword is used or not. Therefore, making a method private, protected or final will trigger a compiler error as it is incompatible with these assumptions.
  5. All non default methods in an interface are assumed to have the modifiers abstract and public in their definition. Therefore, making a method as private, protected, or final will trigger compiler errors as these are incompatible with the abstract and public keywords.
No direct instantiation
Zero or more methods
No final
Assumed public or default.
Assumed abstract.
Non default methods assumed abstract and public
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Default interface method rules

A
  1. A default method may only be declared within an interface and not within a class or abstract class.
  2. A default method must be marked with the default keyword. If a method is marked as default, it must provide a method body.
  3. A default method is not assumed to be static, final, or abstract, as it may be used or overridden by a class that implements the interface.
  4. Like all methods in an interface, a default method is assumed to be public and will not compile if marked private or protected.

Only declared in an interface.
Must be marked default and have a method body.
Not assumed static, final, or abstract.
May be overridden.
Assumed to be public. Will not compile if marked private or protected.

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

A default method is a method defined within an interface with …

A

the default keyword in which a method body is provided.

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

Classes have the option to override the default method if they need to, but…

A

they are not required to do so.

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

If the class doesn’t override the method…

A

the default implementation will be used.

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

Interface variables are assumed to be…

A

public, static, and final.

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

Marking any interface variable private or protected will …

A

trigger a compiler error as will marking any variable abstract.

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

The value of an interface variable must be set…

A

when it is declared since it is marked as final.

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

If two different interfaces have the same method (including signature and return type), you can define a class…

A

that fulfills both interfaces simultaneously.

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

If two abstract interface methods have the same method name but different input parameters, …

A

there is no conflict because this is considered a method overload.

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

If two different interfaces have the same methods with the same signatures but different return types, …

A

the class attempting to inherit both interfaces will not compile.

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

Rules for extending an interface

A
  1. An interface that extends another interface, as well as an abstract class that implements an interface, inherits all of the abstract methods as its own abstract methods.
  2. The first concrete class that implements an interface, or extends an abstract class that implements an interface, must provide an implementation for all of the inherited abstract methods.
interfaces inherit all abstract methods from parent
concrete class must implement all of an interface's abstract methods
17
Q

The keywords abstract and public…

A

are assumed for interfaces.

18
Q

A class may implement multiple interfaces, …

A

each separated by a comma.

19
Q

A concrete class is the first nonabstract subclass that extends an abstract class and …

A

is required to implement all inherited abstract methods.

20
Q

A concrete subclass is not required to provide an implementation for an abstract method if…

A

an intermediate abstract class provides the implementation.

21
Q

If a method in a child class has the same signature as a method in the parent class, but one of them is static and the other isn’t, …

A

the child method will not compile.

22
Q

You can override a private method in a parent class with a method in the child class,…

A

so long as it is private too.