"Hi! I'm going to continue Ellie's lesson on generics. Ready to listen?"

"Yes."

"Then let's begin."

"The first thing you need to know is that a class's methods can also have their own type parameters."

"I know."

"No, I specifically mean their own type parameters:"

Example
class Calculator
{
  T add(T a, T b); // Add
  T sub(T a, T b); // Subtract
  T mul(T a, T b); // Multiply
  T div(T a, T b); // Divide
}

"These type parameters pertain specifically to the methods. The class doesn't have parameters. You could even declare these methods static and call them without an object."

"I see. The point of the type parameters in methods is the same as for classes?"

"Yes. But there's something new."

"As you already know, you can use a wildcard in the type declaration. Then imagine the following situation:"

Example 1
public void doSomething(List<? extends MyClass> list)
{
 for(MyClass object : list)
 {
  System.out.println(object.getState()); // Everything works well here.
 }
}

"But what if we want to add a new item to the collection:"

Example 2
public void doSomething(List<? extends MyClass> list)
{
 list.add(new MyClass()); // Error!
}

"The problem is that in the general case the doSomething method may be passed a List whose elements are not MyClass objects, but rather objects of any subclass of MyClass. But you can't add MyClass objects to such a list!"

"Ah. So, what can be done about that?"

"Nothing. In this situation, you can't do anything. But this gave Java's creators something to think about. And they came up with a new keyword: super."

"The syntax looks almost the same:"

List<? super MyClass> list

But there's an important distinction between extends and super.

"«? extends T» means that the class must be a descendant of T."

"«? super T» means that the class must be a ancestor of T."

"Holy moly. So where is this used?"

"«? super T» is used when a method involves adding to a collection of T objects. In this case, it could be a collection of T objects or any ancestor of T."

"Ah. A T object can be assigned to a reference variable whose type is any of T's ancestors"

"Honestly, this approach is not used very often. What's more, it has a shortcoming. For example:"

Examples
public void doSomething(List<? super MyClass> list)
{
 for(MyClass object : list) // Error!
 {
  System.out.println(object.getState());
 }
}
public void doSomething(List<? super MyClass> list)
{
 list.add(new MyClass()); // Everything works well here.
}

"Now the first example doesn't work."

"Since list could even be a List<Object> (Object is MyClass's topmost superclass), we're essentially writing the following invalid code:"

Example 1
List<Object> list; 

for(MyClass object : list) // Error!
{ 
 System.out.println(object.getState()); 
}

"I see. Thanks for the interesting lesson."

"You're welcome."

undefined
4
Task
Java Core, level 5, lesson 2
Locked
OOP: Arrange interfaces
Clothing can (and should, perhaps) be worn, sold, and bought, preferably at a discount. Let's tell the world. We'll add the Movable, Sellable, and Discountable interfaces to the Clothes class, if possible. And then we'll implement their methods (big surprise!).
undefined
4
Task
Java Core, level 5, lesson 2
Locked
OOP: Animal inheritance
Next to a mouse, even a goose is enormous. But next to a dragon... In this task, we have geese (Goose class) and dragons (Dragon class). As well as their ancestors, BigAnimal and SmallAnimal classes. Guess for yourself whose ancestor is whose. Then override the String getSize() method for Goose and Dragon so that they display strings about the animal's size.
undefined
8
Task
Java Core, level 5, lesson 2
Locked
OOP: Cars
Let's categorize cars. Create classes for affordable and expensive cars, and then make the Ferrari and GeoMetro classes inherit them as you see fit (but please note that the CodeGym code validator has a definite opinion on the matter). We'll implement printlnDesire methods in these classes that will tell us the secret desires of their drivers.
undefined
16
Task
Java Core, level 5, lesson 2
Locked
OOP: Books
In this task, we will create books written by the earthly authors Mark Twain and Agatha Christie. Using the MarkTwainBook and AgathaChristieBook classes, naturally, which inherit Book. You need to implement all the abstract methods in these classes. And in the parent class, implement the getOutputByBookType method.
undefined
8
Task
Java Core, level 5, lesson 2
Locked
OOP: Fix inheritance problems
Let's investigate some body parts. First, we'll check whether bones are present. You need to fix the containsBones method and all associated logic so that, without changing the program's behavior, it returns an Object: "Yes" instead of true, and "No" instead of false.