Interfaces and packages are essential components of Java that support abstraction, multiple inheritance, modularity, and access control. Interfaces define what a class must do, while packages help organize classes and interfaces into logical groups. Together, they make Java applications scalable, maintainable, and easy to manage, especially in large projects.
An interface in Java is a blueprint of a class that contains abstract methods (and from Java 8 onwards, default and static methods). It specifies what a class should do, but not how it should do it.
public and abstract by defaultpublic, static, and finalinterface InterfaceName {
void methodName();
}
Java does not support multiple inheritance using classes, but it does support multiple inheritance using interfaces. A class can implement multiple interfaces at the same time.
class ClassName implements Interface1, Interface2 {
// method implementations
}
interface Printer {
void print();
}
interface Scanner {
void scan();
}
class AllInOne implements Printer, Scanner {
public void print() {
System.out.println("Printing document");
}
public void scan() {
System.out.println("Scanning document");
}
}
The class AllInOne implements two interfaces, achieving multiple inheritance.
To add new methods to existing interfaces without breaking implementing classes.
A default method has a method body and is defined using the default keyword.
interface InterfaceName {
default void methodName() {
// implementation
}
}
interface Vehicle {
default void fuelType() {
System.out.println("Uses petrol or diesel");
}
}
class Car implements Vehicle {
}
Static methods belong to the interface itself and cannot be overridden.
interface InterfaceName {
static void methodName() {
// implementation
}
}
interface Calculator {
static int add(int a, int b) {
return a + b;
}
}
A functional interface is an interface that contains only one abstract method. It is widely used in lambda expressions and functional programming.
@FunctionalInterface
@FunctionalInterface
interface Greeting {
void sayHello();
}
public class Main {
public static void main(String[] args) {
Greeting g = () -> System.out.println("Hello, Java!");
g.sayHello();
}
}
A package is a namespace that organizes related classes and interfaces into a single unit. It helps avoid name conflicts and improves code readability.
java.lang, java.util, java.io)The import statement is used to access classes defined in another package.
import packageName.ClassName;
import packageName.*;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
}
}
Java access modifiers work together with packages to control visibility.
| Modifier | Same Class | Same Package | Subclass (Different Package) | Everywhere |
|---|---|---|---|---|
| public | ✔ | ✔ | ✔ | ✔ |
| protected | ✔ | ✔ | ✔ | ✖ |
| default | ✔ | ✔ | ✖ | ✖ |
| private | ✔ | ✖ | ✖ | ✖ |
package com.company.project;
public class Employee {
public void show() {
System.out.println("Employee details");
}
}
javac -d . Employee.java
java com.company.project.Employee
java
// Interface
interface Drawable {
void draw(); // Abstract method
// Default method
default void display() {
System.out.println("Displaying drawable object");
}
// Static method
static void info() {
System.out.println("This is a Drawable interface");
}
}
interface Colorable {
void setColor(String color);
}
// Multiple inheritance through interfaces
class Circle implements Drawable, Colorable {
private String color;
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
@Override
public void setColor(String color) {
this.color = color;
System.out.println("Circle color set to: " + color);
}
}
// Functional interface
@FunctionalInterface
interface Calculator {
int calculate(int a, int b);
}
public class InterfaceDemo {
public static void main(String[] args) {
Circle c = new Circle(5.0);
c.draw();
c.setColor("Red");
c.display();
Drawable.info();
// Lambda expression with functional interface
Calculator add = (a, b) -> a + b;
Calculator multiply = (a, b) -> a * b;
System.out.println("Sum: " + add.calculate(5, 3));
System.out.println("Product: " + multiply.calculate(5, 3));
}
}