Is it possible to "override" an interface through a proxy class? - java

I have a small set of methods from various classes that I'm exposing through a proxy-like convenience class. My issue is that one of those methods takes as an argument an instance of a class implementing an inner interface. I however, do not want to expose that interface through the original class, and would rather provide it through my proxy.
Here is an example of what I mean:
Class C1 {
public static void addSomeListener(SomeListener listener) {
// Some code
}
public interface someListener {
public void interfaceMethod();
}
}
Class C2 {
public interface someListener {
public void interfaceMethod();
}
public static void doAddListener(SomeListener listener) {
// The compiler, of course, complains here
C1.addSomeListener(listener);
}
}
I'm wondering if it's possible to somehow "override" that interface so that the interface from C2 can be exposed to the user/developer while still keeping the inner interface defined in C1 hidden.

The following should do the job:
class C2 {
public interface SomeOtherListener extends SomeListener {
public void interfaceMethod();
}
public static void doAddListener(SomeOtherListener listener) {
C1.addSomeListener(listener);
}
}

Related

Listen to (register to) a specific method of interface without implement all the methods, Java

Interfaces are a good way to simulate callbacks. However, the class that implements an interface must override all the methods of this interface.
Now, I have an interface
interface MyInterface {
void callback1();
void callback2();
void callback3();
...
void callback100();
}
I want to make a listener that registers only for callback1(). Is there a way to listen to such an event without implementing the whole interface MyInterface ?
You can mark the methods that don't need to be implemented as default and give them an empty body. For example:
public class Main {
public static void main(String[] args) {
I i = new A();
i.f();
}
}
interface I {
default void f() {}
default void g() {}
}
class A implements I {
#Override
public void f() { // only implementing "f"
System.out.println("Hello");
}
}
If you can't use Java 8 features, you can create a separate interface for each callback:
interface Callback1Listener {
void callback1();
}
interface Callback2Listener {
void callback2();
}
interface Callback3Listener {
void callback3();
}
// ...
As alternative to Java 8 default method, you can look at the Adapter classes used in Swing listeners. It is a subclass that provides empty body for all methods of the interface. It allows to distinguish cases of you want an explicit definition of all method bodies and the case of you don't want necessarily to have behavior for all of them.
For example :
interface MyCallback {
void callback1();
void callback2();
void callback3();
}
And Adapter class :
public class MyAdapter implements MyCallback {
public void callback1(){}
public void callback2(){}
public void callback3(){}
}
Now client classes may use the interface or the adapter class to implement the callback :
MyCallBack callback = new MyAdapter(){
public void callback2(){
// I implement it only
}
}
Or :
MyCallBack callback = new MyCallBack(){
// I have to implement all
public void callback1(){
// ...
}
public void callback2(){
// ...
}
public void callback3(){
// ...
}
}
This is why it's a good idea to keep interfaces as simple as possible (and preferably use existing interfaces where possible).
Your example of an interface with 100 methods is known in expert circles as "Bad Design". It almost automatically results in bad code.

C# vs JAVA instance of Interface

