Home Interfaces in java

Interfaces in java

Interfaces is nothing but another way to implement the Abstractions.only the different is Abstractions we are using super class and subclass to inherit using extends keyword but interface we are using implements keyword thats all.

Create a interface Calculator.

Calculator.java

public interface Calculator {

    public int Total(int num1,int num2);
}

Create a Class CalculatorImpl.

CalculatorImpl.java

public class CalculatorImpl implements Calculator {
    @Override
    public int Total(int num1, int num2) {
        return num1 + num2;
    }
}

Main.java

public class Main {
    public static void main(String args[]) {
        Calculator calculator = new CalculatorImpl();
        int result = calculator.Total(10,30);
        System.out.println(result);
    }
}

 

 

 

Comments are closed.