Chapter 6
Interfaces
6.1 Introduction
6.2. Defining and Implementing Interfaces
6.3. Advantages of using Interface
6.1 Introduction
An interface in Java is a collection of abstract methods. It provides a way to achieve abstraction and multiple inheritance in Java. An interface is a blueprint for a class and allows the definition of methods without specifying the implementation. Classes implementing an interface must provide concrete implementations for all its methods.
Syntax
interface MyInterface{ {
// Declare methods (implicitly public and abstract)
void method1();
void method2(); }
Explanation
An interface is declared using the interface keyword.
Methods declared in the interface are implicitly public and abstract.
An interface may also contain constant (public, static, and final) variables.
Programming Example
interface Shape {
void draw(); // Abstract method
double calculateArea(); // Abstract method
}
// Implementation of the interface
class Circle implements Shape {
double radius;
Circle(double radius) {
this.radius = radius; }
public void draw() {
System.out.println("Drawing Circle"); }
public double calculateArea() {
return Math.PI * radius * radius;
} }
* radius * radius; } }
6.2 Defining and Implementing Interfaces
Defining interfaces involves specifying method signatures without providing implementations. Implementing interfaces requires concrete classes to provide actual implementations for the methods declared in the interface.
Syntax
class MyClass implements MyInterface {
// Implement methods declared in MyInterface
@Override
public void method1() {
// Provide implementation
}
public void method2() {
// Provide implementation
} }
//provide implementation } }
Explanation
Use the implements keyword to indicate that a class is implementing an interface.
The implementing class must provide concrete implementations for all methods declared in the interface.
Programming Example
interface MyInterface {
void method1();
void method2();
}
// Class implementing the interface
class MyClass implements MyInterface {
public void method1() {
System.out.println("Implementation of method1");
}
public void method2() {
System.out.println("Implementation of method2");
} }
6.3 Advantages of using Interface
Interfaces provide several advantages, including achieving abstraction, supporting multiple inheritance, and enabling code flexibility and reusability.
Explanation
Abstraction: Interfaces allow you to define methods without specifying their implementations, promoting abstraction.
Multiple Inheritance: Java supports multiple inheritance through interfaces, allowing a class to implement multiple interfaces.
Flexibility and Reusability: Interfaces enhance code flexibility by enabling classes to implement multiple interfaces, promoting code reusability.
Programming Example
interface A {
void methodA(); }
interface B {
void methodB(); }
class MyClass implements A, B {
public void methodA() {
System.out.println("Implementation of methodA"); }
public void methodB() {
System.out.println("Implementation of methodB");}}
Comments
Post a Comment