Interface
Interface
interface is like a class. The interface keyword is used to declare an interface.It contains only
constants and abstract methods. If you don’t use final keyword default all variables are final and
all methods are abstract. Interfaces specify what a class must do and not how. It is the blueprint
of the class. A class uses the implements keyword to implement an interface. The implements
keyword appears in the class declaration following the extends portion of the declaration.
Syntax
interface interfacename{
final variables;
abstract methods;
}
class classname implements interfacename{
abstract methods(){
-----
}
Other methods
}
Why use Java interface?
There are mainly three reasons to use interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It can be used to achieve loose coupling
Example
interface Vehicle {
// all are the abstract methods.
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}
class Bicycle implements Vehicle{
int speed;
int gear;
// to change gear
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed + " gear: " + gear);
}
}
class Bike implements Vehicle {
int speed;
int gear;
// to change gear
public void changeGear(int newGear){
gear = newGear;
}
// to increase speed
public void speedUp(int increment){
speed = speed + increment;
}
// to decrease speed
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed + " gear: " + gear);
}
}
public class GFG {
public static void main (String[] args) {
Bicycle bicycle = new Bicycle();
bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);
System.out.println("Bicycle present state :");
bicycle.printStates();
// creating instance of the bike.
Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);
System.out.println("Bike present state :");
bike.printStates();
}
}
Output
Bicycle present state :
speed: 2 gear: 2
Bike present state :
speed: 1 gear: 1
Important points
All interface Methods are implicitly public and abstract. Even if you use keyword it will
not create the problem as you can see in the second Method declaration.
Interfaces can declare only Constant. Instance variables are not allowed. This means all
variables inside the Interface must be public, static, final. Variables inside Interface
are implicitly public static final.
Interface Methods cannot be static.
Interface Methods cannot be final.
The Interface can extend one or more other Interface. Note: The Interface can only
extend another interface.
Example : interface twowheeler extends Vehicle{
void milage();
}
Multiple Inheritance in java
Multiple inheritance in achieved in java when one of the parent class is made an interface and
another class a normal class.
Example: we have an interface called Exam and a class called student. We have a third class
Result which implements the properties of both the Exam & student class as shown in the
diagram below. Write a code in java to implement the same.
interface Exam {
void Percent_cal();
}
class Student {
String name;
int roll_no, Marks1, Marks2;
Student(String n, int rn, int m1, int m2) {
name = n;
roll_no = rn;
Marks1 = m1;
Marks2 = m2;
}
void show() {
System.out.println("Student Name : "+name);
System.out.println("Roll no : "+roll_no);
System.out.println("Marks1 : "+Marks1);
System.out.println("Marks2 : "+Marks2);
}
}
class Result extends Student implements Exam {
float per;
Result(String n,int rn,int m1,int m2) {
super(n,rn,m1,m2);
}
public void Percent_cal() {
int tot = Marks1 + Marks2;
per = (float)tot / 2;
}
void display() {
show();
System.out.println("Percentage = "+per);
}
}
public class StudentDetails {
public static void main (String[] args) {
Result r = new Result("Aashish",11,75,95);
r.Percent_cal();
r.display();
}
}
Assignment
1. Difference between abstract class and interface
2. Difference between extends and implements keyword
3. Can we re-assign a value to a field of interfaces?
4. Can a class implement more than one interface? Justify.
5. How can we access an interface from another interface and how to access from a class?
Explain with an example.
6. How many interfaces can a class inherit and can a same interface be inherited by 2
different classes? Explain it?
7. Write a program to demonstrate multiple inheritance. Consider two base classes
worker(int code, char name, float salary), HR who calculates the different allowances like
(float DA, HRA). You have another class manger with calculates (float TA(is 10% of
salary), gross salary) where the properties of worker and HR is used in this class. Write a
test application class to create an array of 10 workers and display their details with gross
salary.