Phr0zen_Penguin

Polymorphism - Java

Feb 14th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. /*==================================================================================================
  2.   --------------------------------------------------------------------------------------------------
  3.   [Polymorph.java]
  4.   Polymorphism at work.
  5.  
  6.         (c) Damion 'Phr0z3n.dev' Tapper, 2015.
  7.         Email: [email protected]
  8.         Website: L337Stuff.Blogspot.com
  9.   --------------------------------------------------------------------------------------------------
  10.   ==================================================================================================*/
  11. public class Polymorph
  12. {
  13.     public static void main(String arg[])
  14.     {
  15.         new Polymorph().ShowTypes();
  16.     }
  17.  
  18.     public void ShowTypes()
  19.     {
  20.         MyCanvas    myCanvas = new MyCanvas();
  21.  
  22.             if(myCanvas instanceof MyCanvas)
  23.                 System.out.println("myCanvas is a MyCanvas");
  24.             if(myCanvas instanceof java.awt.Canvas) // Parent of MyCanvas
  25.                 System.out.println("myCanvas is a java.awt.Canvas");
  26.             if(myCanvas instanceof java.awt.Component) // Grand parent of MyCanvas
  27.                 System.out.println("myCanvas is a java.awt.Component");
  28.             if(myCanvas instanceof java.lang.Object) // Great Grand parent (of everyone in the Java family)...
  29.                 System.out.println("myCanvas is a java.lang.Object");
  30.             if(myCanvas instanceof java.lang.Runnable) // Some other ancestor of MyCanvas
  31.                 System.out.println("myCanvas is a java.lang.Runnable");
  32.             if(myCanvas instanceof javax.accessibility.Accessible)
  33.                 System.out.println("myCanvas is a javax.accessibility.Accessible");
  34.             if(myCanvas instanceof java.awt.image.ImageObserver)
  35.                 System.out.println("myCanvas is a java.awt.image.ImageObserver");
  36.             if(myCanvas instanceof java.awt.MenuContainer)
  37.                 System.out.println("myCanvas is a java.awt.MenuContainer");
  38.             if(myCanvas instanceof java.io.Serializable)
  39.                 System.out.println("myCanvas is a java.io.Serializable");
  40.  
  41.         java.awt.Canvas         jaCanvas = myCanvas; // NOTE: No way to fool Java about what its types are... people have tried and all they got were exceptions.
  42.         java.lang.Runnable      jlRun = myCanvas; // interface treated like class
  43.         java.awt.MenuContainer  jaMenuContainer = new MyCanvas();
  44.         java.awt.Canvas         jac = (java.awt.Canvas)jaMenuContainer;
  45.         /**
  46.          * NOTE:
  47.          * If you are making an assignment in the direction such that the compiler can't be positive
  48.          * it will always be correct, you will have to cast it.
  49.          **/
  50.  
  51.         /**
  52.          * NOTE:
  53.          * The MyCanvas class completely inherits the implementation of all interfaces by all super-classes
  54.          * just as if they have been implemented locally.
  55.          *
  56.          * NOTE TO SELF:
  57.          * Polymorphism at its best. ;)
  58.          *
  59.          * DEFINE 'polymorphism':
  60.          * The third pillar of the Object-Oriented Programming (OOP) paradigm, after 'encapsulation'
  61.          * and 'inheritance' (and a central characteristic).
  62.          **/
  63.     }
  64. }
  65.  
  66. class MyCanvas extends java.awt.Canvas implements Runnable
  67. {
  68.     public void run()
  69.     {
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment