Java Wildcards (? extends) Tutorial

beginner
16 min

Java Wildcards (? extends) Tutorial

Welcome to the Java Wildcards (? extends) tutorial! In this lesson, we'll delve into one of the powerful features of Java's generics - the ? extends wildcard. Let's get started! 🎯

What are Wildcards in Java?

Wildcards are a type of placeholder that can be used in generic programming to represent unknown types. They help when working with generic types where the specific type is not known or cannot be defined.

Types of Wildcards in Java

Java supports two types of wildcards:

  1. ? (unbounded wildcard)
  2. ? extends _____ (upper bounded wildcard)

In this lesson, we will focus on the ? extends wildcard.

Understanding ? extends Wildcards

The ? extends wildcard represents any reference type that extends the specified bound. It allows you to create flexible code that works with any subclass of a specific class.

Syntax

java
ClassName<?> // Upper bounded wildcard with no explicit bound Class<? extends Superclass> // Upper bounded wildcard with explicit bound

Let's dive into a practical example to understand the ? extends wildcard better. 📝

Practical Example

Consider we have a Animal class and several subclasses like Dog, Cat, and Bird. We want to create a method that accepts an Animal object but performs different actions based on the actual subclass.

java
class Animal { // Animal class definition here } class Dog extends Animal { // Dog class definition here } class Cat extends Animal { // Cat class definition here } class Bird extends Animal { // Bird class definition here } public class WildcardExample { public static void main(String[] args) { Animal animal = new Animal(); // Create an Animal object Dog dog = new Dog(); // Create a Dog object Cat cat = new Cat(); // Create a Cat object Bird bird = new Bird(); // Create a Bird object // Using a wildcard with no explicit bound feedAnimal(animal); // Accepts any Animal or its subclass // Using a wildcard with an explicit bound (Dog) feedDog(dog); // Accepts only Dog objects } public static void feedAnimal(Animal animal) { System.out.println("Feeding the animal..."); } public static void feedDog(Dog dog) { System.out.println("Feeding the dog..."); // Perform specific actions for Dog here } }

Quiz

What will be the output of the following code when running the main() method?

java
public static void main(String[] args) { Animal animal = new Dog(); feedAnimal(animal); } public static void feedAnimal(Animal animal) { System.out.println("Feeding the animal..."); }
Quick Quiz
Question 1 of 1

What will be the output of the following code?

Now, let's see how to use the ? extends wildcard in Java.

Using ? extends Wildcards

To use the ? extends wildcard, you can replace the superclass with ? extends Superclass. This allows the method or field to accept any subclass of Superclass.

Example

java
public class AnimalExample { public static void main(String[] args) { Animal animal = new Dog(); feedAnySubAnimal(animal); Animal cat = new Cat(); feedAnySubAnimal(cat); } public static void feedAnySubAnimal(Animal animal) { System.out.println("Feeding the animal..."); if (animal instanceof Dog) { // Perform specific actions for Dog here } else if (animal instanceof Cat) { // Perform specific actions for Cat here } } }

Quiz

What will be the output of the following code when running the main() method?

java
public static void main(String[] args) { Animal bird = new Bird(); feedAnySubAnimal(bird); } public static void feedAnySubAnimal(Animal animal) { System.out.println("Feeding the animal..."); if (animal instanceof Dog) { // Perform specific actions for Dog here } else if (animal instanceof Cat) { // Perform specific actions for Cat here } else if (animal instanceof Bird) { // Perform specific actions for Bird here } }
Quick Quiz
Question 1 of 1

What will be the output of the following code?

Now you have a basic understanding of using the ? extends wildcard in Java. Remember, it's crucial to understand the context and use it wisely to create flexible, reusable code. Happy coding! 💡

Quiz

In which case would you use the ? extends wildcard in Java?

Quick Quiz
Question 1 of 1

In which case would you use the `? extends` wildcard in Java?

Keep learning and exploring new concepts on CodeYourCraft! 📝