rajamit872

Static Execution flow in case of Inheritence

Aug 23rd, 2014
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. //order of execution when we extends a class ....
  2. //base class is super class
  3. //child class is called subclass...
  4. //order of execution of main method is from the sub class...if their is no main method in sub class then their
  5. //is the execution of main method of the super class execute.
  6. //if their is main method in sub class then no main method of super class will execute.
  7.  
  8.  
  9.  
  10. class A1{
  11. static int a = m1();
  12. static int m1(){
  13. System.out.println("a::" + a);
  14. System.out.println("Static m1() method");
  15. return 10;
  16. }
  17.  
  18. static{
  19. System.out.println("Static Block");
  20. }
  21.  
  22. public static void main(String args[]){
  23. System.out.println("A1 main() method");
  24. }
  25. }
  26.  
  27.  
  28.  
  29. class B1 extends A1{
  30. static int b=m2();
  31.  
  32. static int m2(){
  33. System.out.println("Class B1 and method is m2()");
  34. return 15;
  35. }
  36.  
  37. static {
  38. System.out.println("Static Block of B1 class i.e Sub Class");
  39. }
  40.  
  41. public static void main(String args[]){
  42. System.out.println("B1 main() method");
  43. System.out.println("A::"  + a);
  44. System.out.println("B::" + b);
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment