[Go to site: main page, start]

0% found this document useful (0 votes)
3 views13 pages

Java Interface

The document explains Java interfaces as abstract types that define methods a class must implement, facilitating abstraction and multiple inheritance. It details the syntax for declaring interfaces, their features, and differences from classes, including the introduction of default and static methods in Java 8 and private methods in Java 9. The document also provides examples of implementing interfaces and the relationship between classes and interfaces.

Uploaded by

inayat10ff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Java Interface

The document explains Java interfaces as abstract types that define methods a class must implement, facilitating abstraction and multiple inheritance. It details the syntax for declaring interfaces, their features, and differences from classes, including the introduction of default and static methods in Java 8 and private methods in Java 9. The document also provides examples of implementing interfaces and the relationship between classes and interfaces.

Uploaded by

inayat10ff
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Interface


An Interface in Java is an abstract type that defines a set of methods a class
must implement.
 An interface acts as a contract that specifies what a class should do, but
not how it should do it. It is used to achieve abstraction and multiple
inheritance in Java. We define interfaces for capabilities
 A class that implements an interface must implement all the methods of
the interface. Only variables are public static final by default.
 Before Java 8, interfaces could only have abstract methods (no bodies).
Since Java 8, they can also include default and static methods (with
implementation) and since Java 9, private methods are allowed.

Syntax of Interface Declaration

Here is the syntax to declare an interface:

1. interface <interface_name> {
2. // declare constant fields
3. // declare abstract methods
4. }

Example of Declaring an Interface

Here is an example to declare an interface:

1. interface Animal {
2. void eat();
3. void sleep();
4. }
This example demonstrates how an interface in Java defines constants and
abstract methods, which are implemented by a class.

import [Link].*;

// Interface Declared
interface testInterface {

// public, static and final


final int a = 10;

// public and abstract


void display();
}

// Class implementing interface


class TestClass implements testInterface {

// Implementing the capabilities of Interface


public void display(){
[Link]("Geek");
}
}

class Geeks{

public static void main(String[] args){

TestClass t = new TestClass();


[Link]();
[Link](t.a);
}
}

Output
Geek
10
Notes:
 Private methods can only be called inside default or static methods.
 Static methods are accessed using the interface name, not via objects.
 To implement an interface, use the implements keyword.
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.
When to Use Class and Interface?
Use a Class when:
 Use a class when you need to represent a real-world entity with attributes
(fields) and behaviors (methods).
 Use a class when you need to create objects that hold state and perform
actions
 Classes are used for defining templates for objects with specific
functionality and properties.
Use an Interface when:
 Use an interface when you need to define a contract for behavior that
multiple classes can implement.
 Interface is ideal for achieving abstraction and multiple inheritance.
Implementation: To implement an interface, we use the keyword
implements
Let’s consider the example of Vehicles like bicycles, cars and bikes share
common functionalities, which can be defined in an interface, allowing each
class (e.g., Bicycle, Car, Bike) to implement them in its own way. This
approach ensures code reusability, scalability and consistency across
different vehicle types.

import [Link].*;

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() {


[Link]("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() {


[Link]("speed: " + speed
+ " gear: " + gear);
}

class Main
{
public static void main (String[] args)
{

// Instance of Bicycle(Object)
Bicycle bicycle = new Bicycle();

[Link](2);
[Link](3);
[Link](1);

[Link]("Bicycle present state : ");


[Link]();

// Instance of Bike (Object)


Bike bike = new Bike();
[Link](1);
[Link](4);
[Link](3);

[Link]("Bike present state : ");


[Link]();
}
}

Output
Bicycle present state : speed: 2 gear: 2
Bike present state : speed: 1 gear: 1
Multiple Inheritance in Java Using Interface
Java does not support multiple inheritance with classes to avoid ambiguity,
but it supports multiple inheritance using interfaces.
import [Link].*;

// 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();
[Link]("Addition : " + [Link](2,1));
[Link]("Substraction : " + [Link](2,1));
}
}

Output
Addition : 3
Substraction : 1
New Features Added in Interfaces in JDK 8
There are certain features added to Interfaces in JDK 8 update mentioned
below:

1. Default Methods
 Interfaces can define methods with default implementations.
 Useful for adding new methods to interfaces without breaking existing
implementations.
interface TestInterface
{
final int a = 10;

default void display() {


[Link]("hello");
}
}

// A class that implements the interface.


class TestClass implements TestInterface
{
// Driver Code
public static void main (String[] args) {
TestClass t = new TestClass();
[Link]();
}
}

Output
hello
2. Static Methods
 Interfaces can now include static methods.
 These methods are called directly using the interface name and are not
inherited by implementing classes.
Another feature that was added in JDK 8 is that we can now define static
methods in interfaces that can be called independently without an
object. These methods are not inherited.
interface TestInterface
{
final int a = 10;
static void display()
{
[Link]("hello");
}
}

// A class that implements the interface.


class TestClass implements TestInterface
{
// Driver Code
public static void main (String[] args)
{
[Link]();
}
}

Output
hello
New Features Added in Interfaces in JDK 9
From Java 9 onwards, interfaces can contain the following also:

1. Private Methods
 Interface can now include private methods.
 Private methods are defined within the interface but it cannot be accessed
by the implementing classes.
 Private methods cannot be overridden by implementing classes as they
are not inherited.
interface Vehicle {
// Private method for internal use
private void startEngine() {
[Link]("Engine started.");
}

// Default method that uses the private method


default void drive() {
// Calls the private method
startEngine();
[Link]("Vehicle is now driving.");
}
}

class Car implements Vehicle {


// Car class implements Vehicle interface and inherits the
default method 'drive'
}

public class Main {


public static void main(String[] args) {
Car car = new Car();
// This will call the default method, which in turn
calls the private method
[Link]();
}
}

Output
Engine started.
Vehicle is now driving.
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.
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() {
[Link]("Method 1");
}

public void method2() {


[Link]("Method 2");
}

public void method3() {


[Link]("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();
}
}
Output
Method 1
Method 2
Method 3
Difference Between Class and Interface
Although Class and Interface seem the same there are certain differences
between Classes and Interface. The major differences between a class and
an interface are mentioned below:

Features Class Interface

In class, you can In an interface, you can't


Instantiation create an object. create an object

Class can have Variables are public static


Variables instance variables final (constants only).

Class can have In an interface, methods are


Methods concrete methods abstract by default

It supports single Supports multiple


Inheritance inheritance inheritance

Can have
No constructors allowed.
Constructors constructors.

Supports private, In an interface, all


protected, public, members are public by
Access Modifiers default. default

Keyword Defined using class. Defined using interface.

It does not support It supports default


Default Methods default methods methods(JDK 8+)

Can have static Supports static methods


Static Methods methods. (JDK 8+)
Features Class Interface

Can have private Supports private methods


Private Methods methods. (JDK 9+).

Can have main() (since


Can have main() for
JDK 8, as static methods
execution.
Main Method are allowed).

You might also like