From the course: Advanced Java Programming

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Using wildcards in generic programming

Using wildcards in generic programming - Java Tutorial

From the course: Advanced Java Programming

Start my 1-month free trial

Using wildcards in generic programming

- Wildcards in Java can be useful in a variety of different situations. A wildcard is essentially an unknown type, and can give you more flexibility when writing methods. In this example, I have four classes. There is a class called building, a class called office, which extends building, a class called house, which also extends the building class, and finally a Main class. In the main class, I have a method called printBuildings. That takes a list of buildings as a parameter, and prints them to the terminal. In the main method, I also have a list of offices and a list of houses. However, I cannot pass either of these lists into the printBuilding methods. Because the list of type office is not a subtype of a list of type building. And nor is a list of type house. When I try and do this, I get a compile time error. One solution would be to create a new method for each different subtype of building. But a quicker and simpler solution is to use wildcards. In the list of arguments in the…

Contents