Java learning Experience-summarizing Java abstract classes and interfaces

Source: Internet
Author: User

*--summarize Java abstract classes and interfaces
* 1. Abstract class:
* 1 Abstract class can modify the method, modify the class, but cannot modify the property, cannot be instantiated
* 2 abstract class can be inherited, can only be single inheritance, inherit abstract class must implement abstract method
* 3 abstract class cannot be modified with static, Fina,private
* 4 abstract class is polymorphic
*
* 2. Interface:
* 1 "interface is a special kind of abstract class, cannot be instantiated
* 2 The properties of the interface declaration The default keyword is public abstract static
* 3 The method of the interface declaration The default keyword is public abstract
* 4 The interface can be inherited, the interface can be inherited multiple, can also inherit multiple
* 5 "interface is polymorphic
*

 PackageCome.java.zhangfan.test3; Public classTest { Public Static voidMain (string[] args) {Students s=NewStudents (); T1 (s);//Output---woshi StudentT T=NewT ();    T (t); }    //the polymorphism of abstract classes     Public Static voidT1 (person p) {p.t (); }       //the polymorphism of the interface     Public Static voidt (Runner p) {p.run ();//Output--- Woshi Run    }}//application of abstract class//declaring abstract ClassesAbstract classperson{//the modifier for field I is not legal; only public, protected, private, static, end state, transient, or variable are allowed.//property cannot be overwritten//abstract int i; //abstract methods in type person T can only set one of the visual modifiers "public" or "protected"//If static then means that you can call the method without creating an object directly, conflict with Abstact//abstract static public void T (); //abstract methods in type person T can only set one of the visual modifiers "public" or "protected"//itself on the conflict, does not explain//abstract Final public void T (); //abstract methods in type person T can only set one of the visual modifiers "public" or "protected"//private methods cannot be overwritten//abstract private void T ();          Abstract  Public voidT ();//True}//declare a student class to inherit the abstract class and override the T methodclassStudentsextendsperson{ Public voidT () {System.out.println ("Woshi Student"); }}//application of the interfaceInterfacerunner{ Public Abstract voidrun ();}Interfaceswimmer{voidswim ();}InterfaceFlier {voidfly ();}//interfaces can inherit multipleInterfaceTextendsrunner,swimmer,flier{}//all inherited methods must be declaredclassT1Implementst{@Override Public voidrun () {//TODO Auto-generated method stubs} @Override Public voidswim () {//TODO Auto-generated method stubs} @Override Public voidFly () {//TODO Auto-generated method stubs           }   }//interfaces can be multi-referenced//all inherited methods must be declaredclassTImplementsflier,runner,swimmer{@Override Public voidFly () {//TODO Auto-generated method stubsSystem.out.println ("Woshi Fly"); } @Override Public voidswim () {//TODO Auto-generated method stubsSystem.out.println ("Woshi Swim"); } @Override Public voidrun () {//TODO Auto-generated method stubsSystem.out.println ("Woshi Run"); }   }

Java learning Experience-summarizing Java abstract classes and interfaces

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.