Suppose I have an interface called Interface, and a concrete class called Base, which, to make thing a bit more complicated, has a ctor that requires some arguments.
I’d like to create an anonymous class that would extend Base and implement Interface.
Something like
Interface get()
{
return new Base (1, "one") implements Interace() {};
}
That looks reasonable to me, but it doesn’t work!
(P.S: Actually, the Interface and Base are also generic classes :D. But I’ll ignore that for now)
This scenario makes little sense to me. Here’s why: assume class
Baseis this:and
Interfaceis this:Now you want to create an anonymous class that would be like this:
this would mean that
MyClassinherits (or possibly overrides) methodbarfromBaseand must implement methodfoofromInterface. So far so good, your class has these two methods either way.Now, think what happens when you are returning an object of type
Interfacefrom your method: you only get the functionality thatInterfaceoffers, namely methodfoo.baris lost (inaccessible). This brings us down to two cases:Baseis useless because you do not get to use its methods, since the new object is seen as anInterface.Interfacealso has a methodfoo, but that would mean thatBaseitself should implementInterface, so you can just extendBasedirectly: