Creating an instance of a subclass extending an abstract class (Java)

In Java, is there a way to instantiate any class that extends abstract class A, inside a member method of class A? Classes that extend the abstract class A will return their instances using this method, but I do not want to implement the same method in all subclasses using "return this ();" kind of line.

EDIT: Sorry for the brief explanation. My application has an interface called "Application" and it has a getInstance () method that returns the type of application. There is an abstract class called AbstractApplication, which is a convenience class for the Application interface to implement, but only the interface is displayed in other applications. In some other applications, there will be a search for application objects, and this search will return the type of application (interface), rather than a specific implementation. Now here is my question; Is there a way to implement getInstance () in the AbstractApplication class so that subclasses do not need to implement this method?

+4
source share
5 answers

Yeap. This is pretty easy (if I do not understand)

You should use the Prototype template template (or its variant, which I will show here)

This is useful if you do not know what the factory class may be prior to execution. Unlike AbstractFactory, where you can have different subclasses creating new types, but you can choose one based on certain conditions.

With a prototype, you can simply get the β€œoriginal” object (prototype) entered into your application (due to the full Injection Dependency futures structure or a simple class name), and then create new instances.

Here is a code example showing how to do this with a change (not using the clone method, but newInstance )

 public abstract class Application { public Application newInstance() { try { return this.getClass().newInstance();//this creates an instance of the subclass } catch( InstantiationException ie ){ throw new RuntimeException( ie ); } catch( IllegalAccessException iae ){ throw new RuntimeException( iae ); } } public String toString() { return "This is the class named: \""+ this.getClass().getSimpleName()+"\""; } } // subclasses doesn't repeat the creation part // they just have to override specific methods. class FirstClass extends Application {} class SecondClass extends Application {} class ThirdClass extends Application {} 

And the rest of your code can be programmed into the Application interface:

 public void doSomethingWith( Application application ) { System.out.println( application.toString() ); } public void startApplication( Application app ) { // etc etc } 

Whenever you need a new instance, you simply call:

 Application newApp = original.newInstance(); 

And that will create the correct type.

As you can see, the subclasses do not indicate how to create new subclasses, all in the base class.

Calling the newInstance method will always create a new instance of the same type.

+9
source

If a superclass is aware of its subclasses, this indicates poor design.

The usual way to achieve something like this is to have a protected abstract method that subclasses must implement in order to return a result corresponding to a particular subclass.

+6
source

Something like that?

  abstract class Abs{ public <T extends Abs> T getInstance(Class<T> clazz) throws InstantiationException, IllegalAccessException { return clazz.newInstance(); } } 

Although this does not guarantee that you will get an instance of the class from which you are calling it. To do this, you still need to do something like:

 class Abs1 extends Abs { public Abs1 getInstance() { return super.getInstance(Abs1.class) } } 

therefore there are not many improvements.

I think the conclusion here is that you will have much less code and much less headaches if you simply declare an abstract method tag in the parent element and implement it with new WhateverClassImIn() in each class that extends it. You can (possibly) do it the way you want, but it will not be worth the effort.

+1
source

I'm a little rusty in my Java, but I believe that reflexively executing code in a superclass (in this example, class A) will consider it part of the subclass.

Example:

 public abstract class A { public abstract void Something(); public A Create() { Class me = this.getType(); // Returns a Reflective "Class" object for the SUB-Type // Use this object to reflectively instantiate a new instance, cast it as an A object obj = ReflectiveInstantiationHere(ItsBeenAWhile, SoGoogleThisPlease); return (A)obj; } } public class B extends A { public void Something() { A newInstanceOfB = Create(); } } 

You can return the return value from A to B later after checking the course types :)

+1
source

You can do this, but only with hackers. I am sure that other people will tell the details.

This is a bit weird design. You do not have to worry about specific implementations - stick with interfaces. The best thing you can do without changing the design:

 protected abstract A createCompatibleInstance(); 

Have subclasses.

But yuck.

0
source

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


All Articles