CHP7 - Beyond Classes Flashcards
Interface Access Modifiers
Interfaces don’t support protected members, as a class cannot extend an interface.
They also do not support package access members, although more likely for syntax reasons and backward compatibility.
default method
interface with default keyword and includes a method body. It maybe optionally overridden by a class implementing the interface.
One use of default methods is for backward compatibility.
Default Interface method definition rules
✅ A default method may be declared only within an interface
✅ A default method must be marked with the default keyword and include method body
✅ A default method is implicitly public
✅ A default method cannot be marked abstract, final or static
✅ A default method may be overridden by a class that implements the interface.
✅ If class inherits two or more default methods with the same method signature, then the class must override the method
Static interface methods
✅ A static method must be marked with the static keyword and include a method body
✅ A static method without an access modifier is implicitly public.
✅ A static method is not inherited and cannot be accessed in a class implementing the interface without a reference to the interface name.
A class implements two interfaces A and B. Both interfaces have same default methods with the same signature.
A class must override the conflicting method if two interfaces have the same default method.
You can explicitly call a specific interface’s implementation using InterfaceName.super.method().
This ensures clear behavior and prevents ambiguity.
Private Interface Method Definition Rules
✅ A private interface method must be marked with the private modifier and include a method body.
✅ A private static interface method may be called by any method within interface definition.
✅ A private interface method may only be called by default and other private non-static methods within the interface definition.
Sealed class
Class that restricts which other classes may extend it
Specifying the subclass modifier
Every class that directly extends a sealed class must specify exactly one of the following three modifiers:
- final
- sealed
- non-sealed
Sealed interface
In sealed interfaces, permits list can apply to a class that implements the interface or an interface that extends the interface.
Interfaces that extend a sealed interface can only be marked sealed or non-sealed.
Sealed class rules
✅ Sealed classes are declared with the sealed and permits modifiers.
✅ Sealed classes must be declared in the same package or named module as their direct subclasses.
✅ Direct subclasses of sealed classes must be marked final, sealed and non-sealed. For interfaces that extend sealed interface, only sealed and non-sealed modifiers are permitted.
✅ The permits clause is optional if the sealed class and its subclasses are declared within the same file or the subclasses are nested within the sealed class.
✅ Interfaces can be sealed to limit the classes that implement them or the interfaces that extend them.
Compact constructors of Record
Special type of constructor used for records to process validation and transformations succinctly.
It takes no parameters and implicitly sets all fields.
public Crane { }
Compact constructor
While compact constructor can modify the constructor parameters, they cannot modify the fields of the record. FE, it does not compile:
public record Crane(int numberEggs) {
public Crane {
this.numberEggs = 10; // DOES NOT COMPILE
}
}
How to modify record?
To ‘modify’ record you have to make a new object and copy all of the data you want to preserve.
Inherit records
Just as interfaces are implicitly abstract, records are also implicitly final. The final modifier is optional but assumed.
Like enums, that means you can’t extend or inherit a record.
Pattern Matching with Records
✅ If any field declared in the record is included, then all fields must be included.
✅ The order of fields must be the same as in the record.
✅ The names of the fields do not have to match
✅ At compile time, the type of the field must be compatable with the type declared in the record.
✅ The pattern may not match at runtime if the record supports elements of various types.
Initializers in Record
Records do not support instance initializers. All initialization for the fields of a record must happen in a constructor.
They do support static initializers.
Nested classes in java
✅ Inner class: A non-static type defined at the member level of a class
✅ Static nested class: A static type defined at the member level of class
✅ Local class: A class defined within a method body
✅ Anonymnous class: A special case of a local class that does not have a name
Inner class properties
✅ Can be declared public, protected, package or private
✅ Can extend a class and impelement interfaces
✅ Can be marked abstract or final
✅ Can access members of the outer class, including private members
Static nested class
It is defined at the member level.
A static nested class can be instantiated without an instance of the enclosing class.
It is like top-level class except for the following:
✅ The nesting creates namespace because the enclosing class name must be used to refer to it.
✅ It can additionally be marked private or protected.
✅ The enclosing class can refer to the fields and methods of the static nested class.
Does not require an instance of the outer class to be created.
Cannot access non-static members of the outer class directly.
Nested Records
They are implicitly static. THis means it can be used without a reference to the outer class. It also means it cannot access member variables of the outer class.
Local classes
Local class is nested class defined within method.
Like local variables, a local class declaration does not exist until the method invoked, and it goes out of scope when the method returns.
They can be declared inside constructors and initializers.
Local class properties
✅ Do not have an access modifier
✅ Can be declared final or abstract
✅ Can include instance and static members
✅ Have access to all fields and methods of the enclosing class (when defined in an instance method)
✅ Can access final and effectively final local variables
Anonymous class
It is specialized form of a local class that doest not have a name. It is declared and instantiated all in one statement using the new keyword, a type name with parentheses, and a set of braces {}.
Anonymous classes must extend existing class or implement an existing interface.
Can anonymous class extend class or implement any number of interfaces?
NO - must have exactly one superclass or one interface.
Object vs Reference
- Type of the object determines which properties exist within the object in memory
- Type of the reference to the object determines which methods and variables are accessible to the Java Program.
Casting objects
✅ Casting a reference from a subtype to a subtype doesnt require an explicit cast
✅ Casting a reference from supertype to a subtype requires an explicit cast
✅ At runtime an invalid cast of reference to an incompatible type results in ClassCAstException being thrown
✅ The compiler disallows casts to unrelated types
Can Record override methods?
Records may override any accessor methods.
protected member on interface?
Interfaces dont have protected member