In engineering applications, distinguishing between object-specific behavior and global utility functions is vital for code organization. This project demonstrates the coexistence of Default Methods and Static Methods within a single interface. The printSquare method provides a default instance-level implementation, while printCube acts as a global static utility. This separation allows for flexible inheritance patterns while maintaining a centralized repository for common mathematical operations.
- Instance Logic: Implemented
printSquareas adefaultmethod accessible via object instances. - Global Utility: Developed
printCubeas astaticmethod accessible directly through theMathHelperinterface. - Implementation Mapping: Created the
NumberPrinterclass to bridge the interface logic with executable code.
- Java 8+ (Default Methods, Static Interface Methods)
- MathHelper: The interface container for both instance and static mathematical logic.
- NumberPrinter: A concrete class that inherits the default squaring behavior.
- MathLauncherApp: The entry point demonstrating both invocation styles.
25
27
Project Structure:
JavaBasics_Task_379/
├── src/
│ └── com/yurii/pavlenko/
│ ├── app/
│ │ └── MathLauncherApp.java
│ └── math/
│ ├── contracts/
│ │ └── MathHelper.java
│ └── modules/
│ └── NumberPrinter.java
├── LICENSE
├── TASK.md
├── THEORY.md
└── README.md
Code
package com.yurii.pavlenko.app;
import com.yurii.pavlenko.math.contracts.MathHelper;
import com.yurii.pavlenko.math.modules.NumberPrinter;
public class MathLauncherApp {
public static void main(String[] args) {
NumberPrinter printer = new NumberPrinter();
printer.printSquare(5);
MathHelper.printCube(3);
}
}package com.yurii.pavlenko.math.contracts;
public interface MathHelper {
default void printSquare(int n) {
System.out.println(n * n);
}
static void printCube(int n) {
System.out.println(n * n * n);
}
}package com.yurii.pavlenko.math.modules;
import com.yurii.pavlenko.math.contracts.MathHelper;
public class NumberPrinter implements MathHelper {
// Inherits printSquare by default
}This project is licensed under the MIT License.
Copyright (c) 2026 Yurii Pavlenko
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files...
License: MIT