Slide 1
Slide 2
Slide 3

Interfaces in java programming (V61)

No Comments

Interfaces

An interface is a collection of abstract methods. An interface is used to achieve abstraction, where you can define methods without providing their implementation (that is, without a method body). An interface is a reference type and is similar to a class.

Along with abstract methods, an interface can also contain constants, default methods, static methods, and nested types. Method bodies are provided only for default methods and static methods.

Writing an interface is similar to writing a class. However, a class describes the attributes and behaviors of an object. An interface defines behaviors that a class must implement, unless the class that implements the interface is abstract. All methods of an interface must be implemented in the class.

Similarities and Differences between Interfaces and Classes

Similarities

  • o An interface can contain a number of methods.
  • o An interface is written in a file with the .java extension, and the name of the interface must match the name of the file.
  • o The bytecode of an interface appears in a .class file.
  • o An interface appears in packages, and their corresponding bytecode files must be in a directory structure that matches the package name.

Differences

  • o You cannot instantiate an interface directly.
  • o An interface does not have any constructors.
  • o All methods in an interface are abstract.
  • o An interface cannot have instance fields. The only fields that can be declared in an interface must be both static and final.
  • o An interface is not extended by a class; it is implemented by a class.
  • o An interface can extend multiple interfaces.

Declaring an Interface

The interface keyword is used to declare an interface.

Syntax:


Properties of Java Interface

  • o An interface is implicitly abstract. You do not need to use the abstract keyword while declaring an interface.
  • o Each method in an interface is also implicitly abstract, so the abstract keyword is not required.
  • o Methods in an interface are implicitly public.

Implementing Interfaces

  • o When a class implements an interface, you can think of the class as signing a contract, agreeing to implement the specific behaviors of the interface.
  • o If a class does not implement all the behaviors of the interface, then the class must declare itself as abstract.

A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration after the extends part of the declaration.

Rules for defining overriding methods in Java Interfaces

  • o Checked exceptions should not be declared in the implementation methods other than those declared by the interface method or its subclasses.
  • o The signature of the interface method and the return type must be the same, or the return type can be a subtype, when overriding methods.
  • o The implementation class can be abstract, and if so, the interface methods do not need to be implemented.

Rules for implementing Java Interfaces

  • o A class can implement more than one interface at the same time.
  • o A class can extend only one class but can implement many interfaces.
  • o An interface can extend another interface in a similar way that a class can extend another class.

Extending Java Interfaces
An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.

Example to declare a Java Interface (Project Name: AnimalSound)

Source Code1: Declaring an Interface (Animal.java)

Copied!
public interface Animal {
    public void makeSound();
    public void eat();
}

Source Code2: Implementing an Interface (Dog.java)

Copied!
public class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Dog says: Woof!");
    }

    @Override
    public void eat() {
        System.out.println("Dog eats bones.");
    }
}

Source Code3: Implementing an Interface (Cat.java)

Copied!
public class Cat implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Cat says: Meow!");
    }

    @Override
    public void eat() {
        System.out.println("Cat eats fish.");
    }
}

Source Code4: Using the Interface (AnimalSound.java)

Copied!
public class AnimalSound {
    public static void main(String[] args) {
        Animal myDog = new Dog();
        Animal myCat = new Cat();

        myDog.makeSound();
        myDog.eat();

        myCat.makeSound();
        myCat.eat();
    }
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top