Misc Flashcards
Rules for overriding a method
- The method in the child class must have the same signature as the method in the parent class.
- The method in the child class must be at least as accessible or more accessible than the method in the parent class.
- 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.
- 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)
Rules for hiding a method
- The method in the child class must have the same signature as the method in the parent class.
- The method in the child class must be at least as accessible or more than the method in the parent class.
- 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.
- If the method returns a value, it must be the same or a subclass of the method in the parent class, aka covariant.
- 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
Abstract Class Definition Rules
- Abstract classes cannot be instantiated directly.
- Abstract classes may be defined with any number, including zero, of abstract and non abstract methods.
- Abstract classes may not be marked as private, protected, or final.
- An abstract class that extends another abstract inherits all of its abstract methods as its own abstract methods.
- 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
Abstract Method Definition Rules
- Abstract methods may only be defined in abstract classes.
- Abstract methods may not be delcared private or final.
- Abstract methods must not provide a method body/implementation in the abstract class for which it is declared.
- 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
Rules for defining an interface
- Interfaces cannot be instantiated directly.
- An interface is not required to have any methods.
- An interface may not be marked final.
- 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.
- 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
Default interface method rules
- A default method may only be declared within an interface and not within a class or abstract class.
- A default method must be marked with the default keyword. If a method is marked as default, it must provide a method body.
- 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.
- 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.
A default method is a method defined within an interface with …
the default keyword in which a method body is provided.
Classes have the option to override the default method if they need to, but…
they are not required to do so.
If the class doesn’t override the method…
the default implementation will be used.
Interface variables are assumed to be…
public, static, and final.
Marking any interface variable private or protected will …
trigger a compiler error as will marking any variable abstract.
The value of an interface variable must be set…
when it is declared since it is marked as final.
If two different interfaces have the same method (including signature and return type), you can define a class…
that fulfills both interfaces simultaneously.
If two abstract interface methods have the same method name but different input parameters, …
there is no conflict because this is considered a method overload.
If two different interfaces have the same methods with the same signatures but different return types, …
the class attempting to inherit both interfaces will not compile.