Interface
Interface is an abstraction of behavior.
- abstract method - Before Java 8, interfaces could only have abstract methods
- default method - Provides method body, available since Java 8
- static method - Provides method body, available since Java 8, can be called via Interface.method()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| public interface MyInterface {
// Abstract method
void abstractMethod();
// Default method
default void defaultMethod() {
System.out.println("This is the interface's default method");
privateHelperMethod(); // Call private helper method (Note: private methods are Java 9 feature, shown here for completeness)
}
// Static method
static void staticMethod() {
System.out.println("This is the interface's static method");
}
// Private helper method (Java 9 feature, included for completeness)
private void privateHelperMethod() {
System.out.println("This is the interface's private helper method");
}
}
public class MyClass implements MyInterface {
@Override
public void abstractMethod() {
System.out.println("Implementing the interface's abstract method");
}
// Can choose to override default method
@Override
public void defaultMethod() {
System.out.println("Overriding the interface's default method");
MyInterface.super.defaultMethod(); // Call the interface's default method
}
}
public class Main {
public static void main(String[] args) {
MyClass myClass = new MyClass();
// Call implemented abstract method
myClass.abstractMethod();
// Call default method
myClass.defaultMethod(); // Note this calls the overridden version
// Call interface's static method
MyInterface.staticMethod();
}
}
|
Abstract Class
Abstract classes usually serve as base classes, like Person, Animal. Because different subclasses may have different method bodies for certain methods, these methods are placed as abstract methods in the abstract class.
Abstract classes can extend another class and can also implement multiple interfaces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| public interface Sing {
public void sing();
}
public interface Dance {
public void dance();
}
public abstract class Animal implements Dance, Sing{
@Override
public void dance(){
System.out.println("Animal dance");
}
@Override
public void sing(){
System.out.println("Animal sing");
}
abstract void eat();
}
public class Cat extends Animal{
@Override
public void eat() {
System.out.println("cat eat");
}
}
public class Dog extends Animal{
@Override
public void eat() {
System.out.println("Dog eats");
}
}
|
- Sing and Dance both represent capabilities, so they are implemented as Interfaces by the Animal abstract class.
- Animal class provides abstract method eat()
- Later, Cat and Dog classes provide different method bodies for the eat() method.
Autoboxing & Unboxing
- Autoboxing:
Automatically converts primitive types to corresponding wrapper class objects, actually calling Integer.valueOf(), Double.valueOf(), etc.
1
2
| int num = 10;
Integer boxedNum = num; // Autoboxing (int → Integer)
|
Unboxing: Automatically converts wrapper class objects to primitive types. Actually calls intValue(), doubleValue(), etc.
Performance issues: Autoboxing/unboxing creates temporary objects, frequent use in loops may cause performance degradation.
When comparing wrapper class objects, == compares memory addresses, not values!
1
2
3
4
5
6
7
| Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true (JVM caches small integer objects)
Integer c = 128;
Integer d = 128;
System.out.println(c == d); // false (exceeds cache range, creates new objects)
|