Log In

Don't have an account? Sign up now

Lost Password?

Sign Up

Prev Next

Interfaces and Packages

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.


1. Interfaces and Multiple Inheritance

What is an Interface?

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.

Key Characteristics

  • Supports 100% abstraction (before Java 8)
  • Methods are public and abstract by default
  • Variables are public, static, and final
  • Cannot create objects of an interface

Syntax

interface InterfaceName {
    void methodName();
}

Multiple Inheritance Using Interfaces

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.

Syntax

class ClassName implements Interface1, Interface2 {
    // method implementations
}

Example

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");
    }
}

Explanation

The class AllInOne implements two interfaces, achieving multiple inheritance.


2. Default and Static Methods in Interfaces

Why Introduced in Java 8?

To add new methods to existing interfaces without breaking implementing classes.


Default Methods

A default method has a method body and is defined using the default keyword.

Syntax

interface InterfaceName {
    default void methodName() {
        // implementation
    }
}

Example

interface Vehicle {
    default void fuelType() {
        System.out.println("Uses petrol or diesel");
    }
}

class Car implements Vehicle {
}

Static Methods in Interfaces

Static methods belong to the interface itself and cannot be overridden.

Syntax

interface InterfaceName {
    static void methodName() {
        // implementation
    }
}

Example

interface Calculator {
    static int add(int a, int b) {
        return a + b;
    }
}

3. Functional Interfaces

What is a Functional Interface?

A functional interface is an interface that contains only one abstract method. It is widely used in lambda expressions and functional programming.

Annotation

@FunctionalInterface

Key Points

  • Only one abstract method
  • Can have multiple default or static methods
  • Used with lambda expressions

Example

@FunctionalInterface
interface Greeting {
    void sayHello();
}

public class Main {
    public static void main(String[] args) {
        Greeting g = () -> System.out.println("Hello, Java!");
        g.sayHello();
    }
}

4. Packages in Java

What is a Package?

A package is a namespace that organizes related classes and interfaces into a single unit. It helps avoid name conflicts and improves code readability.

Types of Packages

  • Built-in Packages (java.lang, java.util, java.io)
  • User-defined Packages

5. import Statement

Description

The import statement is used to access classes defined in another package.

Syntax

import packageName.ClassName;
import packageName.*;

Example

import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
    }
}

6. Access Control with Packages

Java access modifiers work together with packages to control visibility.

ModifierSame ClassSame PackageSubclass (Different Package)Everywhere
public
protected
default
private

7. Creating Custom Packages

Using package Keyword

package com.company.project;

public class Employee {
    public void show() {
        System.out.println("Employee details");
    }
}

Compile and Run

javac -d . Employee.java
java com.company.project.Employee

Using IDE

  • Create package
  • Add class inside it
  • IDE manages structure automatically

Example:

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));
    }
}

Leave a Comment

    🚀 Join Common Jobs Pro — Referrals & Profile Visibility Join Now ×
    🔥