Interfaces in java with example

Interface concept included in java to overcome the Diamond problem of multiple inheritance in java. In other words, Interface was introduced in java to define objects outside the world through methods. For example, buttons on the front of the television set. That are the interfaces between you and the electrical set. You press the power button to turn the television to on or off.

An interface is a specification of method prototypes. Prototypes will be in the interface. All the methods of the interface are abstract. For the polymorphic interface inheritance, where the client wants to only deal with a type and does not care about the actual implementation use interfaces. If you need to change your design frequently, you should prefer using interface to abstract class.

Through interfaces we can achieve multiple inheritance in java. Because there is a possibility of inheriting more than one interface into java class by using the keyword called implements. When we try to inherit from one interface to another interface we must use extends keyword like classes.

In interfaces we have to define the method declaration only. It will not allow to implement methods inside the interfaces. The method implementation/declaration should be present in the class which has implements the interface. The class which contains method implementation called as subclass / child class of the interface.

Here is the example interface with the name called Vehicle with few of the unimplemented methods.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.javatbrains.interfaces;

public interface Vehicle {
    
     void changeGear(int newValue);

     void speedUp(int increment);

     void applyBrakes(int decrement);
}

To implement the interface methods we have to create a class which will implements the interface. To implement the interface, will use the implements keyword in java. The below is the Car class which will implements the Vehicle interface. The methods in the Car class will contains @Override annotation on top of the method. Those methods are called overridden methods. Along with overridden methods, subclass will contains it's own methods. Those methods are not declared with the @Override annotation.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.javatbrains.interfaces;

public class Car implements Vehicle {
     int speed = 0;
     int gear = 1;

     @Override
     public void changeGear(int newValue) {
          gear = newValue;
     }

     @Override
     public void speedUp(int increment) {
          speed = speed + increment;
     }

     @Override
     public void applyBrakes(int decrement) {
          speed = speed - decrement;
     }

     void printStates() {
        System.out.println(" speed: " +
            speed + " gear: " + gear);
   }

}

Implementing an interface allows a class to become more formal about the behaviour it promises to provide. When you are implementing an interface into a class that class should override the interface methods into implementing class. Otherwise compiler will complain.

Key points in interfaces:
  • An interface is a specification of method prototypes.
  • An interface contains 0 or more abstract methods.
  • All the methods of the interface are public and abstract by default.
  • An interface may have variables, which are public static final by default. In other words, interfaces can declare only constants, not instance variables.
  • We can not create an object for interfaces.
  • But we can create a reference of interface type.
  • All the abstract methods of the interface should be implemented in its implementation class.
  • I nterface reference can be used to refer to the objects of implementation classes.
  • If any method is not implemented in an implementation class, class should be declared as "abstract".
  • Once an interface is written, any third party vendor can implement it.
  • An interface cannot implement another interface.
  • An interface can extends another interface.
  • We can write a class inside an interface.
  • A class can implements multiple interfaces.
  • All the methods are incomplete only.
  • Interface methods must not be static. Because interface methods are abstract, they can not be marked final, strictfp, native.
  • Interface does not have constructors. Interfaces are not part of an object's inheritance tree.
Here is the another example of one interface is implementing more than one class.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.javatbrains.interfaces;

interface DbConnection {
     void connect(); // public abstract
}

class OracleConnection implements DbConnection {

     @Override
     public void connect() {
          System.out.println("Connecting to Oracle database");
     }

}

class SybaseConnection implements DbConnection {

     @Override
     public void connect() {
          System.out.println("Connecting to Sybase database");
     }

}

class MySQLConnection implements DbConnection {

     @Override
     public void connect() {
          System.out.println("Connecting to MySQL database");
     }

}

public class Database {

     public static void main(String[] args) throws Exception {
          // Accepts the database name and store it in object c
          Class c = Class.forName(args[0]);
          // Create an object to the class whose name is in c
          DbConnection connection = (DbConnection) c.newInstance();
          connection.connect();
     }

}

What is marker interface?
          Marker interface is an interface, which doesn't contains any of the methods. But, still that type of interfaces are doing their functionality. For example, Serializable, Cloneable..etc.

Do you Know?

Comments

Post a Comment

Popular posts from this blog

Hibernate auto increment with example

how to count the page views by using JSP

Multithreading in java with example

How to retrieve data from table by using JDBC with example

Prime, Fibonacci and Factorial number with example in java

How to insert images into database using JDBC?

How to sort list of objects in java with examples

String interview questions and answers

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Store file into table by using JDBC with example