In addition to the overridden technology, the inheritance system allows sub-classes to behave differently from the parent class. Reflection is also acceptable, and reflection does not require overridden for inheritance.
/*** This small example is more dramatic than it seems-it contains each of the * steps previusly mentioned. the printName method examines the object for its * class (this. getClass ()). in doing so, the demo-of what to print is made by del-* egating to the object's class. the method acts on this demo-by printing the * returned name. without being overridden, the printName method behaves di Ffer-* ently for each subclass than it does for HelloWorld. the printName method is flex-* visible; it adapts to the class that inherits it, causing the change in behavior. as we * build our examples in scope and complexity, we will show you more ways to * attain flexibility using reflection. ** in addition to the overridden technology, the inheritance system allows sub-classes to behave differently from the parent class. Reflection is also acceptable, and reflection does not require overridden for inheritance. ** @ Author doctor ** @ time November 28, 2014 8:54:05 */public class GetClassPractice {public static void main (String [] args) {new HelloWorld (). printName (); new HelloWorldChild1 (). printName (); new HelloWorldChild2 (). printName (); System. out. println (new HelloWorld (). getClassType (); System. out. println (new HelloWorldChild1 (). getClassType (); System. out. println (new HelloWorldChild2 (). getClassType (); System. out. println ("interface"); new Hello1 (). printName (); new Hello2 (). printName ();} public static class HelloWorld {public void printName () {System. out. println (getClass (). getName ();} public Class
GetClassType () {return getClass () ;}} public static class HelloWorldChild1 extends HelloWorld {} public static class HelloWorldChild2 extends HelloWorld {} public interface Hello {public default void printName () {System. out. println (getClass (). getName () ;}} public static class Hello1 implements Hello {} public static class Hello2 implements Hello {}}
Com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorld
Com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorldChild1
Com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorldChild2
Class com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorld
Class com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorldChild1
Class com. github. ebook. javarefectioninaction. GetClassPractice $ HelloWorldChild2
Interface
Com. github. ebook. javarefectioninaction. GetClassPractice $ Hello1
Com. github. ebook. javarefectioninaction. GetClassPractice $ Hello2