Abstract classes in Java

Source: Internet
Author: User

In a top-down inheritance hierarchy, classes on the upper level are more generic and may even be more abstract. From a certain point of view, the ancestor class is more general, it contains only some of the most basic members, people only use it as a base class to derive other classes, and not to create objects. Even, the definition of the method can be given without implementation, and the subclass is implemented according to the specific needs.
This method, which only gives the method definition but not the concrete implementation, is called abstract method, abstract method without method body (without {}), there is no "{}" in the expression of the code. A class that contains one or more abstract methods must also be declared as an abstract class. An abstract modifier is used to represent an abstraction method and an abstract class. Abstract classes can contain specific variables and specific methods in addition to abstract methods. A class can be declared as an abstract class, even if it does not contain an abstract method, to prevent it from being instantiated. Abstract classes cannot be instantiated, and abstract methods must be implemented in subclasses.
Example code:

Package abstract class; Public  class abstractclass {     Public Static voidMain (string[] args) {Teacher T =NewTeacher (); T.setname ("Liming");        T.work (); Student s =NewStudent (); S.setname ("Jim");    S.work (); }}//define an abstract classAbstract  class people{    PrivateString name;//Use SHIFT + ALT + S to quickly generate a get, set method (right-click on source)     PublicString GetName () {returnName } Public voidSetName (String name) { This. name = name; }//Define an abstract method     Public Abstract voidWork ();} class Teacher extends people{     Public voidWork () {System.out.println ("I ' m teacher,name is"+ This. GetName () +"is teaching. "); }} class Student extends people{     Public voidWork () {System.out.println ("I ' m student, name is"+ This. GetName () +"is learning. "); }}

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.