🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Interfaces cannot contain instance fields or constructors. They provide a way to achieve abstraction and multiple inheritance in Java. An interface is a contract that a class can implement, specifying what methods the class must provide.
Key Points:
- Abstraction: Interfaces provide a way to define abstract types.
- Multiple Inheritance: A class can implement multiple interfaces.
- Default Methods: Java 8 introduced default methods, which allow interfaces to have methods with implementations.
- Static Methods: Interfaces can also have static methods.
- Functional Interfaces: Interfaces with a single abstract method can be used as functional interfaces.
Table of Contents
- Defining an Interface
- Implementing an Interface
- Default Methods
- Static Methods
- Functional Interfaces
- Real-World Example
- Conclusion
1. Defining an Interface
An interface is defined using the interface keyword. It can contain method signatures, default methods, static methods, and constants.
Example:
public interface Animal {
// Constant
int DEFAULT_AGE = 1;
// Abstract method
void makeSound();
// Default method
default void eat() {
System.out.println("Eating...");
}
// Static method
static void sleep() {
System.out.println("Sleeping...");
}
}
Explanation:
- DEFAULT_AGE: A constant defined in the interface.
- makeSound(): An abstract method that must be implemented by any class that implements the interface.
- eat(): A default method with a provided implementation.
- sleep(): A static method that can be called without an instance of a class implementing the interface.
2. Implementing an Interface
A class implements an interface using the implements keyword. The class must provide implementations for all abstract methods in the interface.
Example:
public class Dog implements Animal {
@Override
public void makeSound() {
System.out.println("Bark");
}
}
public class InterfaceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Bark
dog.eat(); // Outputs: Eating...
Animal.sleep(); // Outputs: Sleeping...
}
}
Explanation:
- Dog: A class that implements the
Animalinterface and provides an implementation for themakeSound()method. - InterfaceExample: A class with a
mainmethod to test theDogclass.
3. Default() Methods
Default methods allow an interface to have methods with implementations. This helps to provide additional functionalities to interfaces without breaking the implementing classes.
Example:
public interface Animal {
void makeSound();
default void eat() {
System.out.println("Eating...");
}
}
Explanation:
- eat(): A default method with an implementation. Classes that implement the
Animalinterface can use this method directly or override it.
4. Static() Methods
Interfaces can have static methods, which are called on the interface itself rather than on instances of the implementing classes.
Example:
public interface Animal {
void makeSound();
static void sleep() {
System.out.println("Sleeping...");
}
}
Explanation:
- sleep(): A static method that can be called using
Animal.sleep().
5. Functional Interfaces
A functional interface is an interface with a single abstract method. Functional interfaces can be used as the assignment target for lambda expressions or method references.
Example:
@FunctionalInterface
public interface Greeting {
void sayHello();
}
public class FunctionalInterfaceExample {
public static void main(String[] args) {
Greeting greeting = () -> System.out.println("Hello, World!");
greeting.sayHello(); // Outputs: Hello, World!
}
}
Explanation:
- Greeting: A functional interface with a single abstract method
sayHello(). - FunctionalInterfaceExample: A class demonstrating the use of a lambda expression to implement the
Greetinginterface.
6. Real-World Example
Let's create a real-world example with multiple interfaces and a class that implements them.
Example:
public interface Animal {
void makeSound();
void eat();
}
public interface Pet {
void play();
}
public class Dog implements Animal, Pet {
@Override
public void makeSound() {
System.out.println("Bark");
}
@Override
public void eat() {
System.out.println("Dog is eating");
}
@Override
public void play() {
System.out.println("Dog is playing");
}
}
public class RealWorldExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.makeSound(); // Outputs: Bark
dog.eat(); // Outputs: Dog is eating
dog.play(); // Outputs: Dog is playing
}
}
Explanation:
- Animal: An interface with abstract methods
makeSound()andeat(). - Pet: An interface with an abstract method
play(). - Dog: A class that implements both
AnimalandPetinterfaces. - RealWorldExample: A class with a
mainmethod to test theDogclass.
7. Conclusion
Interfaces in Java provide a powerful way to achieve abstraction and multiple inheritance. By using interfaces, you can define contracts that classes must adhere to, allowing for more flexible and maintainable code. Understanding default methods, static methods, and functional interfaces can help you leverage the full potential of Java interfaces in your applications.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment