Java abstract class

Source: Internet
Author: User

1. Basic concepts of abstract classes

A. Many objects with the same characteristics can be abstracted as a class, and many classes with the same characteristics and behaviors can be abstracted as an abstract class

B. Classes declared with the abstract keyword are abstracted classes

2. Rules for abstract classes

A. Abstract classes can have no abstract methods, classes with abstract methods must be abstract classes

B. Non-abstract class inheritance abstract classes must implement methods of all abstract classes

C. Abstract classes can have method implementations and properties

D. Abstract classes cannot be instantiated

E. Abstract classes cannot be declared as final: abstract classes can be inherited by subclasses but final classes cannot be inherited by subclasses

F. An abstract class can have a constructor that shows but does not instantiate the object because the child class invokes the constructor of the parent class when it is instantiated (calling a constructor method is not necessarily an instantiated object)

Example:

1  Public classAbstracttest {2      Public Static voidMain (string[] args) {3Goddess G =Newgoddess ();4G.setname ("Tong Tong");5 G.say ();6System.out.println ("--------------------------");7Uglywomen U =Newuglywomen ();8U.setname ("Small Buns");9 U.say ();Ten } One } A  - //Women - //Abstract represents the declaration of an abstraction class the Abstract classwomen{ -     PrivateString name; -      PublicString GetName () { -         returnname; +     } -      Public voidsetName (String name) { +          This. Name =name; A     } at      -      Public Abstract voidSay ();//methods of abstract classes are not specific (only declaration implementations) -      Public Abstract voidmethod (); - } -  - classGoddessextendswomen{//a class-inheriting abstract class must implement all methods in an abstract class in     //abstract methods that implement the parent class do not have an abstract adornment -      Public voidsay () { toSystem.out.println ("I am the goddess, I am" +getName ());  +     } -      Public voidMethod () {}//NULL Implementation the } *  $ classUglywomenextendswomen{Panax Notoginseng     //abstract methods that implement the parent class do not have an abstract adornment -      Public voidsay () { theSystem.out.println ("I am an ugly girl, I am" +getName ());  +     } A      Public voidmethod () {} the}

Java abstract class

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.