Declaring interface
abstract interface CanBarrow{
public abstract float getSpeed(int age);
public static final int MAX=25;
}
All interfaces are implicitly abstract modifier at top level
all methods implicitly have public abstract modifiers
All variables are implicitly constants having public static final
Can interface extends multiple interfaces at the same time?
public interface HasBigEyes extends Nocturnal, CanFly{}
Yes
When a class implements 2 interfaces that have exact abstract method signatures?
Allowed
Implementing class would have to provide a compatible concrete method
If both abstract method declarations in interfaces has similar method signatures but incompatible then Java throws compiler error
Are these valid in interface ?
protected void canFly();
private int count=5;
No
Modifiers explicitly declared should not conflict with implicitly assumed modifiers
Special interface methods
default
private
static
private static
I think you can guess what they mean😀
Can you guess their implicitly added modifiers?
default - public
private - n/a
static - public
private static - n/a
interface default method rules
Note: these are instance related methods
Can not be abstract, final or static
If a class inherits two or more default methods with same signature then class must override the method
How to call interface default method when concrete class overrides it
<interfacename>.super.<method>
Example:
Walk.super.getSpeed();
This is where default method exhibits static properties but it’s not quite the same. We use interface name first followed by super operator to indicate we are not using class inheritance but using instance inheritance
</method></interfacename>
interface static method rules other than obvious rules
1) can not be abstract or final
2) not inherited into implementing class. You have to use interface name to access it
Note: if not coded implicit access modifier is public. It can have explicit modifier private
interface private method
This is the only method that looks exactly like a regular concrete method in an interface.
This is used by other private methods only
interface private static method
Like its name this is a static method available to be used only with in interfaces. Typically used by other private or private static method
enum built in methods?
values() - gives an array of all enums constants
ordinal() - integer of index of the constant
valueOf(String)- takes string and returns enum value
Can enum implement an interface?
Can enum extend another class?
Yes
No
It can but it doesn’t extend a class
Enum constructor access modifiers
private (implicit)
This is only valid when enum is initialized by JVM. We don’t call enum constructor to create an enum constant. We only use it.
Enum initializing
First time an enum is used JVM calls all constructors in enum and create enum values all together before it’s used in program
How do you call enum instance method
Seasons.SUMMER.printVisitors();
Or
Season s=Season.SUMMER;
s.printVisitors();
The instance methods declared should be public
Can we create enum methods different for each constant
Yes
public enum SeasonWithTimes{
WINTER {
public String getHours() { return 10am-3pm; }
},
SUMMER{
public String getHours() { return 9-7pm; }
};
public abstract String getHours();
}
This enum looks like an abstract class . This enum has an abstract method. So each enum value must implement the method. Else Java issues compiler error
Important note: instead of declaring an abstract method we can provide a common method implementation and enum value can choose to override like in above example
What can an enum has?
Implement an interface
Have constructors (private)
Have final variable (private)
Have final static variables (private)
Have enum methods (public)
Sealed class syntax
public sealed class Bear permits Kodiak, Panda {}
sealed keyword with permits keyword defines wha subclasses are allowed to extend the current class
non-sealed keyword
Indicates a class that extends sealed classes, can be extended by unspecified classes
If you don’t want the class to be extended by any other classes, just use fina keyword
If you want a specific class to extend this subclass use sealed keyword agian on this subclass with permits keyword indicating next level subclasses . Chaining
Does sealed and subclasses need to be in same package?
Yes
Subclass that extends sealed classes to have certain modifiers?
Yes
Should be one of these 3
sealed - indicating another set of subclasses. Gaining
non-sealed - meaning any class can extend it further
final - this subclass can’t be extended anymore
An example of sealed subclass extending sealed classes
public sealed class ClownFish extends Fish permits OrangeClownFish{}
Is permits keyword required on a sealed class definition
No
Not required as long as both sealed and subclasses are declared with in single .java file
permits keyword can be omitted if the subclasses are nested
public sealed class Snake{
final class Cobra extends Snake {}
}
See the attached image
Nested subclass extending a sealed classes. What if we want to use permits keyword even though it’s not required
Use namespace qualifiers
Since subclass requires a reference to the sealed classes namespace