Interfaces
6.1. Interface
An interface in Java is a collection of abstract methods (methods without a body) and constant declarations. It provides a way to achieve abstraction in Java, allowing the definition of a set of methods that must be implemented by any class that implements the interface.
Syntax:
interface MyInterface {
// Declare constant(s)
int MY_CONSTANT = 42;
// Declare abstract method(s)
void myMethod();
}
Explanation:
Interfaces declare methods without providing a method body. They only specify what methods a class should implement.
Constants in interfaces are implicitly public, static, and final.
Any class implementing an interface must provide concrete implementations for all the declared methods.
Programming Example:
interface MyInterface {
int MY_CONSTANT = 42;
void myMethod();
}
class MyClass implements MyInterface {
@Override
public void myMethod() {
System.out.println("Implementation of myMethod");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod();
System.out.println("Constant value: " + MyInterface.MY_CONSTANT);
}
}
6.2. Extend Interfaces
Define:
In Java, an interface can extend another interface. This allows the new interface to inherit the abstract methods and constants of the extended interface.
Syntax:
interface MyInterface {
void myMethod();
}
interface MyExtendedInterface extends MyInterface {
void myExtendedMethod();
}
Explanation:
MyExtendedInterface inherits the abstract method myMethod from MyInterface.
Classes implementing MyExtendedInterface must provide concrete implementations for both myMethod and myExtendedMethod.
Programming Example:
interface MyInterface {
void myMethod();
}
interface MyExtendedInterface extends MyInterface {
void myExtendedMethod();
}
class MyClass implements MyExtendedInterface {
@Override
public void myMethod() {
System.out.println("Implementation of myMethod");
}
@Override
public void myExtendedMethod() {
System.out.println("Implementation of myExtendedMethod");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod();
obj.myExtendedMethod();
}
}
6.3. Implement an Interface
Define:
To use an interface in a class, you need to implement the interface using the implements keyword. This means providing concrete implementations for all the abstract methods declared in the interface.
Syntax:
interface MyInterface {
void myMethod();
}
class MyClass implements MyInterface {
@Override
public void myMethod() {
// Implementation of myMethod
}
}
Explanation:
The implements keyword is used to indicate that a class is going to provide concrete implementations for the methods declared in the interface.
Programming Example:
interface MyInterface {
void myMethod();
}
class MyClass implements MyInterface {
@Override
public void myMethod() {
System.out.println("Implementation of myMethod");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.myMethod();
}
}
6.4. Implement Multiple Interfaces
Define:
A class in Java can implement multiple interfaces. This allows a class to inherit and provide implementations for the abstract methods declared in multiple interfaces.
Syntax:
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
class MyClass implements Interface1, Interface2 {
@Override
public void method1() {
// Implementation of method1
}
@Override
public void method2() {
// Implementation of method2
}
}
Explanation:
The class MyClass must provide concrete implementations for all methods declared in both Interface1 and Interface2.
Programming Example:
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
class MyClass implements Interface1, Interface2 {
@Override
public void method1() {
System.out.println("Implementation of method1");
}
@Override
public void method2() {
System.out.println("Implementation of method2");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method1();
obj.method2();
}
}
6.5. Differentiate between Interface and Abstract Class
Define:
An abstract class in Java is a class that may have abstract methods (methods without a body) as well as concrete methods (methods with a body). An interface, on the other hand, is a collection of abstract methods and constants.
Explanation:
An abstract class can have fields (variables), constructors, and methods with or without a body.
An interface can only have constants and abstract methods, and all the methods are implicitly public and abstract.
A class can implement multiple interfaces, but it can extend only one abstract class.
Programming Example:
// Abstract Class
abstract class MyAbstractClass {
int myField;
MyAbstractClass(int value) {
this.myField = value;
}
abstract void myAbstractMethod();
void myConcreteMethod() {
System.out.println("Concrete method in abstract class");
}
}
// Interface
interface MyInterface {
int MY_CONSTANT = 42;
void myMethod();
}
// Class implementing an Interface and extending an Abstract Class
class MyClass extends MyAbstractClass implements MyInterface {
MyClass(int value) {
super(value);
}
@Override
void myAbstractMethod() {
System.out.println("Implementation of myAbstractMethod");
}
@Override
public void myMethod() {
System.out.println("Implementation of myMethod");
}
}
public class Main {
public static void main(String[] args) {
MyClass obj = new MyClass(10);
obj.myAbstractMethod();
obj.myMethod();
obj.myConcreteMethod();
System.out.println("
constant value: " + MyInterface.MY_CONSTANT);
}
}
In this example, MyClass implements MyInterface and extends MyAbstractClass, demonstrating the combination of abstract classes and interfaces in a class hierarchy.
Comments
Post a Comment