Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning subclass object from superclass method

I keep coming back to variants of this problem: it probably has a very simple solution, but I can't seem to figure it out...

I have a bunch of classes of the form xQuantity, e.g. DistanceQuantity, AreaQuantity, etc., which extend a class DimensionQuantity. Now you can add or subtract DistanceQuantity's or AreaQuantity's, etc., but you can't mix them, so I think I need to have (short) add, subtract, etc., methods in the subclasses, but I want to reduce any logic duplication to a minimum. However, I need to return an object of the subclass, and this seems difficult to do from the superclass method. I believe this can be done using reflection, but AFAIK you still need to do a cast at the end in the subclass method, and I am told that reflection can be expensive... The best I have come up with so far is:

In DistanceQuantity (and the other similar ones):

public DistanceQuantity() {     
}

public DistanceQuantity add(DistanceQuantity d1) {
    DistanceQuantity dn = new DistanceQuantity(); 
    Object o = super.add(dn, this, d1, DistanceUnit.REF_UNIT);
    return (DistanceQuantity) o;
}

In DimensionQuantity (minus some less relevant statements):

public Object add(DimensionQuantity dn, DimensionQuantity d1, DimensionQuantity d2, 
  AbstractUnit au) {
    dn.unit = au;
    dn.scalar = d1.scalar + d2.scalar;
    dn.units = dn.scalar;    
    return dn;   
}

Can anyone come up with leaner code - that is still type-safe? TIA

like image 417
Paul Morrison Avatar asked Aug 30 '10 19:08

Paul Morrison


People also ask

Can I access the subclass method using a superclass object?

Yes, you can call the methods of the superclass from static methods of the subclass (using the object of subclass or the object of the superclass).

What method does subclass inherit from superclass?

A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

Can a superclass object be treated as a subclass object?

ANS: c. Objects of a subclass can be treated like objects of their superclass.

Can we typecast the superclass object to subclass in Java?

You can try to convert the super class variable to the sub class type by simply using the cast operator. But, first of all you need to create the super class reference using the sub class object and then, convert this (super) reference type to sub class type using the cast operator.


1 Answers

You can use Generics like this :

public abstract class DimensionQuantity<T extends DimensionQuantity>{
    public abstract T add(T parameter);
}

and you extends it like this :

public class DistanceQuantity extends DimensionQuantity<DistanceQuantity>{
    public DistanceQuantity add(DistanceQuantity parameter){
        //Whatever
        return null;
    }
}

And for the initial question, it's a really bad idea (and a bad practice) to have a superclass which uses one of its sub-classes.


Resources :

  • Oracle.com - Generics

On the same topic :

  • Resource for learning Java generics?
like image 51
Colin Hebert Avatar answered Sep 26 '22 11:09

Colin Hebert