Skip to content

Commit ac554a3

Browse files
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

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

Section14Interfaces/src/Animal.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
//Abstract Method
22
//Static Constant
33

4-
interface Animal
5-
{
4+
interface Animal {
65
public static int MAX_AGE = 100;
76

87
public abstract void eat();
9-
void sleep();
108

9+
void sleep();
1110

12-
//static methods
13-
public static void info()
14-
{
11+
//static methods.
12+
public static void info() {
1513
System.out.println("THis is an animal interface");
1614
}
1715
}
1816

19-
class Dog implements Animal
20-
{
17+
class Dog implements Animal {
2118
public void eat(){
2219
System.out.println("Dog is eating");
2320
}
21+
2422
public void sleep(){
2523
System.out.println("nothing just sleeping");
2624
}
@@ -30,6 +28,7 @@ class Cat implements Animal{
3028
public void eat(){
3129
System.out.println("Cat is eating");
3230
}
31+
3332
public void sleep(){
3433
System.out.println("just sleeping");
3534
}
@@ -46,4 +45,4 @@ public static void main(String[] args) {
4645
System.out.println((Dog.MAX_AGE)); //You can access from Animal also.
4746
System.out.println(Animal.MAX_AGE);
4847
}
49-
}
48+
}

0 commit comments

Comments
 (0)