Commit ac554a3
committed
feat: Demonstrate interface constants, abstract methods, and static methods with Animal, Dog, and Cat
WHAT the code does:
Defines an interface Animal:
- Declares a static constant MAX_AGE = 100.
- Declares abstract methods eat() and sleep().
- Provides a static method info() that prints a message.
Defines Dog and Cat classes implementing Animal:
- Each class provides its own implementations of eat() and sleep().
Defines Tetst class with main():
- Instantiates Dog and Cat, calls their eat() methods.
- Accesses the MAX_AGE constant through both Dog.MAX_AGE and Animal.MAX_AGE to show static constants are inherited but belong to the interface.
WHY this matters:
Illustrates core features of Java interfaces:
- Interfaces can define constants (implicitly public, static, and final).
- Interfaces can declare abstract methods that must be implemented by concrete classes.
- Since Java 8, interfaces can also define static (and default) methods.
- Demonstrates polymorphism: different classes (Dog, Cat) implement the same contract in different ways.
HOW it works:
1. Dog and Cat implement Animal’s required methods (eat, sleep).
2. In main(), Dog and Cat objects are created and their eat() methods are called.
3. MAX_AGE is accessed via both class and interface references, showing it is truly static.
4. The Animal.info() static method could also be called directly to print interface information.
Tips and gotchas:
- All interface variables are implicitly public, static, and final; declaring them that way explicitly is redundant.
- Static methods in interfaces belong to the interface itself; they cannot be overridden by implementing classes.
- Avoid using interfaces as a dumping ground for constants (the so-called “constant interface anti-pattern”); consider enums or dedicated constants classes instead.
- When accessing static fields, prefer using the interface name (Animal.MAX_AGE) for clarity instead of class references.
Use-cases / analogies:
- Animal behaviors (eat, sleep) map to real-world polymorphic actions: different animals behave differently but follow the same contract.
- Constants like MAX_AGE represent universal biological constraints (like a species’ lifespan).
- In real systems, interfaces define shared capabilities (e.g., Serializable, Comparable), while constants centralize values used across implementations.
Short key: java-interfaces constants abstract-methods static-methods polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 67b64b6 commit ac554a3
1 file changed
+8
-9
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | | - | |
| 4 | + | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
9 | | - | |
10 | 8 | | |
| 9 | + | |
11 | 10 | | |
12 | | - | |
13 | | - | |
14 | | - | |
| 11 | + | |
| 12 | + | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | | - | |
20 | | - | |
| 17 | + | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
| 21 | + | |
24 | 22 | | |
25 | 23 | | |
26 | 24 | | |
| |||
30 | 28 | | |
31 | 29 | | |
32 | 30 | | |
| 31 | + | |
33 | 32 | | |
34 | 33 | | |
35 | 34 | | |
| |||
46 | 45 | | |
47 | 46 | | |
48 | 47 | | |
49 | | - | |
| 48 | + | |
0 commit comments