Interfaces in Java

Interfaces in java are same like an abstract class, it does not attribute definition and all of its methods are abstract. An interface don’t have constructors and can’t be instantiated. ¬†From Java 8 onward non abstract methods can be included in the interface by using default keyword. These methods are known as default or extension methods. ¬†Following are the most important points about java interfaces.

  • An interface is treated as a special class in java. Each interface is compiled into a separate byte code file.
  • An interface contains only constants and abstract methods
  • You can’t create instance from an interface using new operator. It means you can use interface more or less same way as an abstract class
  • Java interfaces share the common behavior among the instances of objects
  • A class can implement more then one interfaces
  • To model strong relationship always use classes
  • To model a weak type relationship use interface
  • You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. In the case of multiple inheritance, you have to design one as a superclass, and others as interface

Interface Syntax

modifier interface InterfaceName {
  /** Constant declarations */
  /** Method signatures */
}

Example 1

interface Int1 {
    public void method();
}

class Ex1 implements Int1 {
public void method(){

}
}

Example 2 : Implementing Eidble Interface and Extending Animal class

public interface Edible {
  //method describes how to eat
  public String howToEat();
}

class Animal {
}
class Chicken extends Animal implements Edible {
  int weight;

  public Chicken(int weight) {
    this.weight = weight;
  }

  public String howToEat() {
    return "Fry it";
  }
}

class Tiger extends Animal {
}

Example 3 : Extending Abstract class and Implementing Edible Interface

abstract class Fruit implements Edible {
}

class Apple extends Fruit {
  public String howToEat() {
    return "Make apple cider";
  }
}

class Orange extends Fruit {
  public String howToEat() {
    return "Make orange juice";
  }
}

Including Default Methods in Interface

package java8;

public interface MathEx {
    double sqroot(int a);
    double absolute(double a);

    default double sqrt(int a) {
        return Math.sqrt(a);
    }
    default double abs(double a){
    	return Math.abs(a);
    }
}

Besides the abstract methods sqroot and absolute, the interface MathEx also defines the default methods sqrt abd abs. Concrete classes only have to implement the abstract methods sqroot and absolute. The default methods sqrt and abs can be used out of the box.

public class Test {

	public static void main(String[] args) {
		MathEx formula = new MathEx() {
		    

			@Override
			public double sqroot(int a) {
				return sqrt(a * 100);			
			}

			@Override
			public double absolute(double a) {
				return abs(a*100);
			}
		};

		System.out.println(formula.sqroot(9));     
		System.out.println(formula.sqrt(9));
		
		System.out.println(formula.absolute(-1.2));     
		System.out.println(formula.abs(-1.2));
	}

}

No Responses