Understanding Java Interfaces

Textual Information

An interface in Java defines a contract for how classes interact. It contains public method signatures, requires implementing classes to provide method implementations, and can include default and static methods that are not inherited.


Click to learn more

Video Information

Code Snippets

    // interface example
    public interface Animal {
        void eat();  
        void sleep(); 
    }

    // class implementation example
    public class Dog implements Animal {
        public void eat() {
            System.out.println("The dog is eating.");
        }
        public void sleep() {
            System.out.println("The dog is sleeping.");
        }
    }
                        

Quiz

Test your knowledge on Java interfaces:

1. What is an interface in Java?

2. Which keyword is used to implement an interface in Java?

3. What must a class do when it implements an interface?