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.

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.