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