Java subclass has a way to return its class

Well, maybe this is a stupid question. But I'm just wondering if this can be done in java.

abstract public class ParentClass<T> { 
  abstract public T getTest();
}

in subclass

public class SubClass extends ParentClass<MyObject> {
  public MyObject getTest() {
    // I can return the object with class MyObject
    return null;
  }
}

My question is: can I return a class type in a child method? I mean, can this be done by adding some code to the ParentClass, so I can do it below?

for instance

public class Sub1Class extends parentClass<Object1> {
  public Object1 getTest() { }
  // I want to have a method that return it class in the superclass
  public Sub1Class getItClassObject() { }
}

another example

public class Sub2Class extends parentClass<Object2> {
  public Object2 getTest() { }
  // I want to have a method that return it class in the superclass
  public Sub2Class getItClassObject() { }
}

one example again

public class Sub3Class extends parentClass<Object3> {
  public Object3 getTest() { }
  // I want to have a method that return it class in the superclass
  public Sub3Class getItClassObject() { }
}

if you see, the getItClassObject method in Sub1Class, Sub2Class and Sub3Class will follow its class. But I don’t want to add the same method for each subclass, I just want to add some code (if possible) in ParentClasss, so in the subclass I can just call getItClassObject directly without writing all the code in each subclass.

Usually I add a method to ParentClass as follows.

abstract public class ParentClass<T> {
  abstract public T getTest();
  public Object getItClassObject() { }
}

, : (

Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = (Sub1Class) sub1Class.getItClassObject();

Sub2Class sub2Class = new Sub2Class();
Sub2Class after2Cast = (Sub2Class) sub2Class.getItClassObject();

, java. , .

+3
4

, , . :

abstract class A {
    public abstract A getA();
}

class B extends A {
    // Declared to return a B, but it still properly overrides A method
    @Override
    public B getA() {
        return new B();
    }
}

class C extends A {
    // Declared to return a B, but it still properly overrides A method
    @Override
    public C getA() {
        return new C();
    }
}

, A , getA() A. , .

+3

, , , Object.getClass() , . :

public abstract class ParentClass<T> {
  public abstract T getTest();
}

class SubClassString extends ParentClass<String> {
   public String getTest() {
      return "";
   }
}

class SubClassInteger extends ParentClass<Integer> {
   public Integer getTest() {
      return Integer.valueOf(0);
   }
}

getClass()

  public static void main(String[] args) {
      SubClassString subString = new SubClassString();
      // displays "class SubClassString"
      System.out.println(subString.getClass()); 

      SubClassInteger subInteger = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(subInteger.getClass());

      ParentClass<?> parentInstance = new SubClassInteger();
      // displays "class SubClassInteger"
      System.out.println(parentInstance.getClass());  
  }
+1

, , - , , ( , "" ). :

public abstract class ParentClass<T,U> { 
    abstract public T getTest();
    abstract public U getItClassObject();
}

They define your subclass as follows:

public class Sub1Class extends ParentClass<Object1,Sub1Class> {
    public Object1 getTest() { }
    public Sub1Class getItClassObject() { }
}

Then you can do what you want without type casting:

Sub1Class sub1Class = new Sub1Class();
Sub1Class after1Cast = sub1Class.getItClassObject();
+1
source

If your objects have no-arg constructors (or some consistent constructor form for all of them), you can use reflection to do this. Some pseudo code will be

public class MyClass {

    public MyClass instantiateType() {
       Class<?> actualClass = getClass();
       return actualClass.newInstance();
    }

}

This uses the class runtime type, so subclasses return their type. This only works for the no-arg constructor.

0
source

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


All Articles