Chapter 5 Flashcards
Method Hiding (5 rules)
- 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. - If the method returns a value, it must be the same or a subclass of the method in the
parent class, known as covariant return types. - The method defined in the child class must be marked as static if it is marked as
static in the parent class (method hiding). Likewise, the method must not be marked
as static in the child class if it is not marked as static in the parent class (method
overriding).
Constructor definition (5 rules)
- The first statement of every constructor is a call to another constructor within the class
using this(), or a call to a constructor in the direct parent class using super(). - The super() call may not be used after the first statement of the constructor.
- If no super() call is declared in a constructor, Java will insert a no-argument super()
as the first statement of the constructor. - If the parent doesn’t have a no-argument constructor and the child doesn’t define any
constructors, the compiler will throw an error and try to insert a default no-argument
constructor into the child class. - If the parent doesn’t have a no-argument constructor, the compiler requires an explicit
call to a parent constructor in each child constructor.
super() vs super
super(), is a statement that explicitly calls a parent constructor and may only be used in
the first line of a constructor of a child class.
The second, super, is a keyword used to reference a member defined in a parent class and may be used throughout the child class
OCA will try to trick you. Look at how super() and super are used:
public Rabbit(int age) {
super();
super.setAge(10);
}
public Rabbit(int age) {
super; // DOES NOT COMPILE
super().setAge(10); // DOES NOT COMPILE
}
Method Overriding (4 rules)
- 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. - If the method returns a value, it must be the same or a subclass of the method in the
parent class, known as covariant return types.
final methods
final methods cannot be overridden.
Defining an interface (5 rules)
- Interfaces cannot be instantiated directly.
- An interface is not required to have any methods.
- An interface may not be marked as final.
- All top-level interfaces are assumed to have public or default access, and they must
include the abstract modifier in their definition. Therefore, marking an interface as
private, protected, or final will trigger a compiler error, since this is incompatible
with these assumptions. - All nondefault methods in an interface are assumed to have the modifiers abstract
and public in their definition. Therefore, marking a method as private, protected,
or final will trigger compiler errors as these are incompatible with the abstract and
public keywords.
Inheriting an Interface (2 rules)
- 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. - 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.
Interface Variables (2 rules)
- Interface variables are assumed to be public, static, and final. Therefore, marking
a variable as private or protected will trigger a compiler error, as will marking any
variable as abstract. - The value of an interface variable must be set when it is declared since it is marked as
final.
Default Interface Methods (4 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 as private or protected.
Static Interface Methods (2 rules)
- Like all methods in an interface, a static method is assumed to be public and will not
compile if marked as private or protected. - To reference the static method, a reference to the name of the interface must be used.
Polymorphism
The property of an object to take on many different forms.
A Java object may be accessed using a reference with the same type
as the object, a reference that is a superclass of the object, or a reference that defi nes an
interface the object implements, either directly or through a superclass. Furthermore, a cast
is not required if the object is being reassigned to a super type or interface of the object.
Casting Variables (4 rules)
- Casting an object from a subclass to a superclass doesn’t require an explicit cast.
- Casting an object from a superclass to a subclass requires an explicit cast.
- The compiler will not allow casts to unrelated types.
- Even when the code compiles without issue, an exception may be thrown at runtime if
the object being cast is not actually an instance of that class.
Objects Vs. Reference (2 rules)
- The type of the object determines which properties exist within the object in memory.
- The type of the reference to the object determines which methods and variables are
accessible to the Java program.
In Java, all objects are accessed by reference, so as a developer you never have direct access
to the object itself. Conceptually, though, you should consider the object as the entity that
exists in memory, allocated by the Java runtime environment. Regardless of the type of the
reference you have for the object in memory, the object itself doesn’t change.
Concrete class
A concrete class is the first nonabstract subclass that extends an abstract class and is required to implement all inherited abstract methods.
When you see a concrete class extending an abstract class on the exam, check that it implements all of the required abstract methods.