Core Java instance-interface Abstraction

Source: Internet
Author: User

 

Interface Usage:

 

1. Use the interface with Polymorphism: It is divided into the status at compile time and runtime.

2. Pay attention to the consistency of objects.

3. Forced conversion.

 

 

  1. Package com;
  2. Public interface animal {
  3. }

 

  1. Package com;
  2. /***
  3. *
  4. * Birds
  5. *
  6. * @ Author Administrator
  7. *
  8. */
  9. Public class bird implements animal {
  10. Public bird (){
  11. }
  12. Public String color;
  13. Private int age;
  14. Public int getage (){
  15. Return age;
  16. }
  17. Public void setage (INT age ){
  18. This. Age = age;
  19. }
  20. Public String getcolor (){
  21. Return color;
  22. }
  23. Public void setcolor (string color ){
  24. This. Color = color;
  25. }
  26. }

 

  1. Package com;
  2. Public class samllbird extends bird {
  3. }

 

 

  1. Package com;
  2. Import java. Lang. Reflect. invocationtargetexception;
  3. Public class test {
  4. Public static void main (string [] ARGs) throws classnotfoundexception, securityexception, nosuchfieldexception, illegalargumentexception, illegalaccessexception, nosuchmethodexception, invocationtargetexception {
  5. Animal B = new bird ();
  6. B. tostring ();
  7. Bird = (BIRD) B;
  8. Bird. setcolor ("red ");
  9. System. Out. println (bird. getcolor ());
  10. System. Out. println (B = bird );
  11. Animal sb = new samllbird ();
  12. Bird BB = (BIRD) Sb;
  13. System. Out. println (SB = bb );
  14. System. Out. println (B instanceof animal );
  15. System. Out. println (bird instanceof animal );
  16. System. Out. println (SB instanceof animal );
  17. }
  18. }

Running result:

Red
True
True
True
True
True

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.