Java: Abstract Classes, Interfaces, and Related Concepts
=========================================================
1. Abstract Class Basics
------------------------
- An abstract class cannot be instantiated directly.
- It can contain:
- Abstract methods (without body)
- Concrete methods (with body)
- Constructors
- Instance variables and static variables
- Used when:
- You want to share common code among subclasses.
- The class has state (fields) to be inherited.
- You need non-static or non-public methods.
Example:
abstract class Animal {
String name;
Animal(String name) {
this.name = name;
}
abstract void sound();
void eat() {
System.out.println("Eating...");
}
}
2. Constructors in Abstract Classes
-----------------------------------
- Abstract classes can have constructors.
- These constructors are called when subclass objects are created.
Example:
abstract class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
System.out.println("Dog constructor");
}
}
3. Anonymous Inner Classes
--------------------------
- Anonymous class: unnamed class created for one-time use.
- Commonly used to extend abstract classes or implement interfaces on the spot.
Example:
Animal a = new Animal() {
void sound() {
System.out.println("Bark from anonymous class");
}
};
4. Interface vs Abstract Class
------------------------------
| Feature | Abstract Class | Interface
|
|--------------------------|-------------------------------------|-----------------
--------------------|
| Method types | Abstract + Concrete | Abstract,
default, static (Java 8+) |
| Constructors | Allowed | Not allowed
|
| Fields | Instance, static, final | public static
final (constants) |
| Access modifiers | Any | Only public
(methods are public) |
| Inheritance | Single | Multiple
|
| Use case | Code reuse, base class | Capability
declaration |
5. Java 8+ Interface Enhancements
---------------------------------
- Interfaces can have:
- `default` methods (with body)
- `static` methods
- `private` methods (Java 9+)
- Purpose: backward compatibility without breaking existing implementations.
6. Default Method vs Abstract Class Method
------------------------------------------
| Aspect | Default Method (Interface) | Method in
Abstract Class |
|----------------------------|----------------------------------|------------------
-------------------|
| Can access instance fields | No | Yes
|
| Constructors | No | Yes
|
| Access modifiers | Only public | public,
protected, private allowed |
7. Fields in Interface
----------------------
- Allowed but treated as:
- public
- static
- final
Example:
interface Config {
int TIMEOUT = 5000; // implicit public static final
}
8. Multiple Inheritance with Interface
--------------------------------------
class MyClass implements InterfaceA, InterfaceB {
// Implement all methods
}
9. Method Conflict Resolution
-----------------------------
interface A { default void show() { } }
interface B { default void show() { } }
class C implements A, B {
public void show() {
A.super.show(); // or B.super.show()
}
}