Java notes (3) abstract classes and abstract methods

Source: Internet
Author: User

Abstract classes and abstract methods

(Abstract classes must be inherited, abstract methods must be heavy)

Abstract class: A class that contains an abstract method.

Abstract methods: Methods that are declared but not implemented, the abstract method must use the abstract keyword declaration.

Abstract classperson{Private intAge ; PrivateString name;  PublicPerson (intAge , String name) {         This. Age =Age ;  This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }        Public abstract void want ();}classStudentextendsperson{Private intscore;  Public intGetscore () {returnscore; }     Public voidSetScore (intscore) {         This. score =score; }     PublicStudent (intAge, String name,intscore) {        Super(age, name);  This. score=score; } @Override Public voidwant () {System.out.println ("Name:" +getname () + "Age" +getage () + "score" +Getscore ()); }    }classWorkerextendsperson{Private intMoney ;  Public intGetmoney () {returnMoney ; }     Public voidSetmoney (intMoney ) {         This. Money =Money ; }     PublicWorker (intAge, String name,intMoney ) {        Super(age, name);  This. money=Money ; //TODO auto-generated Constructor stub} @Override Public voidwant () {System.out.println ("Name:" +getname () + "Age" +getage () + "salary" +Getmoney ()); }        } Public classAbsDemo01 { Public Static voidMain (string[] args) {Student s=NewStudent (10, "xiaoming", 100);        S.want (); Worker W=NewWorker (35, "Daming", 1000);    W.want (); }}

Operation Result:

Name: Xiao Ming age 10 score 100
Name: Daming Age 35 Salary 1000

Analysis:

The abstract method of the above source code public abstract void want; (I can think of it as a method of their subclasses, and every student and woker have what they want!) )

Induction:

1. Abstract classes cannot be instantiated.

2. Abstract classes do not necessarily contain abstract methods, but if an abstract method is included in a class, the class must be defined as an abstract class.

3. Abstract classes must be inherited.

4. The abstract method must be rewritten.

In summary, an abstract class cannot be instantiated directly, but by its subclasses.

Java notes (3) abstract classes and abstract methods

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.