Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Java Interface

A Java interface is an abstract type that defines a set of behaviors for classes, allowing for abstraction and multiple inheritance. Interfaces contain public, static, and final variables and require implementing classes to define their abstract methods. They can extend other interfaces, and implementing multiple interfaces is a way to achieve multiple inheritance in Java, which is otherwise not allowed with classes.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Java Interface

A Java interface is an abstract type that defines a set of behaviors for classes, allowing for abstraction and multiple inheritance. Interfaces contain public, static, and final variables and require implementing classes to define their abstract methods. They can extend other interfaces, and implementing multiple interfaces is a way to achieve multiple inheritance in Java, which is otherwise not allowed with classes.

Uploaded by

pvy1524
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Interface

An Interface in Java programming language is defined as an abstract type used to


specify the behavior of a class. An interface in Java is a blueprint of a behavior. A
Java interface contains static constants and abstract methods.

 The interface in Java is a mechanism to achieve abstraction.


 By default, variables in an interface are public, static, and final.
 It is used to achieve abstraction and multiple inheritances in Java.
 It is also used to achieve loose coupling.
 In other words, interfaces primarily define methods that other classes must
implement.
 An interface in Java defines a set of behaviors that a class can implement,
usually representing an IS-A relationship, but not always in every scenario.

Syntax
interface {
// declare constant fields
// declare methods that abstract
// by default.
}

Relationship Between Class and Interface

A class can extend another class, and similarly, an interface can extend
another interface. However, only a class can implement an interface, and
the reverse (an interface implementing a class) is not allowed.

Example of Interface
import java.io.*;
interface Vehicle {
// Abstract methods defined
void changeGear(int a);
void speedUp(int a);
void applyBrakes(int a);
}

// Class implementing vehicle interface


class Bicycle implements Vehicle{
int speed;
int gear;
// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}
}
// Class implementing vehicle interface
class Bike implements Vehicle {
int speed;
int gear;
// Change gear
@Override
public void changeGear(int newGear){
gear = newGear;
}
// Increase speed
@Override
public void speedUp(int increment){
speed = speed + increment;
}
// Decrease speed
@Override
public void applyBrakes(int decrement){
speed = speed - decrement;
}
public void printStates() {
System.out.println("speed: " + speed
+ " gear: " + gear);
}}

class Main
{
public static void main (String[] args)
{
// Instance of Bicycle(Object)
Bicycle bicycle = new Bicycle();

bicycle.changeGear(2);
bicycle.speedUp(3);
bicycle.applyBrakes(1);

System.out.print("Bicycle present state : ");


bicycle.printStates();

// Instance of Bike (Object)


Bike bike = new Bike();
bike.changeGear(1);
bike.speedUp(4);
bike.applyBrakes(3);

System.out.print("Bike present state : ");


bike.printStates();
}
}

Multiple Inheritance in Java Using Interface

Multiple Inheritance is an OOPs concept that can’t be implemented in Java using


classes. But we can use multiple inheritances in Java using Interface. Let us check
this with an example.
Example of Multiple Inheritance in java
import java.io.*;
// Add interface
interface Add{
int add(int a,int b);
}

// Sub interface
interface Sub{
int sub(int a,int b);
}

// Calculator class implementing


// Add and Sub
class Cal implements Add , Sub
{
// Method to add two numbers
public int add(int a,int b){
return a+b;
}

// Method to sub two numbers


public int sub(int a,int b){
return a-b;
}

class GFG{
// Main Method
public static void main (String[] args)
{
// instance of Cal class
Cal x = new Cal();

System.out.println("Addition : " + x.add(2,1));


System.out.println("Substraction : " + x.sub(2,1));

}
}

Extending Interfaces

One interface can inherit another by the use of keyword extends. When a class
implements an interface that inherits another interface, it must provide an
implementation for all methods required by the interface inheritance chain.

Example
interface A {
void method1();
void method2();
}

// B now includes method1


// and method2
interface B extends A {
void method3();
}

// the class must implement


// all method of A and B.
class GFG implements B
{
public void method1() {
System.out.println("Method 1");
}

public void method2() {


System.out.println("Method 2");
}

public void method3() {


System.out.println("Method 3");
}

public static void main(String[] args){

// Instance of GFG class created


GFG x = new GFG();

// All Methods Called


x.method1();
x.method2();
x.method3();
}
}

In general, the development process is step by step:


Level 1 – interfaces: It contains the service details.
Level 2 – abstract classes: It contains partial implementation.
Level 3 – implementation classes: It contains all implementations.
Level 4 – Final Code / Main Method: It have access of all interfaces data.

Advantages of Interfaces
 Without bothering about the implementation part, we can achieve the
security of the implementation.
 In Java, multiple inheritances are not allowed, however, you can use
an interface to make use of it as you can implement more than one
interface.

You might also like