Abstract classes and Interfaces in Java

Source: Internet
Author: User

1. Abstract class/**1.  Abstract methods must exist in the abstract class, abstract methods and abstract classes need to be abstract!2.  abstract class cannot instantiate .3.  if there are multiple abstract methods in the abstract class, and its subclasses inherit only an abstract method, The subclass is an abstract class, so it cannot be powerful. 4.  abstract class can only be used to cover all of its abstract methods to create sub-class objects! 5.  Abstract classes can have non-abstract methods *///The subclass does not overwrite the parent class Func1 abstract method, so the subclass is abstract class, cannot be instantiated. Compilation failed Abstract class person{    abstract void func ();     abstract void func1 ();     void func2 () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method 2! ");             };} Class student extends person{    public void func () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method! ");         }}class abstractdemo{    public  static void main (String[]args) {        student st  =new student ();//subclass is abstract class, cannot instantiate   &nbsP;     st.func ();        }     }//the parent class does not have an abstract method, the class name has not been modified by the abstract, and the compilation fails Class person{    abstract void func ( );     //abstract void func1 ();     void func2 () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method 2! ");             };} Class student extends person{    public void func () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method! ");         }}class abstractdemo{    public  static void main (String[]args) {        student st  =new student ();         st.func ();         }     }//compiled successfully abstract class person{    abstract void func ();     //abstract void func1 ();     void func2 () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method 2! ");             };} Class student extends person{    public void func () {         SYSTEM.OUT.PRINTLN ("Non-abstract Method! ");         }}class abstractdemo{    public  static void main (String[]args) {        student st  =new student ();//subclass is abstract class, cannot instantiate         st.func ();         }    }2. All the methods in the interface/**1.  interface are abstract methods, so they can be regarded as the abstract class of the special vulgar 2. The members of the   interface have fixed modifiers          constant: public static final          method:   public abstract, all methods in the interface are public permissions.         */interface Inter{    public  static final int num = 3;    public abstract void  Show ();} Class subinter implements inter{    public void show () {         SYSTEM.OUT.PRINTLN ("Implement Interface! ");     }class interfacedemo{    public static void  main (string args[]) {        subinter sb =  New subinter ();         sb.show ();    }     }


This article is from the "Sima" blog, make sure to keep this source http://9274590.blog.51cto.com/9264590/1695656

Abstract classes and Interfaces in Java

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.