Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================================================
- --------------------------------------------------------------------------------------------------
- [Polymorph.java]
- Polymorphism at work.
- (c) Damion 'Phr0z3n.dev' Tapper, 2015.
- Email: [email protected]
- Website: L337Stuff.Blogspot.com
- --------------------------------------------------------------------------------------------------
- ==================================================================================================*/
- public class Polymorph
- {
- public static void main(String arg[])
- {
- new Polymorph().ShowTypes();
- }
- public void ShowTypes()
- {
- MyCanvas myCanvas = new MyCanvas();
- if(myCanvas instanceof MyCanvas)
- System.out.println("myCanvas is a MyCanvas");
- if(myCanvas instanceof java.awt.Canvas) // Parent of MyCanvas
- System.out.println("myCanvas is a java.awt.Canvas");
- if(myCanvas instanceof java.awt.Component) // Grand parent of MyCanvas
- System.out.println("myCanvas is a java.awt.Component");
- if(myCanvas instanceof java.lang.Object) // Great Grand parent (of everyone in the Java family)...
- System.out.println("myCanvas is a java.lang.Object");
- if(myCanvas instanceof java.lang.Runnable) // Some other ancestor of MyCanvas
- System.out.println("myCanvas is a java.lang.Runnable");
- if(myCanvas instanceof javax.accessibility.Accessible)
- System.out.println("myCanvas is a javax.accessibility.Accessible");
- if(myCanvas instanceof java.awt.image.ImageObserver)
- System.out.println("myCanvas is a java.awt.image.ImageObserver");
- if(myCanvas instanceof java.awt.MenuContainer)
- System.out.println("myCanvas is a java.awt.MenuContainer");
- if(myCanvas instanceof java.io.Serializable)
- System.out.println("myCanvas is a java.io.Serializable");
- 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.
- java.lang.Runnable jlRun = myCanvas; // interface treated like class
- java.awt.MenuContainer jaMenuContainer = new MyCanvas();
- java.awt.Canvas jac = (java.awt.Canvas)jaMenuContainer;
- /**
- * NOTE:
- * If you are making an assignment in the direction such that the compiler can't be positive
- * it will always be correct, you will have to cast it.
- **/
- /**
- * NOTE:
- * The MyCanvas class completely inherits the implementation of all interfaces by all super-classes
- * just as if they have been implemented locally.
- *
- * NOTE TO SELF:
- * Polymorphism at its best. ;)
- *
- * DEFINE 'polymorphism':
- * The third pillar of the Object-Oriented Programming (OOP) paradigm, after 'encapsulation'
- * and 'inheritance' (and a central characteristic).
- **/
- }
- }
- class MyCanvas extends java.awt.Canvas implements Runnable
- {
- public void run()
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment