Java - parameter to override the type of an object with a specific type

EDIT: I will leave this here as an example. Read the comments for more information, but in general: DO NOT USE THIS DESIGN! It's free!

I searched for an answer for a while, but I couldn’t find anything concrete to say, you can’t, because ... or yes, you can do it the way you do it.

So, the question is, can I create an abstract method that defines the parameters of an object type, and then something to implement it with a specific parameter type as follows:

public abstract class ToBeOverriden { public Object method1 (Object parameter); public String method2 (Object parameter); public void method3 (Object parameter); } 

And then redefine it as follows:

 public class Implementation { @Override public DateTime method1 (Person parameter){ return new DateTime(); } @Override public String method2 (MotorCycle parameter){ return new DateTime(); } @Override public void method3 (String parameter){ return new DateTime(); } } 

Where Person is an object created by me. The return type can be any. Currently, I cannot do this. This does not allow me. I assume this is because my class does not extend Object. Although Object extends everything ... So ...

Or do I need to update my knowledge of Java? :)

EDIT: Added a more complex class structure.

Thanks!

+6
source share
3 answers

You will need Java Generics :

 public abstract class ToBeOverriden<E,V> { public E method (V parameter); } public class Implementation extends ToBeOverriden<DateTime,Person> { @Override public DateTime method (Person parameter){ return new DateTime(); } } 

Added:

E parameter may be skipped, the code will still compile. However, if different implementations of ToBeOverriden use different types of return data, I think it's better to save E But this is a matter of personal taste - I do not like to see Object anywhere in the code.

Added 2:

Like your update in question, for each method you will need a separate Generic type. For instance:

 public abstract class ToBeOverriden<A,B,C> { public Object method1 (A parameter); public String method2 (B parameter); public void method3 (C parameter); } 

However, as a rule, when you need such a terrible structure, then your code is designed incorrectly. In 95% of cases, one typical parameter is sufficient. In 4.99% of cases, 2 typical type parameters are sufficient.

+15
source

You can do the following:

 public abstract class ToBeOverriden<T> { public Object method (T parameter); } public class Implementation extends ToBeOVerriden<Person>{ @Override public DateTime method (Person parameter){ return new DateTime(); } } 

But you cannot do this without generation, and the problem is the argument, not the return type. Suppose you could do this without generating, then you could associate your implementation object with an interface, and you could call the method with any object as an argument, and not just a Person (which is against security like Java).

+2
source

You do :) The specification says:

Two methods have the same signature if they have the same name and argument types.

You can easily have a public Object method (Object parameter); and public Object method (Person parameter); in its class, side by side, and these will be different methods.

And yes, all classes end up extending Object .

0
source

Source: https://habr.com/ru/post/910332/


All Articles