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! 🎯
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.
Java supports two types of wildcards:
In this lesson, we will focus on the ? extends wildcard.
? extends WildcardsThe ? 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.
ClassName<?> // Upper bounded wildcard with no explicit bound
Class<? extends Superclass> // Upper bounded wildcard with explicit boundLet's dive into a practical example to understand the ? extends wildcard better. 📝
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.
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
}
}What will be the output of the following code when running the main() method?
public static void main(String[] args) {
Animal animal = new Dog();
feedAnimal(animal);
}
public static void feedAnimal(Animal animal) {
System.out.println("Feeding the animal...");
}What will be the output of the following code?
Now, let's see how to use the ? extends wildcard in Java.
? extends WildcardsTo use the ? extends wildcard, you can replace the superclass with ? extends Superclass. This allows the method or field to accept any subclass of Superclass.
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
}
}
}What will be the output of the following code when running the main() method?
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
}
}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?
In which case would you use the `? extends` wildcard in Java?
Keep learning and exploring new concepts on CodeYourCraft! 📝