🎓 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
1. Introduction
In Java, extends and implements are keywords used for inheritance and implementation. While extends is used by a class to inherit properties and methods from another class (superclass), implements is used by a class to adhere to an interface or multiple interfaces, meaning it must implement all abstract methods defined in the interface. This fundamental concept of object-oriented programming (OOP) helps in code reusability and polymorphism. This blog post will discuss the difference between extends and implements through a simple example.
2. Program Steps
1. Define a superclass and an interface with some methods.
2. Create a subclass that extends the superclass and implements the interface.
3. Demonstrate method overriding and implementation in the subclass.
4. Create an instance of the subclass and call its methods to demonstrate inheritance and implementation.
3. Code Program
// Step 1: Defining a superclass
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
// Defining an interface
interface Pet {
void play(); // Interface method (does not have a body)
}
// Step 2: Creating a subclass that extends a class and implements an interface
class Dog extends Animal implements Pet {
// Step 3: Implementing interface method and overriding superclass method
@Override
void eat() {
System.out.println("Dog eats meat.");
}
@Override
public void play() {
System.out.println("Dog plays with owner.");
}
}
public class Main {
public static void main(String[] args) {
// Step 4: Creating an instance of the subclass and calling its methods
Dog myDog = new Dog();
myDog.eat(); // Demonstrates overriding
myDog.play(); // Demonstrates interface method implementation
}
}
Output:
Dog eats meat. Dog plays with owner.
Explanation:
1. Superclass (Animal): Defines a general method eat() that describes a generic action applicable to all animals.
2. Interface (Pet): Declares an abstract method play() that any class implementing the Pet interface must define. Interfaces specify a contract that implementing classes must fulfill.
3. Subclass (Dog): Inherits from Animal using extends and commits to the Pet interface using implements. It provides specific implementations for the eat() and play() methods, demonstrating how a class can extend functionality from a superclass and adhere to an interface's contract.
4. The Dog class overrides the eat() method inherited from Animal to provide a specific implementation for dogs. It also implements the play() method defined by the Pet interface.
5. An instance of Dog is created, and calling its eat() and play() methods demonstrates both method overriding and interface method implementation in action.
6. This example highlights the use of extends for inheritance and implements for fulfilling interface contracts in Java, illustrating how they contribute to polymorphism and code reusability.
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