INTERFACE IN JAVA

Maha

INTERFACE IN JAVA

In the Java programming language, an interface is an abstract type that specifies a class's behaviour.

  • In Java, an interface is a blueprint for a class. Static constants and abstract methods are found in a Java interface.
  • An interface is a completely "abstract class" that is used to group related methods with empty bodies
  • The interface keyword is used to declare an interface. 
  •  The Java interface can only have abstract methods, not method bodies. In Java, it is used to achieve abstraction as well as multiple inheritance.

Definition of interface:

Defining an interface is similar to class. To create an interface, we use the keyword interface. An interface can extend any number of interfaces.

Syntax:

Interface intname

{

//constant fields

// methods and abstract

//by default

}


Example:

interface bike

{

int speed = 50;

public void showbike();

}

interface car

{

int speed = 65;

public void showcar();

}

class vehicle implements bike, car

{

public void showbike()

{

 System.out.println(“Bike speed: “ + speed);

}

public void showcar()

{

System.out.println(“Car speed: “ + speed);

}

public static void main(String as[])

{

Vehicle V = new Vehicle();

V.showbike();

V.showcar();

}

}

Output:

Bike speed: 50

Car speed: 65



Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send