CHP7 - Beyond Classes Flashcards

1
Q

Interface Access Modifiers

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

default method

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Default Interface method definition rules

A

✅ 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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Static interface methods

A

✅ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

A class implements two interfaces A and B. Both interfaces have same default methods with the same signature.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Private Interface Method Definition Rules

A

✅ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Sealed class

A

Class that restricts which other classes may extend it

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Specifying the subclass modifier

A

Every class that directly extends a sealed class must specify exactly one of the following three modifiers:

  • final
  • sealed
  • non-sealed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Sealed interface

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Sealed class rules

A

✅ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Compact constructors of Record

A

Special type of constructor used for records to process validation and transformations succinctly.
It takes no parameters and implicitly sets all fields.
public Crane { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Compact constructor

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to modify record?

A

To ‘modify’ record you have to make a new object and copy all of the data you want to preserve.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Inherit records

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Pattern Matching with Records

A

✅ 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Initializers in Record

A

Records do not support instance initializers. All initialization for the fields of a record must happen in a constructor.
They do support static initializers.

17
Q

Nested classes in java

A

✅ 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

18
Q

Inner class properties

A

✅ 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

19
Q

Static nested class

A

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.

20
Q

Nested Records

A

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.

21
Q

Local classes

A

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.

22
Q

Local class properties

A

✅ 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

23
Q

Anonymous class

A

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.

24
Q

Can anonymous class extend class or implement any number of interfaces?

A

NO - must have exactly one superclass or one interface.

25
Q

Object vs Reference

A
  1. Type of the object determines which properties exist within the object in memory
  2. Type of the reference to the object determines which methods and variables are accessible to the Java Program.
26
Q

Casting objects

A

✅ 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

27
Q

Can Record override methods?

A

Records may override any accessor methods.

28
Q

protected member on interface?

A

Interfaces dont have protected member