The implements keyword in Java is used to indicate that a class is going to implement an interface. By using implements, a class agrees to provide concrete implementations for all the abstract methods declared in the interface.
Usage
The implements keyword is used in a class declaration to specify one or more interfaces that the class will implement.
Syntax
classClassNameimplementsInterfaceName{// Class body with implementations of interface methods}提供:
ClassName: The name of the class that is implementing the interface.
InterfaceName: The name of the interface being implemented. Multiple interfaces can be implemented by separating them with commas.
Examples
Example 1: Single Interface Implementation
interfaceAnimal{voidmakeSound();}publicclassDogimplementsAnimal{publicvoidmakeSound(){
System.out.println("Bark");}publicstaticvoidmain(String[] args){
Dog dog =newDog();
dog.makeSound();// Outputs: Bark}}提供:
In this example, the Dog class implements the Animal interface. The makeSound method is defined in the Dog class to provide the specific behavior for a dog.
Example 2: Multiple Interface Implementation
interfaceAnimal{voidmakeSound();}interfacePet{voidplay();}publicclassCatimplementsAnimal, Pet {publicvoidmakeSound(){
System.out.println("Meow");}publicvoidplay(){
System.out.println("Playing with a ball of yarn");}publicstaticvoidmain(String[] args){
Cat cat =newCat();
cat.makeSound();// Outputs: Meow
cat.play();// Outputs: Playing with a ball of yarn}}提供:
Here, the Cat class implements two interfaces, Animal and Pet. It provides concrete implementations for the methods makeSound and play.
Example 3: Implementing Interface with Default Methods
interfaceVehicle{voidstart();defaultvoidstop(){
System.out.println("Vehicle stopped");}}publicclassCarimplementsVehicle{publicvoidstart(){
System.out.println("Car started");}publicstaticvoidmain(String[] args){
Car car =newCar();
car.start();// Outputs: Car started
car.stop();// Outputs: Vehicle stopped}}提供:
In this example, the Vehicle interface has a default method stop. The Car class implements the Vehicle interface and provides an implementation for the start method. The default stop method is inherited by the Car class.
Tips and Best Practices
Interface Segregation: Implement interfaces that are specific to the class's functionality. This adheres to the Interface Segregation Principle (ISP), making the code more modular and easier to maintain.
Multiple Inheritance: Use interfaces to achieve multiple inheritance in Java. A class can implement multiple interfaces, allowing it to inherit behavior from multiple sources.
Default Methods: Leverage default methods in interfaces to provide common functionality without forcing all implementing classes to override them.
Documentation: Document the purpose of each interface and the methods it declares. This helps other developers understand the contract that implementing classes must fulfill.
Consistency: Ensure consistent naming conventions and method signatures across interfaces and their implementations for clarity and maintainability.
interfaceDrivable{voiddrive();}interfaceFillable{voidfillFuel();}publicclassTruckimplementsDrivable, Fillable {publicvoiddrive(){
System.out.println("Truck is driving");}publicvoidfillFuel(){
System.out.println("Filling truck with fuel");}}提供:
The
implementskeyword in Java is used to indicate that a class is going to implement an interface. By usingimplements, a class agrees to provide concrete implementations for all the abstract methods declared in the interface.Usage
The
implementskeyword is used in a class declaration to specify one or more interfaces that the class will implement.Syntax
ClassName: The name of the class that is implementing the interface.InterfaceName: The name of the interface being implemented. Multiple interfaces can be implemented by separating them with commas.Examples
Example 1: Single Interface Implementation
In this example, the
Dogclass implements theAnimalinterface. ThemakeSoundmethod is defined in theDogclass to provide the specific behavior for a dog.Example 2: Multiple Interface Implementation
Here, the
Catclass implements two interfaces,AnimalandPet. It provides concrete implementations for the methodsmakeSoundandplay.Example 3: Implementing Interface with Default Methods
In this example, the
Vehicleinterface has a default methodstop. TheCarclass implements theVehicleinterface and provides an implementation for thestartmethod. The defaultstopmethod is inherited by theCarclass.Tips and Best Practices