Java Interfaces

An interface in Java is a blueprint of a class that contains abstract methods (methods without a body). Interfaces are used to achieve abstraction and multiple inheritance in Java. It defines a set of methods that a class must implement but does not provide the implementation.

Key Points About Interfaces:

  1. An interface can contain:
  • Abstract methods (methods without a body).
  • Default methods (methods with a body, introduced in Java 8).
  • Static methods (introduced in Java 8).
  • Private methods (introduced in Java 9)
  1. A class can implement multiple interfaces (unlike extending classes, which is limited to one).
  2. Interfaces cannot have instance variables; they can have constants (public, static, and final by default).
  3. All methods in an interface are implicitly public and abstract (except default, static, and private methods).


Syntax:

interface InterfaceName {
    // Abstract method
    void method1();

    // Default method (introduced in Java 8)
    default void method2() {
        System.out.println("Default Method");
    }

    // Static method (introduced in Java 8)
    static void method3() {
        System.out.println("Static Method");
    }
}
Example: Basic Interface
interface Animal {
void sound(); // Abstract method

default void sleep() { // Default method
System.out.println("Sleeping...");
}
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}

public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // Calls the overridden method
dog.sleep(); // Calls the default method
}
}

Try it yourself

Example: Multiple Interfaces
interface Printable {
void print();
}

interface Showable {
void show();
}
class Demo implements Printable, Showable {
public void print() {
System.out.println("Printing...");
}

public void show() {
System.out.println("Showing...");
}
}

public class Main {
public static void main(String[] args) {
Demo demo = new Demo();
demo.print();
demo.show();
}
}

Try it yourself

Example: Default Method
interface Vehicle {
default void start() {
System.out.println("Vehicle is starting");
}
}

class Car implements Vehicle {
public void start() {
System.out.println("Car is starting");
}
}

public class Main {
public static void main(String[] args) {
Vehicle car = new Car();
car.start(); // Calls the overridden method in Car
}
}

Try it yourself

Example: Static Method
interface MathUtils {
static int add(int a, int b) {
return a + b;
}
}

public class Main {
public static void main(String[] args) {
System.out.println(MathUtils.add(5, 10)); // Directly calls the static method
}
}

Try it yourself

Real-World Example: Interface for Multiple Objects
interface Shape {
double area();
double perimeter();
}

class Circle implements Shape {
double radius;

Circle(double radius) {
this.radius = radius;
}

public double area() {
return Math.PI * radius * radius;
}

public double perimeter() {
return 2 * Math.PI * radius;
}
}

class Rectangle implements Shape {
double length, width;

Rectangle(double length, double width) {
this.length = length;
this.width = width;
}

public double area() {
return length * width;
}

public double perimeter() {
return 2 * (length + width);
}
}

public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(10, 20);

System.out.println("Circle Area: " + circle.area());
System.out.println("Circle Perimeter: " + circle.perimeter());
System.out.println("Rectangle Area: " + rectangle.area());
System.out.println("Rectangle Perimeter: " + rectangle.perimeter());
}
}

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.