Forums Register Login

Interface implementation problem

+Pie Number of slices to send: Send
In my application, my concrete class(MyClass) needs to implement a interface(ParentInterface).
There is only one method in MyInteface(MyMethod())

Also it is required to extend class(ParentClass) having only
one static method with same name(static MyMethod()).
Method in ParentClass is static.
I need to override the method in MyClass.

When i am trying to do this, compiler complains that MyMethod present in MyInterface is not defined which is non-static method.
Since i have already defined static MyMethod, another method with same name is restricted.
I can't manipulate ParentClass or ParentInterface.

Is there any way to get out of this situation??

[ October 23, 2007: Message edited by: Saurabh Gupta ]

[ October 23, 2007: Message edited by: Saurabh Gupta ]
[ October 23, 2007: Message edited by: Saurabh Gupta ]
+Pie Number of slices to send: Send
There's no way out without relaxing one of your criteria -- i.e., either the class doesn't directly extend the parent, or doesn't directly implement the interface.

One thing you could do is extend the parent, but don't implement the interface; then provide a method named something like "asParentInterface()" that returns an instance of another class (perhaps an inner class) which implements the interface and the method, using the data in your main class as needed.
+Pie Number of slices to send: Send
The problem really has nothing to do with ParentClass, it's the fact that you must implement the method defined in the interface, and declaring it static does not fulfill that requirement.

And, as you may see on this forum - it's not possible to override static methods. Overriding implies dynamic dispatch, and static do not have dynamic dispatch - they have no context in which to dynamically choose the implementation.
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 649 times.
Similar Threads
asking about interfaces
Why the interviewer challenged me?
Can someone explain this to me?
Sorting arrays
How a static main method, calls other non-static member methods of its class?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:04:41.