Abstract class of Java Basics Review

Source: Internet
Author: User
Tags define abstract

/*abstract class: Abstract: General, vague, can not understand! Not specific.    Features: 1, the method is an abstract method, and it needs to be modified only when the declaration is not implemented. Abstract methods must be defined in an abstract class. The class must also be modified by the abstract. 2, abstract classes cannot be instantiated. Why? Because it doesn't make sense to invoke an abstract method.    (Because there is no method body) 3, an abstract class must have its subclasses covered by all the abstract methods before the subclass can be instantiated. Otherwise, this subclass is an abstract class.    1, is there a constructor in the abstract class? There, used to initialize the subclass object.    2, can abstract classes not define abstract methods? OK. However, it is rare to have the class create an object.    The adapter object for AWT is this class.    Usually the method in this class has a method body, but there is no content. Abstract class Demo {void Show1 () {....                The method body without content ...} void Show2 () {....    The method body without content ...}    }3, can abstract keywords coexist with those keywords?    Private not, the abstract method is overwritten, private words can not be overwritten, will report illegal modifier combination error static no final No. 4, abstract class and general class similarities and differences.    The same point: abstract classes and general classes are used to describe things, and members are internally determined.           Different: 1, the General class has enough information to describe things.        Abstract classes may not have enough information to describe things.           2, abstract methods cannot be defined in general classes, only non-abstract methods can be determined.        Abstract methods can be defined in abstract classes, and non-abstract methods can also be defined.           3, the generic class can be instantiated. Abstract classes cannot be instantiated.    5, the abstract class must be a parent class? Yes. You can instantiate a child class only if it needs to overwrite its method. */Abstract classdemo{Abstract /*Abstract*/ voidshow ();}/*class Demoa extends demo{void Show () {System.out.println ("Demoa show");    }}class demob extends demo{void Show () {System.out.println ("demob show"); }}*/Abstract classCanine Branch {Abstract voidroar ();}classDogextendsCanine Branch {voidRoar () {System.out.println (Barking); }}classWolfextendsCanine Branch {voidRoar () {System.out.println (Whining); }}classAbstractdemo { Public Static voidMain (string[] args) {System.out.println ("Hello world!"); }}

Application of abstract class:

/*Employee Example: requirements: The programmer in the company has the name, work number, salary, job content. The project manager has the name, job number, salary, bonus and work content. Modeling data for a given requirement. Analysis: In this problem area, first find out the objects involved. By the method of noun refinement. Programmer: Attribute: Name, job number, salary, behavior: work.    Manager: attribute: Name, work number, salary, bonus. Behavior: Work. There is no direct inheritance between programmers and managers, but programmers and managers have common content. can be extracted.    Because they're all employees of the company, programmers and managers can be extracted. Establish the system. *///describe the employee. Abstract classemployee{PrivateString name; PrivateString ID; Private DoublePay ; Employee (String name,string ID,DoublePay ) {         This. Name =name;  This. ID =ID;  This. Pay =Pay ; }     Public Abstract voidWork (); }//describe the programmer. classProgrammerextendsemployee{Programmer (String name,string ID,DoublePay ) {        Super(Name,id,pay);//the instantiation of the subclass, which can be done by the parent class, invokes the constructor of the parent class to initialize    }     Public voidWork () {System.out.println ("Code ..."); }}//describe the manager. classManagerextendsemployee{Private intbonus; Manager (String name,string ID,DoublePayintbonus) {        Super(Name,id,pay);  This. Bonus =bonus; }     Public voidWork () {System.out.println ("Manage"); }}classabstracttest{ Public Static voidMain (string[] args) {System.out.println ("Hello world!"); }}classperson{PrivateString name; Private intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}classStudentextendsperson{Student (String name,intAge ) {        Super(Name,age); }}classWorkerextendsperson{Worker (String name,intAge ) {        Super(Name,age); }}

Abstract class of Java Basics Review

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.