What is the difference between
CoffeeInterface<Beverage> cup1 = new Coffee<Beverage>()

and

Coffee<Beverage> cup2 = new Coffee<Beverage>()

Coffee is a class that extends the CoffeeInterface
Is cup1 an object or can it be used as one.
Are there any differences betweeen cup1 and cup2?

Yes, cup1 is an object (or rather it's a variable referring to an object - just like cup2).

The biggest difference between cup1 and cup2 is that, if the Coffee class defines public methods in addition to those declared in CoffeeInterface, you will be able to call those on cup2, but not on cup1.

Another difference is that, if you have a method m that has one overload for Coffee and one for CoffeeInterface, then m(cup1) will call the overload for the interface while m(cup2) will call the overload for the class.

commented: Good answer +15

(Edit) deleted text of post that was based on an incorrect reading of sepp2k's post. My mistake. He is right.

I think there's been a misunderstanding. I didn't say that the method m should be defined in the interface. The method can be defined in any class you want as long as it has two overloads: one for Coffe and one for CoffeeInterface.

(Edit)
I'm very sorry. I just re-read your post and it was completely correct. I just didn't read it properly. My mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.