Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class JavaMainClass {
- private static AbstractClass obj;
- private static String className;
- public static void main (String args[]) throws IOException {
- /******************************************* ABSTRACT CLASS RUN IN JAVA ***************************************** START **************/
- //Open stream to input details from users
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- //Input child class name to extend (Input AbstractClassChild1 , as that is the only class extending the AbstractClass for now)
- System.out.println("Please enter the class name to instantiate: ");
- className = br.readLine();
- System.out.print("The class name enetered is: ");
- System.out.println(className);
- //instantiate the child class entered by user
- try {
- //Instantiate the child class
- //obj = new AbstractClassChild1();
- obj = (AbstractClass) Class.forName(className).newInstance();
- obj.add(3, 2);
- obj.subtract(3, 2);
- //Call method introduce of child class (if the method is overridden in the child class, then it would never call the method of the parent or abstract class)
- obj.introduce();
- //Call constructor of child class (it does not get called by newinstance(). Contructors can even have params below
- try {
- Class.forName(className).getConstructor();
- } catch (NoSuchMethodException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- }
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- /******************************************* ABSTRACT CLASS RUN IN JAVA ***************************************** END ****************/
- }
Add Comment
Please, Sign In to add comment