

The following code created a method to use interface we parameter type. It calls the method on the object to which it is referring. When calling a method using a reference variable of an interface type, When invoking the walk() method on w2, it invokes the walk() method of the Dog object because w2 is referring to a Dog object. When invoking the walk() method on w1, it invokes the walk() method of the Person object because w1 is referring to a Person object. Since Walkable interface has only one member, which is the walk() method, Although a Java class can extend only a single superclass, it can implement any number of interfaces.

We can access any members of the interface using its reference type variable. Javas solution to this problem is called an interface. We can create an object only for a class type, but we can use an interface type variable can refer to any object whose class implements that interface.īecause the Person and Dog classes implement the Walkable interface,Ī reference variable of the Walkable type can refer to an object of these classes. You cannot create an object of an interface type since the interface is to define an abstract Walkable w // w is a reference variable of type Walkable Walkable), we define a new reference interface type. Like a class, an interface defines a new reference type. If a class implements the Walkable interface, it must provide implementation for the walk() method. ( "a dog is walking.") Ī class can implement one or more interfaces using the keyword implements in its declaration.īy implementing an interface, a class guarantees that it will provide an implementation for all methods declared in the interface or the class will declare itself abstract. Suppose Person class has a walk() method. In the following we will use an example to show why do we need interface. And the Dog class can implement the Walkable interface and makes the dog to walk in The Person class implements the Walkable interface and makes the person to walk inĪ human being way. We can have the Person class and Dog class to implement the Walkable concept and provide their own The byte code of an interface appears in a. Here we can create an interface called Walkable to represent the walk concept. java extension, with the name of the interface matching the name of the file. Interfaces define a relationship between unrelated classes through the abstract concept.įor example, we can create a Person class to represent a person and we canīoth person and dog can walk. Java 8 allows an interface to have static and default methods with implementation. Prior to Java 8, an interface could contain only abstract methods. The interface is implemented by classes to provide an implementation of the concept. In this step, I will create a BrassInstrument interface which extends from MusicalInstrument with an additional buzz(String song) method.īrassInstrument.java package Object Oriented Design - Java interfaceĪn interface in Java defines a reference type to create an abstract concept. With Default interface method solve this issue. Adding new methods will force all the implementation to * modifying an interface by adding new methods when the interface has more than * 1) add Default interface method to preserve backward compatibility when

In this step, I will create a MusicalInstrument interface which contains: In Java language, an interface is a type that defines the method signatures. In this step, I will created five interfaces based on the following diagram.įigure 1 Interfaces 4.1 MusicalInstrument Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. A class implements an interface, thereby inheriting the abstract methods of the interface. I will include both Junit and Logback in the pom.xml. An interface is a reference type in Java. The example code in this example was built and run with: Utilizing the interface design pattern in an application.Creating an implementation class from multiple interfaces.Creating an implementation class from a single interface.Defining an interface which extends from one or more interfaces.

Defining an interface which contains default, static, private, and abstract methods.
