Java: Method to instantiate a particular sub class of an abstract class?

Yes, if all the constructors receive the same parameter (in your example, no paramaters) you can do Class clazz = Class. ForName("fully.qualified.class. Nane"); A a = (A) clazz.newInstace() Both methods can throw various exceptions, so you need to add some catch blocks.

Yes, if all the constructors receive the same parameter (in your example, no paramaters) you can do Class clazz = Class. ForName("fully.qualified.class. Nane"); A a = (A) clazz.newInstace(); Both methods can throw various exceptions, so you need to add some catch blocks.

For e. G Base class constructor is A(String str){} and sub-classes constructors are like A1(String str, String str2){super(str);} – porsh Sep 14 at 20:03 Then you need to call something like Constructor c = Clazz. GetConstructor( new Object {String.

Class} ); which returns the constructor that takes a String as a parameter. And then A a = c. NewInstance( new Object"your_string" ).

Read a bit about Java Reflection, it's the feature in java which allows to open a class in runtime and do some interesting stuff (like calling private methods :p). – Augusto Sep 14 at 20:07 Thanks for the pointer! Java Reflection is indeed cool, just what I wanted :) – porsh Sep 15 at 6:26.

What about doing something like return (A)Class. ForName(runtimeClassName).newInstance(); with appropriate error handling?

Thanks! This would work for me. My bad :-/ – porsh Sep 14 at 19:40.

Look in the Java API at java.lang. Class, methods forName(String) and newInstance().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions