In this lesson, you will learn

Interfaces in Java are powerful constructs that provide a way to achieve abstraction and define contracts. Here’s a breakdown of their key features:

1. Abstract Methods:
void calculateArea();2. Constants (Static Final Variables):
public, static, and final. This means they are constants and cannot be changed.int MAX_VALUE = 100;3. Default Methods (Java 8 and Later):
default void printMessage() { System.out.println("Default message"); }4. Static Methods (Java 8 and Later):
static int calculateSquare(int x) { return x * x; }5. Multiple Inheritance (of Type):
class MyClass implements InterfaceA, InterfaceB { ... }6. Abstraction:
7. Contracts:
In summary, interfaces in Java are designed to promote abstraction, enforce contracts, and enable a form of multiple inheritance, contributing to cleaner, more flexible, and maintainable code.
You declare an interface using the interface keyword. Here’s the basic syntax:
public interface MyInterface {
// Constant declarations
int MY_CONSTANT = 10;
// Abstract method declarations
void myMethod();
// Default method (Java 8 and later)
default void myDefaultMethod() {
// Default implementation
}
// Static method (Java 8 and later)
static void myStaticMethod() {
// Static implementation
}
}
Note: When no access modifier is included, then default access results, and the interface is only available to other members of the package in which it is declared.
interface item
{
static final int n=1001;
static final String name="Alice";
void display();
}
interface Area
{
static final float pi=3.14f;
float compute (float x, float y);
void show();
}
Implementing an interface in Java involves creating a class that agrees to fulfill the contract defined by the interface. Here is the syntax
class class-name implements Interface1, Interface2, ...
{
//Class Body
}
package ch10.l4;
//Define an interface
interface Animal {
// Interface method (does not have a body)
void eat();
void sleep();
// Default method with a body (Java 8+)
default void breathe() {
System.out.println("Breathing");
}
}
//Implement the interface with a class
class Dog implements Animal {
// The body of eat() is provided here
public void eat() {
System.out.println("Dog is eating");
}
// The body of sleep() is provided here
public void sleep() {
System.out.println("Dog is sleeping");
}
}
public class AnimalAccess {
public static void main(String[] args) {
Dog myDog = new Dog();
// Call the method on the dog object
myDog.eat();
myDog.sleep();
myDog.breathe(); // Default method can be called
}
}
Dog is eating
Dog is sleeping
Breathing
In this example, Animal is an interface with two methods eat() and sleep() that do not have bodies, which means they are abstract methods.
The Dog class implements the Animal interface, meaning it must provide implementations for the eat() and sleep() methods.
Additionally, the Animal interface has a default method breathe() with a body, which the Dog class inherits and can be used without providing its own implementation.
package ch10.l4;
interface Shape {
// Static field
int MAX_SHAPES = 100; // Example static field (constant)
// Abstract methods
double calculateArea();
double calculatePerimeter();
// Default method with a body (Java 8+)
default void printDetails() {
System.out.println("Area: " + calculateArea());
System.out.println("Perimeter: " + calculatePerimeter());
}
// Static method (Java 8+)
static int getMaxShapes() {
return MAX_SHAPES;
}
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
@Override
public double calculateArea() {
return length * width;
}
@Override
public double calculatePerimeter() {
return 2 * (length + width);
}
}
public class AccessShapes {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(10, 5);
// Use the default method
System.out.println("Circle Details:");
circle.printDetails();
System.out.println("Rectangle Details:");
rectangle.printDetails();
// Use the static method
System.out.println("Maximum number of shapes: " + Shape.getMaxShapes());
}
}
Circle Details:
Area: 78.53981633974483
Perimeter: 31.41592653589793
Rectangle Details:
Area: 50.0
Perimeter: 30.0
Maximum number of shapes: 100
Very nice videos, i always study for my exams from here, the course is very detailed and helpful
You must be logged in to submit a review.