Chapter 5 - Class Design Flashcards
Inheritance
(4)
- Inheritance is the process by which the new child subclass automatically includes any public or protected primitives, objects, or methods de ned in the parent class.
- Java supports single inheritance, by which a class may inherit from only one direct parent class.
- Java does allow one exception to the single inheritance rule: classes may implement multiple interfaces.
- It is possible in Java to prevent a class from being extended by marking the class with the final modi er.
Extending
(2)
public abstract **class** ElephantSeal **extends** Seal { // Methods and Variables defined here }
- must be public or default accessible
- can be abstract or final itself (but doesnt have to)
Class Modifiers
(4)
- public:
indicates that it can be referenced and used in any class - default package private access:
indicates the class can be accessed only by a subclass or class within the same package - protected:
can only be applied to inner classes - private:
can only be applied to inner classes
Java Object Inheritance
(2)
The key is that when Java sees you define a class that doesn’t extend another class, it immediately adds the syntax extends java.lang.Object to the class defnition.
If you define a new class that extends an existing class, Java doesn’t add this syntax, although the new class still inherits from java.lang.Object. Since all classes inherit from java.lang.Object, extending an existing class means the child automatically inherits from java.lang.Object by construction.
Constructor Rule
(5)
- 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() or this() 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.
Calling Inherited Class Members
(4)
- Java classes may use any public or protected member of the parent class, including meth- ods, primitives, or object references.
- If the parent class and child class are part of the same package, the child class may also use any default members de ned in the parent class.
- Finally, a child class may never access a private member of the parent class, at least not through any direct reference.
- In Java, you can explicitly reference a member of the parent class by using the super keyword
Overriding a Method
(5)
- 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. (private methods can be redeclared though)
- 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.
Hiding Methods
(5)
- 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).
Creating final methods
(1)
- final methods cannot be overridden or hidden
Hiding Variables
(4)
- you can’t override a variable; you can only hide it
- If you’re referencing the variable from within the parent class, the variable de ned in the parent class is used
- Alternatively, if you’re referencing the variable from within a child class, the variable de ned in the child class is used
- Likewise, you can refer- ence the parent value of the variable with an explicit use of the super keyword.
Abstract Class Definition Rules (5)
- 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 or final.
- An abstract class that extends another abstract class 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.
Concrete Classes
(2)
- A concrete class is the first nonabstract subclass that extends an abstract class and is required to implement all inherited abstract methods.
- a concrete subclass is not required to provide an implementation for an abstract method if an interme- diate abstract class provides the implementation
Abstract Method Definition Rules
(4)
- Abstract methods may only be defined in abstract classes.
- Abstract methods may not be declared private or final.
- Abstract methods must not provide a method body/implementation in the abstract class for which is it 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.
Object vs. Reference
(2)
- 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.
Casting Objects
(4)
- 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 if 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.