Some summaries of abstract classes in Java

Source: Internet
Author: User

    1. Abstract classes, like normal classes, have Constructors. There are attributes in an abstract class that can be initialized with a constructor method. When a subclass object is instantiated, the construction of the abstract class is performed before the subclass Constructs.
    2. Abstract classes cannot be declared with final . Abstract classes cannot use the final definition because abstract classes must have subclass inheritance .
    3. an abstract class cannot be declared with static, but an inner class in an abstract class can use the static Declaration.

Abstract classa{Static Abstract classB { public Abstract voidPrint (); }}classXextendsa.b{ public voidprint () {System.out.println ( this. GetClass ()); }} public classhello{ public Static voidMain (string[] Args)throwsException {a.b AB=NewX ();    Ab.print (); }}

Output: class X

4. Abstract classes, like normal classes, can have static methods and can be passed through the class name . method name is called in the way

Abstract class a{    publicstaticvoid  test ()    {        System.out.println (" static method in the abstract class ");}    }  public class hello{    publicstaticvoid main (string[] Args)  throws  Exception    {        a.test ();    }}

5. Sometimes abstract classes require only a specific system subclass operation, which can hide the implementation of an abstract class Externally. Such a design is common in the System class library in order to hide the subclass implementations that we do not need.

Abstract classa{ public Abstract voidPrint (); Private Static classBextendsA { public voidprint () {System.out.println ("print message test"); }    }     public StaticA getinstance () {return NewB (); }} public classhello{ public Static voidMain (string[] Args)throwsException {a a=a.getinstance ();    A.print (); }}

6. Observe the output below.

Abstract classa{ publicA () { this. Print (); }     public Abstract voidPrint ();}classBextendsa{Private intnum = 100;  publicBintNum) {        Super();  this. num =num; }     public voidprint () {System.out.println ( this. num); }} public classhello{ public Static voidMain (string[] Args)throwsException {b b=NewB (30); }}

Above code output: 0

Some summaries of abstract classes 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.