Chapter 5 Class Design Flashcards
Inheriting a class gives you access to … ?
Inheriting a class gives you access to all of the public and protected methods of the class, but special rules for constructors and overriding methods must be followed or the code will not compile.
Should we provide in the child’s constructors parent class constructor ?
If the parent class doesn’t include a no-argument constructor, an explicit call to a parent constructor must be provided in the child’s constructors.
What is variable hiding (inheritance aspect) ?
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 defi ned for the child reference.
What is the difference between overriding and hiding methods ?
Unlike overriding a method, in which a child method
replaces the parent method in calls defi ned in both the parent and child, hidden methods only replace parent methods in the calls defi ned in the child class.
What abstract class definition rules you know (instantiation, numbers of methods, modifiers, inheritance, implementation)?
- Abstract classes cannot be instantiated directly.
- Abstract classes may be defined with any number, including zero, of abstract and nonabstract 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.
What abstract method definition rules you know (scope of definition, modifiers, implementation,)?
- 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
What is interface ?
It may be helpful to think of an interface as a specialized kind of abstract class, since it shares many of the same properties and rules as an abstract class. The following is a list of rules for creating an interface, many of which you should recognize as adaptions of the rules for defining abstract classes:
- 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.
What is default interface methods (purpuse and rules) ?
A default method is a method defi ned within an interface with the default keyword in which a method body is provided. Contrast default methods with “regular” methods in an interface, which are assumed to be abstract and may not have a method body.
A default method within an interface defi nes an abstract method with a default implementation. In this manner, 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. In this manner, the method defi nition is concrete, not abstract.
The following are the default interface method rules you need to be familiar with:
- 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.
What is static interface methods ?
Java 8 also now includes support for static methods within interfaces. These methods are defined explicitly with the static keyword and function nearly identically to static methods defined in classes. In fact, there is really only one distinction between a static method in a class and an interface. A static method defined in an interface is not inherited in any classes that implement the interface.
Here are the static interface method rules you need to be familiar with:
- 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.
What is called virtual method ?
A virtual method is a method in which the specific implementation is not determined until runtime. In fact, all non-final, nonstatic, and non-private Java methods are considered virtual methods, since any of them can be overridden at runtime. What makes a virtual method special in Java is that if you call a method on an object that overrides a method, you get the overridden method, even if the call to the method is on a parent reference or within the parent class.
What modifiers are implicitly applied to all interface methods ?
All interface methods are only implicitly public. Since Java 8 now includes default and static methods and they are never abstract, you cannot assume the abstract modifier will be implicitly applied to all methods by the compiler.
Can we override nonprivate static method from parent class to non-static method in the child class ?
No. For nonprivate methods in the parent class, both methods must use static
(hide) or neither should use static (override).
When null can be passed as an object value ?
Null value can always be passed as an object value, regardless of type.