Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Overriding Object Type Parameter With Concrete Type

Tags:

java

EDIT: I will leave this here as an example. Read the comments for more information but generally: DON'T USE THIS DESIGN! It's BAD!

I searched for an answer for a while now, but couldn't find anything really specific saying, no you can't because... or yes you can that's how you do it..

So the question is, Can I create an abstract method defining Object type parameters and then have something implement it with a concrete Type of parameter like this:

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

And then override it with this:

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. Return Type can be whatever. Currently I can't do this. It doesn't let me. My guess is that this is because my Class doesn't extend Object. Although everything extends Object... So...

Or do I need to refresh my Java knowledge? :)

EDIT: Added a more complex class structure.

Thanks!

like image 593
Hannibal Avatar asked Mar 09 '12 10:03

Hannibal


People also ask

Can we change parameter type in method overriding in Java?

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.

Can overriding methods have different parameters?

Overloading occurs when two or more methods in one class have the same method name but different parameters.

What is Java@ override?

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.


1 Answers

You would need to use 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 can be ommited, the code will still compile. However, if different implementations of the ToBeOverriden will use different return types, I think it's better to retain E. But that's a matter of personal taste - I don't like seeing Object anywhere in code.

Added 2:

As about your update in the question, you would need to have a separate Generic type for every method. For example:

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

However, usually, when you need such a horrible structure - then your code is designed the wrong way. In 95% cases 1 generic type parameter is enough. In 4.99% cases 2 generic type parameters are enough.

like image 174
bezmax Avatar answered Oct 14 '22 10:10

bezmax