Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending a class such that any parent class can be cast to it, in Java

I have a feeling this is impossible, but if not it would be very useful.

I’m trying to extend a parent class in a way that the child class only has new methods, no new constructors, no new fields. So the underlying data structure of the child class is identical to the parent class. This tends to occur when I want to give added functions to a built in java class (e.g. Vector3d). Given that the underlying data is identical is it possible in any way to downcast an object initialized as the parent class to the child class so I can use the added functionality. As an example of what I mean see below

import javax.vecmath.Vector3d;

public class Vector3dPlus extends Vector3d{
    //same fields, same constructors as Vector3d

    public double extraMethod(){
        return x+y+z;
    }
}

Try to use the new method added to Vector3d

import javax.vecmath.Vector3d;     

public class Test {

    public static void main(String[] args) {
        Vector3d basic=new Vector3d(1,2,3);

        useExtraMethod(basic); //this line correctly raises an exception, but is there a way around that
    }

    public static void useExtraMethod(Vector3dPlus plus){
        System.out.println(plus.extraMethod());
    }
}

Clearly java gets upset with this because usually we can't guarantee that Vector3dPlus methods will work with all Vector3d. But is there anyway I can say to java that the underlying data structures are the same and so allow all downcasting from all Vector3d to Vector3dPlus.

The current way I deal with this is to put all the extra methods in a generic utilities class, but that's obviously a bit horrible

like image 245
Richard Tingle Avatar asked Apr 29 '13 09:04

Richard Tingle


People also ask

Can you cast a parent class to child in Java?

Therefore, the child can be implicitly upcasted to the parent. However, a parent may or may not inherits the child's properties. However, we can forcefully cast a parent to a child which is known as downcasting.

Can you cast parent class to subclass?

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.

Can a method extend a class Java?

Final method in java can be extended but alongside the main concept to be taken into consideration is that extend means that you can extend that particular class or not which is having final method, but you can not override that final method.

Can you cast object to subclass Java?

In java object typecasting one object reference can be type cast into another object reference. The cast can be to its own class type or to one of its subclass or superclass types or interfaces.


1 Answers

You can achieve it with method overloading and a copy constructor:

public class Vector3dPlus extends Vector3d {
    public Vector3dPlus(Vector3d vector) {
        super( ... ); // parameters from vector
    }

    // rest of your Vector3dPlus code
}

and in your test class, overload the method:

    public class Test {

        public static void useExtraMethod(Vector3d vector) {
            useExtraMethod(new Vector3dPlus(vector));
        }

        public static void useExtraMethod(Vector3dPlus plus){
             System.out.println(plus.extraMethod());
        }

        public static void main(String[] args) {
           Vector3d basic=new Vector3d(1,2,3);
           useExtraMethod(basic); // this line works now
        }
    }
like image 83
jlordo Avatar answered Oct 06 '22 01:10

jlordo