Implements vs extends: When to use? What"s the difference?

Cover Image for Implements vs extends: When to use? What"s the difference?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🎉📝 Implements vs extends: When to use? What's the difference? 🤔

Are you tired of scratching your head 🤯 every time you need to decide between implements and extends in your code? Fear not! In this blog post, we'll break down these two powerful concepts and help you understand when to use each one. Let's dive in! 🏊‍♂️

🔄 The Basics: Implements vs Extends

In the world of object-oriented programming (OOP), both implements and extends serve crucial purposes when it comes to class inheritance. Let's see what each one does:

1️⃣ Implements

The implements keyword is used when a class implements an interface. An interface defines a contract that a class must adhere to by implementing its methods. By using implements, a class promises to provide an implementation for all the methods defined in the interface. Here's an example:

interface Animal {
   void makeSound();
}

class Dog implements Animal {
   void makeSound() {
      System.out.println("Woof!");
   }
}

In this example, the Dog class implements the Animal interface and provides its own implementation for the makeSound() method.

2️⃣ Extends

On the other hand, the extends keyword is used when a class extends another class, inheriting its properties and behavior. By using extends, a class can access the methods and fields of its superclass. Check out this example:

class Animal {
   void makeSound() {
      System.out.println("Animal sound");
   }
}

class Dog extends Animal {
   void makeSound() {
      System.out.println("Woof!");
   }
}

Here, the Dog class extends the Animal class, which means it inherits the makeSound() method. However, it can also override that method to provide its own implementation.

💡 Which one to use?

Now that we understand the basics of implements and extends, let's talk about when to use each one. It all depends on the relationship between classes and interfaces in your code.

You should use implements when:

  • Your class needs to adhere to a contract defined by an interface.

  • You want to provide a specific implementation for all the methods in the interface.

  • You may implement multiple interfaces.

On the other hand, you should use extends when:

  • Your class wants to inherit properties and behavior from another class.

  • You want to override methods from the superclass to provide a different implementation.

  • You can only extend one class (Java doesn't support multiple inheritance).

🚀 Conclusion and Call to Action

By now, you should have a clear understanding of when to use implements and extends in your code. Remember, implements is for implementing interfaces while extends is for inheriting from other classes. So choose wisely! ✨

If you found this blog post helpful, please consider sharing it with your fellow developers! And if you have any questions or additional insights, feel free to leave a comment below. Let's continue the discussion! 👇

Happy coding! 🎉👩‍💻👨‍💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello