Packages and Interfaces  «Prev  Next»

Lesson 6Inside interfaces
ObjectiveCreate Interfaces to define General Characteristics

Create Interfaces to define General State and Behavior

Interfaces are Java constructs similar to classes that do not contain any functioning code.Interfaces are used to define the general design for a class without getting into the details of an implementation. The idea behind interfaces is that you can use them to define the general make-up of a type of class and then apply it to create the classes themselves. Following is an example of a simple interface:

interface Fish {
  public void swim();
  public void eat();
}

Interface Method Declarations

Notice that the methods defined in the Fish interface are simply declarations and contain no actual code.
To create a class based on an interface, you must use the implements keyword, like this:

class Shark implements Fish {
  boolean isHungry;

  public void swim() {
    isHungry = true;
  }

  public void eat() {
    isHungry = false;
  }
}

Since the Shark class implements the Fish interface, it must provide implementations of the swim() and eat() methods.
You could also create other types of fish based on the Fish interface, in which case they would provide suitable implementations of the swim() and eat() methods.
The benefit of implementing an interface in this situation is that all fish classes would have commonly supported methods, as defined by the Fish interface. Although interfaces primarily consist of method declarations, it is possible to declare member variables in them as well. However, any member variable declared in an interface is implicitly made static and final.
The following page discusses a class that implements two interfaces where the variables and methods of the interface are the same.


Interface Ambiguous Fields

What will be the output of compiling and running the following program:

interface I1{
 int VALUE = 1;
 void m1();
}
interface I2{
 int VALUE = 2;
 void m1();
}
class TestClass implements I1, I2{
 public static void main(String[] args){
  I1 i1 = new TestClass();
  i1.m1();
 }
 public void m1() { 
  System.out.println("Hello"); 
  System.out.println(I1.VALUE);
  System.out.println(I2.VALUE);	  
 }		
}

The output for the above program is
Hello
1
2

Ambiguous Interface Fields

Having ambiguous fields does not cause any problems but referring to such fields in an ambiguous way will cause a compile time error. So you cannot execute the following statement in your code:
System.out.println(VALUE); 
//The field VALUE is ambiguous

because it is ambiguous.
You can refer to a static variable of an interface by associating the static variable with the interface:
For example, I3.VALUE and I4.VALUE as in the example below.


interface I3{
 int VALUE = 5;
 void m1();
}
interface I4{
 int VALUE = 7;
 void m1();
}
public class AlteredClass implements I3, I4{
 public static void main(String[] args) {
  AlteredClass ac1 = new AlteredClass();
  ac1.m1();
  System.out.println("I3.VALUE= " + I3.VALUE);
  System.out.println("I4.VALUE= " + I4.VALUE);
 }
 public void m1() { 
  System.out.println("Within method m1()"); 
 }	
}

//Program output 
Within method m1()
I3.VALUE= 5
I4.VALUE= 7

Ambiguous Interface Fields with Exceptions

What will be the output of compiling and running the following program:

package com.java.inheritance;
/*  
date_created: February 6, 2015	/	
date_updated: September 24, 2022
[01 - Java Class Design ] (Overriding/ Overloading) 
 */
import java.io.IOException;
import java.sql.SQLException;

interface I1 { void m1(String str1) throws IOException;}
interface I2 { void m1(String str2) throws SQLException; }

public class TestClass implements I1, I2 {
 public static void main(String args[]) throws Exception {
  TestClass tc = new TestClass();
   I1 i1 = (I1) tc; // This is valid.
   i1.m1("i1 reference variable");
   I2 i2 = (I2) tc; // This is valid too.
   i2.m1("i2 reference variable");
 }
 public void m1(String s1) {
  System.out.println("In m1(): s1= " + s1);
 }
}

/* 
Program Output 
In m1(): s1= i1 reference variable
In m1(): s1= i2 reference variable
*/

Ambiguous Interface Fields

Having ambiguous fields does not cause any problems but referring to such fields in an ambiguous way will cause a compile time error.


Java Reference

Rules for Interfaces in Java

  1. Every interface is implicitly abstract. This modifier is obsolete for interfaces and should not be used in new Java programs.
  2. An interface can extend 1 or more interfaces.
  3. Every field declaration in the body of an interface is implicitly 1) public, 2) static and 3) final. It is permitted, but strongly discouraged as a matter of style, to redundantly specify any or all of these modifiers for such fields.
    A constant declaration in an interface must not include any of the modifiers synchronized, transient or volatile, or a compile-time error occurs.
  4. It is possible for an interface to inherit more than one field with the same name. Such a situation does not cause a compile-time error. However, any attempt within the body of the interface to refer to either field by its simple name will result in a compile-time error, because such a reference is ambiguous.
  5. Every method declaration in the body of an interface is implicitly public and abstract, so its body is always represented by a semicolon, not a block.
  6. A method in an interface cannot be declared static, because in Java static methods cannot be abstract.
  7. A method in an interface cannot be declared native or synchronized, or a compile-time error occurs. These keywords describe implementation properties rather than interface properties. However, a method declared in an interface may be implemented by a method that is declared native or synchronized in a class that implements the interface.

Java Interfaces - Exercise

Click the Exercise link below to create an interface.
Java Interfaces - Exercise

SEMrush Software