aquaballoon

Java - Anonymous Inner Class

Jul 15th, 2013
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. package com.peter.inheritance;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         new Son().func1();  // Anonymous Inner Class
  7.        
  8.         Son ObSon = new Son();
  9.         ObSon.func2();
  10.        
  11.         new Son().func2();
  12.        
  13.         new Son() {         // Anonymous Inner Class
  14.             public void func2() {      // Overriding func2()
  15.                 System.out.println("Main");
  16.             }
  17.         }.func2();
  18.     }
  19. }
  20.  
  21. -----------------------------------------
  22. package com.peter.inheritance;
  23.  
  24. public interface Father {
  25.     String data = "Father";
  26.     abstract void func();
  27. }
  28.  
  29. -----------------------------------------
  30. package com.peter.inheritance;
  31.  
  32. public class Son implements Father {
  33.     public void func(){};   // Needed for override!!
  34.    
  35.     public void func1() {
  36.         new Father() {      // Anonymous Inner Class
  37.             public void func() {
  38.                 System.out.println(data);
  39.             }
  40.         }.func();
  41.     }                       // Description of Method "test()"
  42.  
  43.     public void func2(){
  44.         System.out.println("Son");
  45.     }
  46.    
  47. }
Advertisement
Add Comment
Please, Sign In to add comment