Java interface tutorial


The interface is a blueprint for classes, and it will guide developers to follow the method signatures contracts. So whatever the implementation, it will not expose the implementation to the out. So we can use this to achieve abstraction OOP concept.

    public interface Animal {
        String getAnimalType();
    }

Properties of an interface

  • We cannot create objects with interfaces

  • Interfaces cannot have concrete methods. All the methods should be abstract
        public interface Animal {
            String getAnimalType();
        }

  • Interfaces can only have public static final constants

    Since interfaces only allow public static final part when you declare variables, we don't need to specify public static final part when you declare variables.

        public interface Animal {
            String ANIMAL_CONSTANT = "Constant";
    
            final String FINAL_ANIMAL_CONSTANT = "FinalConstant";   //here Modifier 'final' is redundant for interface fields. No need to use it
            static String STATIC_ANIMAL_CONSTANT = "StaticConstant";   //here Modifier 'static' is redundant for interface fields. No need to use it
            public String PUBLIC_ANIMAL_CONSTANT = "PublicConstant";   //here Modifier 'public' is redundant for interface fields. No need to use it
            public static final String ALL_ANIMAL_CONSTANT = "AllConstant";   //here Modifier 'public', 'static', 'final ' all redundant for interface fields. No need to use it
    
            String getAnimalType();
        }

  • An interface can be implemented using implements keyword.
        public interface Animal {
            String getAnimalType();
        }
    
        public class HummingBird implements Animal {
            @Override
            public String getAnimalType() {
                return "Bird";
            }
        }

  • An interface can be expands into a another interface.
        public interface Animal {
            String getAnimalType();
        }
    
        public interface Carnivore extends Animal {
            boolean isCarnivore();
        }

  • We can have default and static methods in an interface. This is only available after java 8
        public interface Animal {
            default void printDefaultMethod() {
                System.out.println("This is default methid of a interface");
            }
    
            static void printStaticMethod() {
                System.out.println("This is static methid of a interface");
            }
    
            String getAnimalType();
        }

  • After java 9 it allows having private methods in an interface

Possible interview question about Java interface classes


  • Can we implement multiple interfaces into a concrete class
        public interface Animal {
            String getAnimalType();
        }
    
        public interface Bird {
            void fly();
        }
    
        public class HummingBird implements Animal, Bird {
    
            @Override
            public String getAnimalType() {
                return "HummingBird";
            }
    
            @Override
            public void fly() {
                System.out.println("HummingBird is flying");
            }
        }

Usages of java interface classes


  • We can use interface to achieve abstraction

    Since we can hide the implementation from the outside with interface, we can use it to achieve abstraction.


  • To define specifications and frameworks we can use java interfaces

    Since we can hide the implementation from the outside with interface, we can use it to achieve abstraction.

    Lots of specifications only have interfaces and abstract classes. For ecample J2EE specification and JDBC specification. Some other vendors will do the implementation for them.

    J2EE: Jboss, Weblogic, GlassFish etc.
    JDBC: Oracle, MySQL, Postgresql etc.


  • We can use interface to achieve best practices like
    Coding to interfaces

    Coding to interfaces is a technique developers use to achieve best practices, and with it, developers will write codes by following interfaces. This will help to achieve abstraction and loosely coupled modules and components in the program.


<< Java Abstract Classes      Java all inheritance >>