0% found this document useful (0 votes)
2 views

java_interfacing

The document provides an overview of Java interfaces, explaining their purpose as fully abstract classes that define abstract methods to be implemented by other classes. It covers key concepts such as implementing interfaces, extending interfaces, default methods, and the advantages of using interfaces for abstraction and multiple inheritance. Additionally, it includes practical examples demonstrating the implementation of interfaces and their methods in Java programming.

Uploaded by

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

java_interfacing

The document provides an overview of Java interfaces, explaining their purpose as fully abstract classes that define abstract methods to be implemented by other classes. It covers key concepts such as implementing interfaces, extending interfaces, default methods, and the advantages of using interfaces for abstraction and multiple inheritance. Additionally, it includes practical examples demonstrating the implementation of interfaces and their methods in Java programming.

Uploaded by

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

Java Interface

Introduction:
• An interface is a fully abstract class. It includes a group
of abstract methods (methods without a body).
• We use the interface keyword to create an interface in Java.
For example,

Here,
•Language is an interface.
•It includes abstract methods: getType() and getVersion().
Contd.
• To access the interface methods, the interface must be "implemented" like inherited) by
another class with the implements keyword (instead of extends).
• The body of the interface method is provided by the "implement" class:
Implementing an Interface
• Like abstract classes, we cannot create objects of interfaces.
• To use an interface, other classes must implement it. We use the implements
keyword to implement an interface.
• Example 1: Java Interface
interface Polygon { class Main {
void getArea(int length, int breadth); public static void main(String[] args) {
} Rectangle r1 = new Rectangle();
r1.getArea(5, 6);
// implement the Polygon interface }
class Rectangle implements Polygon { }

// implementation of abstract method


public void getArea(int length, int breadth) {
System.out.println("The area of the rectangle is " + (length * breadth));
}
}
• in the above example, we have created an interface named Polygon. The interface contains an abstract
method getArea().
• Here, the Rectangle class implements Polygon. And, provides the implementation of the getArea() method.
Example 2: Java Interface
// create an interface
interface Language {
void getName(String name);
}
// class implements interface
class ProgrammingLanguage implements Language { • In the above example, we have created an
// implementation of abstract method interface named Language.
public void getName(String name) { • The interface includes an abstract
System.out.println("Programming Language: " + name); method getName().
} • Here, the ProgrammingLanguage class
}
implements the interface and provides the
class Main {
implementation for the method.
public static void main(String[] args) {
ProgrammingLanguage language = new
ProgrammingLanguage();
language.getName("Java");
}
}
Implementing Multiple Interfaces
• In Java, a class can also implement multiple interfaces. For example,

interface A {
// members of A
}
interface B {
// members of B
}
class C implements A, B {
// abstract members of A // abstract members of B
}
Example:

OUTPUT:

Some text..
Some other text...
Extending an Interface
• Similar to classes, interfaces can extend other interfaces. The extends keyword is
used for extending interfaces. For example,

• Here, the Polygon interface extends the Line interface.


• Now, if any class implements Polygon, it should provide implementations for all the abstract
methods of both Line and Polygon.
Extending Multiple Interfaces
An interface can extend multiple interfaces. For example,
interface A {
...
}
interface B {
...
}
interface C extends A, B {
...
}
Advantages of Interface in Java
• Similar to abstract classes, interfaces help us to achieve abstraction in
Java.
Here, we know getArea() calculates the area of polygons, but the way area
is calculated is different for different polygons. Hence, the implementation of
getArea() is independent of one another.
• Interfaces provide specifications that a class (which implements it) must
follow.
In our previous example, we used getArea() as a specification inside the
interface polygon. This is like setting a rule that we should be able to get
the area of every polygon.
.
Now any class that implements the Polygon interface must provide an implementation for the getArea()
method.
• Interfaces are also used to achieve multiple inheritance in Java. For
example,
• Here, the class Rectangle is implementing two different interfaces.
This is how we achieve multiple inheritance in Java.
Note: All the methods inside an interface are implicitly public and all
fields are implicitly public static final. For example,
default methods in Java Interfaces:
• When we add methods with implementation inside an interface.
These methods are called default methods.
• To declare default methods inside interfaces, we use the default
keyword. For example,
Why default methods?
• Suppose, we need to add a new method in an interface.
• We can add the method in our interface easily without
implementation.
• However, all our classes that implement that interface must provide
an implementation for the method.
• If a large number of classes were implementing this interface, we
need to track all these classes and make changes to them. This is not
only tedious but error-prone as well.
• To resolve this, Java introduced default methods. Default methods are
inherited like ordinary methods.
Example: Default Method in Java Interface
interface Polygon { // implements the interface
void getArea(); class Square implements Polygon {
// default method public void getArea() {
default void getSides() { int length = 5;
System.out.println("I can get sides of a polygon."); int area = length * length;
} System.out.println("The area of the square is " + area);
} }
// implements the interface }
class Rectangle implements Polygon { class Main {
public void getArea() { public static void main(String[] args) {
int length = 6; // create an object of Rectangle
int breadth = 5; Rectangle r1 = new Rectangle();
int area = length * breadth; r1.getArea();
System.out.println("The area of the rectangle is " + area); r1.getSides();
}
// overrides the getSides() // create an object of Square
public void getSides() { Square s1 = new Square();
System.out.println("I have 4 sides."); s1.getArea();
} s1.getSides();
} }
}
• In the above example, we have created an interface named Polygon. It has a
default method getSides() and an abstract method getArea().
• Here, we have created two classes, Rectangle and Square, that
implement Polygon.
• The Rectangle class provides the implementation of the getArea() method and
overrides the getSides() method. However, the Square class only provides the
implementation of the getArea() method.
• Now, while calling the getSides() method using the Rectangle object, the
overridden method is called.
• However, in the case of the Square object, the default method is called.
Default methods in Java provide:
• Backward compatibility when evolving interfaces.
• Code reusability by providing common implementations in interfaces.
• Support for multiple inheritance of behavior.
• Smooth evolution of APIs while maintaining existing functionality.
• Flexible default implementations in libraries and frameworks.
• These features help Java interfaces be more powerful and adaptable
to changing requirements over time.
static Methods in Interface
• Similar to a class, we can access static methods of an interface using
its references. For example,
Practical Example of Interface
this.c = c;
// To use the sqrt function
s = 0;
import java.lang.Math;
}
interface Polygon {
// calculate the area of a triangle
void getArea();
public void getArea() {
// calculate the perimeter of a Polygon
s = (double) (a + b + c)/2;
default void getPerimeter(int... sides) {
area = Math.sqrt(s*(s-a)*(s-b)*(s-c));
int perimeter = 0;
System.out.println("Area: " + area);
for (int side: sides) {
}
perimeter += side;
}
}
class Main {
System.out.println("Perimeter: " + perimeter);
public static void main(String[] args) {
}
Triangle t1 = new Triangle(2, 3, 4);
}
class Triangle implements Polygon {
// calls the method of the Triangle class
private int a, b, c;
t1.getArea();
private double s, area;
// initializing sides of a triangle
// calls the method of Polygon
Triangle(int a, int b, int c) {
t1.getPerimeter(2, 3, 4);
this.a = a;
}
this.b = b;
}
• In the above program, we have created an interface named Polygon. It includes a
default method getPerimeter() and an abstract method getArea().
• We can calculate the perimeter of all polygons in the same manner so we
implemented the body of getPerimeter() in Polygon.
• Now, all polygons that implement Polygon can use getPerimeter() to calculate
perimeter.
• However, the rule for calculating the area is different for different polygons.
Hence, getArea() is included without implementation.
• Any class that implements Polygon must provide an implementation
of getArea().
Example: Multiple Inheritance in Java:
interface Backend {
// abstract class public static void main(String[] args) {
public void connectServer();
} // create object of Language class
class Frontend { Language java = new Language();
public void responsive(String str) {
System.out.println(str + " can also be used as frontend."); java.connectServer();
}
} // call the inherited method of Frontend class
// Language extends Frontend class java.responsive(java.language);
// Language implements Backend interface }

class Language extends Frontend implements Backend { }


String language = "Java";
// implement method of interface
public void connectServer() {
System.out.println(language + " can be used as backend language.");
}
• In the above example, we have created an interface named Backend and a class
named Frontend. The class Language extends the Frontend class and implements
the Backend interface.
• Here, the Language class is inheriting the property of
both Backend and Frontend. Hence, we can say it is an example of multiple
inheritance.

You might also like