Chapter 5 : Class Design
1. Class Inheritance
class access modifiers:
public and default package-level class access modifiers, because these are the only ones that can be applied to top-level classes within a Java file. The default package private modifier, which is the lack of any access modifier, indicates the class can be accessed only by a subclass or class within the same package.
protected and private modifiers can only be applied to inner classes, which are classes that are defined within other classes.
Constructor
super() command may only be used as the first statement of the constructor
Constructor Definition 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 usingsuper(). - 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-argumentsuper()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.
In Java, the parent constructor is always executed before the child constructor
Override
The compiler performs the following checks when you override a nonprivate 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.
- 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.
Redeclaring private Methods (method hidden):
Java permits you to redeclare a new method in the child class with the same or modified signature as the method in the parent class. This method in the child class is a separate and independent method, unrelated to the parent version’s method, so none of the rules for overriding methods are invoked.
Hiding Static Methods
- 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).
Overriding vs. Hiding Methods
Unlike overriding a method, in which a child method replaces the parent method in calls defined in both the parent and child, hidden methods only replace parent methods in the calls defined in the child class.
Hiding Variables
When you hide a variable, you define a variable with the same name as a variable in a parent class. This creates two copies of the variable within an instance of the child class: one instance defined for the parent reference and another defined for the child reference.
2. Abstract Class
An abstract class is a class that is marked with the abstract keyword and cannot be instantiated.
An abstract method is a method marked with the abstract keyword defined in an abstract class, for which no implementation is provided in the class in which it is declared
A concrete class is the first non-abstract subclass that extends an abstract class and is required to implement all inherited abstract methods.
if an intermediate class provides an implementation for an abstract method, that method is inherited by subclasses as a concrete method, not as an abstract one. In other words, the subclasses do not consider it an inherited abstract method because it is no longer abstract by the time it reaches the subclasses.
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 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.
Abstract Method Definition Rules
- 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.
3. Interface
Interface Definition 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 (assumed) 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.
Interface inheritance 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.
if the method name and input parameters are the same but the return types are different between the two methods, the class or interface attempting to inherit both interfaces will not compile.
Interface variables 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 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 as private or protected.
When an interface extends another interface that contains a default method,
- it may choose to ignore the default method, in which case the default implementation for the method will be used.
- the interface may override the definition of the default method using the standard rules for method overriding, such as not limiting the accessibility of the method and using covariant returns.
- the interface may redeclare the method as abstract, requiring classes that implement the new interface to explicitly provide a method body.
Analogous options apply for an abstract class that implements an interface.
If a class implements two interfaces that have default methods with the same name and signature, the compiler will throw an error. There is an exception to this rule, though: if the subclass overrides the duplicate default methods, the code will compile without issue—the ambiguity about which version of the method to call has been removed.
you can access an interface’s overridden method with the syntax:<Interface>.super.<method>();
Static interface method 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
the static interface methods are not inherited by a class implementing the interface. a class that implements two interfaces containing static methods with the same signature will still compile at runtime, because the static methods are not inherited by the subclass and must be accessed with a reference to the interface name.
4. Polymorphism
- a reference with the same type as the object,
- a reference that is a superclass of the object,
- a reference that defines an interface the object implements, either directly or through a superclass
Addition, a null value can always be passed as an object value, regardless of type.
Once the object has been assigned a new reference type, only the methods and variables available to that reference type are callable on the object without an explicit cast.
- 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 variables 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. May throw a ClassCastException at runtime.
A virtual method is a method in which the specific implementation is not determined until runtime. In fact, all non-final, non- static, and non-private Java methods are considered virtual methods, since any of them can be overridden at runtime.
polymorphic parameters

浙公网安备 33010602011771号