I am not sure how am I suppose to go about my question. It is about Android can Instantiate Interface. I am trying to do in C#. Now I am pretty sure that the rules for both Java and C# is you can't create an Instance of abstract and Interface as being said.
But I would really like to know how Android does this practice.
In Android you can do this.
public interface Checkme{
void Test();
void Test2();
}
public void myFunc(Checkme my){
//do something
}
// Now this is the actual usage.
public void Start(){
myFunc(new Checkme(){
#Override
public void Test()
{
}
#Override
public void Test2()
{
}
});
}
Actually once you press Enter on new Checkme() You will automatically get the Override methods of the Interface. Like auto Implement method of an Interface in C#.
I hope my question make sense.
C# doesn't support anonymously auto-implemented interfaces because it has delegates:
public void Foo(Func<string> func, Action action) {}
// call it somewhere:
instance.Foo(() => "hello world", () => Console.WriteLine("hello world"));
With delegates you can fill the gap and it can be even more powerful than implementing interfaces with anonymous classes.
Learn more about delegates.
This is an Anonymous Class:
public void Start(){
myFunc(new Checkme() {
#Override
public void Test() {
}
#Override
public void Test2() {
}
});
}
An anonymous class is an unnamed class implemented inline.
You could also have done it using a Local Class, but those are rarely seen in the wild.
public void Start(){
class LocalCheckme implements Checkme {
#Override
public void Test() {
}
#Override
public void Test2() {
}
}
myFunc(new LocalCheckme());
}
These both have the advantage that they can use method parameters and variables directly, as long as they are (effectively) final.
As a third option, you could do it with an Inner Class.
private class InnerCheckme implements Checkme {
#Override
public void Test() {
}
#Override
public void Test2() {
}
}
public void Start(){
myFunc(new InnerCheckme());
}
An inner class cannot access method variables (obviously because it's outside the method), but can be used by multiple methods.
Any local values from the method can however be passed into the constructor and stored as fields of the inner class, to get the same behavior. Just requires a bit more code.
If the inner class doesn't need access to fields of the outer class, it can be declared static, making it a Static Nested Class.
So, all 3 ways above a very similar. The first two are just Java shorthands for the third, i.e. syntactic sugar implemented by the compiler.
C# can do the third one, so just do it that way for C#.
Of course, if the interface only has one method, using a Java lambda or C# delegate is much easier than Anonymous / Local / Inner classes.
If I understand correcly, you're defining a class that implements an interface, and when you specify that the class implements an interface, you want it to automatically add the interface's methods and properties.
If you've declared this:
public interface ISomeInterface
{
void DoSomething();
}
And then you add a class:
public class MyClass : ISomeInterface // <-- right-click
{
}
Right-click on the interface and Visual Studio will give you an option to implement the interface, and it will add all the interface's members to the class.
you mean something like this?
pulic interface Foo{
void DoSomething();
}
public class Bar : Foo {
public void DoSomething () {
//logic here
}
}
myFunc(new Checkme(){
#Override
public void Test()
{
}
#Override
public void Test2()
{
}
});
You're passing into myFunc() something that is called an anonymous class. When it says "new Checkme() { .... }", it is defining an anonymous implementation of the Checkme interface. So, it's not an instance of the interface itself, just an instance of a type that implements it.
In C# anonymously implemented classes for Interface are not auto generated just like in java, you need to follow the below procedure to workout.
public class MyClass {
public void someMethod (string id, IMyInterface _iMyInterface) {
string someResponse = "RESPONSE FOR " + id;
_iMyInterface.InterfaceResponse (someResponse);
}
}
public interface IMyInterface {
void InterfaceResponse (object data);
void InterfaceResponse2 (object data, string x);
}
public class MyInterfaceImplementor : IMyInterface {
private readonly Action<object> actionname;
private readonly Action<object, string> actionInterfaceResponse2;
public MyInterfaceImplementor (Action<object> InterfaceResponse) {
this.actionname = InterfaceResponse;
}
public MyInterfaceImplementor(Action<object> interfaceResponseMethod, Action<object, string> interfaceResponseMethod1) {
this.actionname = interfaceResponseMethod ?? throw new ArgumentNullException(nameof(interfaceResponseMethod));
this.actionInterfaceResponse2 = interfaceResponseMethod1 ?? throw new ArgumentNullException(nameof(interfaceResponseMethod1));
}
public void InterfaceResponse (object data) {
this.actionname (data);
}
public void InterfaceResponse2(object data, string x) {
this.actionInterfaceResponse2(data, x);
}
}
Gist Source : https://gist.github.com/pishangujeniya/4398db8b9374b081b0670ce746f34cbc
Reference :

What is the correct way to override method of interface from a child interface using generics?

I have those two interfaces:
public interface ApiResultCallback {
void onSuccess(RestApi.Success<?> successResult);
void onFailure(RestApi.Failure failureResult);
}
public interface GetHappyCowsCallback extends ApiResultCallback {
void onSuccess(RestApi.Success<List<HappyCow>> successResult);
}
Where Success and Failure are:
public static class Success<T> extends ApiResult {
public T data;
}
public static class Failure extends ApiResult {
public String message;
}
I get an error in GetCleverPointsCallback interface saying that
both methods have same erasure but neither overrides the other.
What does that mean? Shouldn't the method from GetHappyCowsCallback override the method of its parent?
What I'm trying to achieve here is some kind of mapping between callbacks and their data without having to implement long mapping functions or even worse, duplicating the Success class like this:
public static abstract class Success<T> extends ApiResult {
public T data;
}
public static class ListHappyCowSuccess extends Success<List<HappyCow>> {
}
void onSuccess(RestApi.Success<?> successResult);
And
void onSuccess(RestApi.Success<List<HappyCow>> successResult);
Do not have the same signature. So the second does not override the first
What you're trying to do can be achieved by making the interface generic:
public interface ApiResultCallback<T> {
void onSuccess(RestApi.Success<T> successResult);
void onFailure(RestApi.Failure failureResult);
}
public interface GetHappyCowsCallback extends ApiResultCallback<List<HappyCow>> {
}
In fact, you probably don't need the second interface at all. Such pseudo-typedefs are even considered an anti-pattern, because the new types cannot be exchanged with their equivalents.
If I have a method like this:
void myMethod(GetHappyCowsCallback callback);
I can not pass an ApiResultCallback<List<HappyCow>> to it.
In most cases interface overriding doesn't really make sense. Unless it involves default methods:
interface InterfaceA {
public void doSomething();
}
interface InterfaceB extends InterfaceA {
#Override
public default void doSomething() {...} // Provides a default implementation
}

Returning values from child-classes

I want to be able to call the ObjectAction#firstClick in a dynamic way which has support for more classes. My goal is not to access the Test class directly and call it from there.
Keep in mind you may not use static contexts.
public abstract class ObjectAction implements AchievementListener {
public abstract void firstClick(GameObject object);
}
firstClick is what I want to call in a dynamic way..
Here is more code..
public interface AchievementListener {
}
This acts as the listener type which has child-classes such as ObjectAction, ItemAction, GroundAction etc.
Here is the enum
public enum Achievements {
TEST(new ObjectAction() {
#Override
public void firstClick(Object object) {
}
});
private static final Set<Achievements> ACHIEVEMENTS = Collections.unmodifiableSet(
EnumSet.allOf(Achievements.class));
public static Optional<AchievementListener> getListener() {
return ACHIEVEMENTS.stream().filter(a -> a.listener).findAny();
}
AchievementListener listener;
Achievements(AchievementListener listener) {
this.listener = listener;
}
}
The #getListener() function is the part I need help with, it doesn't work atm because it isnt'returning an AchievementListener, what i'm looking for is something like getListener().getObjectActions().firstClick(GameObject object); and for something like ItemActions it should be getListener().getItemActions().executeAction(Item item);
You have created an interface which doesn't declare any methods. Then you implement that interface at an abstract class which only declares that one abstract method; thus you don't need that abstract class because you are not doing anything with a necessary constructor. Read more on abstract classes and interfaces here.
What you need to do is to declare the interface like this:
public interface AchievementListener {
public void firstClick(GameObject object); // Declared the interface method
}
You can leave the abstract class now because it has no use and directly ask for a AchievementListener:
public class Test extends Achievement {
#Override
public AchievementListener process (Player player) {
return new AchievementListener () {
#Override
public void firstClick(GameObject object) {
System.out.println("This works for sure ;)");
}
};
}
}
So in short, the dynamic is in the interface, because you want that one method in all of your child classes!

Extending functionality of all implementations of an Interface?

I'm looking to create a set of functions which all implementations of a certain Interface can be extended to use. My question is whether there's a way to do this without using a proxy or manually extending each implementation of the interface?
My initial idea was to see if it was possible to use generics; using a parameterized type as the super type of my implementation...
public class NewFunctionality<T extends OldFunctionality> extends T {
//...
}
...but this is illegal. I don't exactly know why this is illegal, but it does sort of feel right that it is (probably because T could itself be an interface rather than an implementation).
Are there any other ways to achieve what I'm trying to do?
EDIT One example of something I might want to do is to extend java.util.List... Using my dodgy, illegal syntax:
public class FilterByType<T extends List> extends T {
public void retainAll(Class<?> c) {
//..
}
public void removeAll(Class<?> c) {
//..
}
}
You can achieve something like this using a programming pattern known as a 'decorator' (although if the interface is large then unfortunately this is a bit verbose to implement in Java because you need to write single-line implementations of every method in the interface):
public class FilterByType<T> implements List<T> {
private List<T> _list;
public FilterByType(List<T> list) {
this._list = list;
}
public void retainAll(Class<?> c) {
//..
}
public void removeAll(Class<?> c) {
//..
}
// Implement List<T> interface:
public boolean add(T element) {
return _list.add(element);
}
public void add(int index, T element) {
_list.add(index, element);
}
// etc...
}
Alternatively, if the methods don't need to access protected members, then static helper methods are a less clucky alternative:
public class FilterUtils {
public static void retainAll(List<T> list, Class<?> c) {
//..
}
public static void removeAll(List<T> list, Class<?> c) {
//..
}
}
What prevents you from just adding new methods to the interface?
If you can't just add the new functionality to old interface, you could consider making another interface and then an implementation which merely implements those two. Just to be clear, in code this is what I mean:
// Old functionality:
public interface Traveling {
void walk();
}
// Old implementation:
public class Person implements Traveling {
void walk() { System.out.println("I'm walking!"); }
}
// New functionality:
public interface FastTraveling {
void run();
void fly();
}
// New implementation, option #1:
public class SuperHero extends Person implements FastTraveling {
void run() { System.out.println("Zoooom!"); }
void fly() { System.out.println("To the skies!"); }
}
// New implementation, option #2:
public class SuperHero implements Traveling, FastTraveling {
void walk() { System.out.println("I'm walking!"); }
void run() { System.out.println("Zoooom!"); }
void fly() { System.out.println("To the skies!"); }
}
I think it's illegal because you can not guarantee what class T will be. Also there are technical obstacles (parent's class name must be written in bytecode, but Generics information get lost in bytecode).
You can use Decorator pattern like this:
class ListDecorator implements List {
private List decoratingList;
public ListDecorator(List decoratingList){
this.decoratingList = decoratingList;
}
public add(){
decoratingList.add();
}
...
}
class FilterByArrayList extends ListDecorator {
public FilterByAbstractList () {
super(new ArrayList());
}
}
There is a delegation/mixin framework that allows a form of this. You can define a new interface, implement a default implementation of that interface, then request classes which implement that interface but subclass from elsewhere in your hierarchy.
It's called mixins for Java, and there's a webcast right there that demonstrates it.
I'm afraid it's not clear what do you want to get.
Basically, I don't see any benefit in using 'public class NewFunctionality<T extends OldFunctionality> extends T' in comparison with 'public class NewFunctionality extends OldFunctionality' ('public class FilterByType<T extends List> extends T' vs 'public class FilterByType<T> implements List<T>')

Categories

Resources