Interface in java

In previous post, we have seen abstract class in java which provides partial abstraction. Interface is one of the core part of java and is used to achieve full abstraction.
Interface is generally used to provide contract for class to implement. Interface do not have implementation of any method.A class implements an interface, thereby inheriting the abstract methods of the interface. So it is kind of signing a contract, you agree that if you implement this interface, then you have to use its methods.It is just a pattern, it can not do anything itself.

All the methods in the interface are by default public and abstract and all variables are public static final.

Syntax for interface in java:

For example:
Let’s say you have Room class as your pojo class. Now you want all your rooms to be decorable, so you can just implement decorable interface and implement decorate method.

Interface and inheritance:

You can create inheritance in terms of interface too. An interface can only extend(extends) another interface.

Important points about interface in java:

  • You need to use “Interface” keyword to declare an interface.
  • An interface can not have any method implementation.
  • Interface methods are by default public abstract.
  • Interface variables are by default public static final.
  • implements keyword is used by class to use interface.
  • One interface can not extend another class bit can extend another interface using extends keyword.
  • A class needs to override all methods of interface unless it is abstract class, otherwise, you will get compilation errors.
  • You need to design interface very carefully, as once you have used in business logic, you won’t be able to remove or add methods from interface.


Let’s create an interface called

Create class named "SimpleRoom"

Create class named "SpecialRoom"

Create main class named "RoomInterfaceMain"

When you run above program, you will get below output:

Showing simple room
Showing special Room

Java 8 interface:

There were major changes done JDK 8 in interface.You can read more about at Default method in java 8.

That’s all about interface in java.

Was this post helpful?

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Table of ContentsEarly Binding or Static BindingWhy are static final and private methods associated by Early Binding?Code Example:Analysis:Late Binding or Dynamic BindingCode Example:Output:Analysis:Differences between Static and Dynamic Binding Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. In simpler words, […]

  • 04 November

    Can we overload main method in java

    In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void main(String[] args) and execute that method. Overload main method in […]

  • 02 November

    Dynamic method dispatch in java

    In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by a reference, then which version of overridden method […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to create one, if you want to have copy […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.