Guest User

Untitled

a guest
Jul 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. public class JavaMainClass {
  6.  
  7. private static AbstractClass obj;
  8. private static String className;
  9.  
  10. public static void main (String args[]) throws IOException {
  11.  
  12. /******************************************* ABSTRACT CLASS RUN IN JAVA ***************************************** START **************/
  13.  
  14. //Open stream to input details from users
  15. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.  
  17. //Input child class name to extend (Input AbstractClassChild1 , as that is the only class extending the AbstractClass for now)
  18. System.out.println("Please enter the class name to instantiate: ");
  19. className = br.readLine();
  20.  
  21. System.out.print("The class name enetered is: ");
  22. System.out.println(className);
  23.  
  24. //instantiate the child class entered by user
  25. try {
  26. //Instantiate the child class
  27. //obj = new AbstractClassChild1();
  28. obj = (AbstractClass) Class.forName(className).newInstance();
  29. obj.add(3, 2);
  30. obj.subtract(3, 2);
  31.  
  32. //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)
  33. obj.introduce();
  34.  
  35. //Call constructor of child class (it does not get called by newinstance(). Contructors can even have params below
  36. try {
  37. Class.forName(className).getConstructor();
  38. } catch (NoSuchMethodException e) {
  39. e.printStackTrace();
  40. } catch (SecurityException e) {
  41. e.printStackTrace();
  42. }
  43.  
  44. } catch (InstantiationException e) {
  45. e.printStackTrace();
  46. } catch (IllegalAccessException e) {
  47. e.printStackTrace();
  48. } catch (ClassNotFoundException e) {
  49. e.printStackTrace();
  50. }
  51.  
  52. /******************************************* ABSTRACT CLASS RUN IN JAVA ***************************************** END ****************/
  53.  
  54. }
Add Comment
Please, Sign In to add comment