1. Abstract class overview
Package com;class animal{public void Eat () {System.out.println ("Animal Eats");} public void Sleep () {System.out.println ("Sleeping Animals");}} Class Dog extends animal{@Overridepublic void Eat () {System.out.println ("Cat Eats");} @Overridepublic void Sleep () {System.out.println ("Cat Sleeps");}} Class Cat extends animal{@Overridepublic void Eat () {System.out.println ("dog eating");} @Overridepublic void Sleep () {System.out.println ("dog Sleeps");}} public class Animaltest {public static void main (string[] args) {Animal a = new Dog (); A.eat ();}}
From the above code, we begin to think. An animal is not a concrete thing, but an abstract thing. Only a real cat or dog is a specific animal. In the same way, we can infer that different animals should eat something that is not the same. Therefore, we should not give specific implementation details in the animal class, but should give a declaration. In Java, a method without a method body should be defined as an abstract method, and if there is an abstract method in the class, the class is called an abstract class.
2. Abstract class Features
Abstract classes and abstract methods must be decorated with the abstract keyword.
Format:
Abstract class class name {}
public abstract void Eat ();
Abstract classes do not necessarily have abstract methods, and classes with abstract methods must be abstract classes.
Abstract classes cannot be instantiated. Because it's not specific. Abstract classes have construction methods, but they cannot be instantiated. So what is the function of the construction method? Initializes the child class to access the parent class data.
The subclass of an abstract class is either an abstract class or an abstract class that implements all the abstract methods.
Package Com;abstract class Animal{public abstract void Eat ();p ublic abstract void Sleep ();} Class Dog extends animal{@Overridepublic void Eat () {System.out.println ("Cat Eats");} @Overridepublic void Sleep () {System.out.println ("Cat Sleeps");}} Class Cat extends animal{@Overridepublic void Eat () {System.out.println ("dog eating");} @Overridepublic void Sleep () {System.out.println ("dog Sleeps");}} public class Animaltest {public static void main (string[] args) {Animal a = new Dog ();//polymorphic a.eat ();}}
3. Abstract class member characteristics
Member variable: can be either a variable or a constant.
Constructor: There is, for subclass access, the initialization of the parent class data.
Member methods: It can be either a common method or an abstract method.
Member method attributes for abstract classes:
Abstract method: Forcing subclasses to do things.
Common method: Let subclass inherit, improve the reusability of code.
4. Abstract class Cases
package com;/** * specific business: Cats Dogs * Commonalities: name age Eat * * analysis: From concrete to abstract * cat: * member variables: name Age * member Method: Eat (cat eat fish) * dog: * member variable: name Age * Membership Method: Eat (dog eat meat) * * because there is a common content, so the parent class-animal. * but because the content of meals is not the same, the way to eat is abstract. * and the method is abstract, so the animal class must be an abstract class. * * abstract class: * member variable: name age * member Method: meal () * * implementation: from abstraction to concrete * Animal class: * member variable: name age * member Method: meal () * Dog class: * inherit from the animal class, rewrite meal () * Cat class: * inheritance AutomaticClass of things, rewriting meals () *///abstract class---animal abstract class animal{private string name;//name private int age;//Age//No parameter construction method Public animal () {}//has parameter construction method Public animal (string name,int age) { This.name = name;this.age = age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} Public abstract void eat ();} The concrete class--The dog class class dog extends animal{//the non-parameter constructs the method Public dog () {}//has the parameter construction method Public dog (String name,int age) {super (name, age);} @Overridepublic void eat () {system.out.println ("Dog eats Meat");}} Concrete Class--cat class class cat extends animal{//non-parametric construction method Public cat () {}//method Public cat (String name,int age) {super (name, age);} @Overridepublic void eat () {System.out.println ("Cat eats Fish");}} Public class abstracttest {public static void main (String[] args) { Animal a = new dog ("Wang Choi", 2); System.out.println ("Dog's Name:" +a.getname () + ", Age:" +a.getage ()); A.eat (); A = new cat ("Jin Bao", 3); A.eat (); System.out.println ("Cat's Name:" +a.getname () + ", Age:" +a.getage ());}}
package com;/** * specific things: Chinese teacher math teacher * commonness: Name age Lectures * * analysis: * Chinese teacher * name Age * lecture * math teacher * name Age * lecture * realization: * Teacher class * Chinese teacher * Mathematics teacher *///Abstract class--teacher class abstract class teacher{private string name;//name Private int age;//Age//No parameter construction method Public teacher () {}//has parameter construction method Public teacher (string name,int age) { This.name = name;this.age = age;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (Int age) {this.age = age;} /** * lectures */public abstract void teach ();} Chinese Teacher class class chineseteacher extends teacher{//non-parametric construction method Public chineseteacher () {}// Method Public chineseteacher (String name,int age) {super (name,age);} @OvErridepublic void teach () {system.out.println ("speaking Language");}} Mathematics Teacher Class class mathteacher extends teacher{//non-parametric construction method Public mathteacher () {}// Method Public mathteacher (String name,int age) {super (name, age);} @Overridepublic void teach () {system.out.println ("speaking Mathematics");}} Test class Public class abstracttest1 {public static void main (String[] args) {teacher t = new chineseteacher ("Chinese teacher haha", 40); System.out.println ("Language Teacher Name:" +t.getname () + ", Age:" +t.getage ()); T.teach (); T = new mathteacher (" Math teacher hehe ", 22); System.out.println ("Math teacher Name:" +t.getname () + ", Age:" +t.getage ()); T.teach ();}}
package com;/** * Student Case * Specific things: Humanities class students science class students * commonness: Name Age class Learning Meal * Analysis: * liberal Arts class * member variables: Name Age Class * members method: Learn Eat * Science class student * member variable: name Age Class * members method: Learn Eat * get a learner class: * member Variable: Name Age Class * member method: Learning Eating * Realization: * Students class: * Arts Class Students * Science class students * *///define abstract Trainee class abstract class employee{ private string name;//name private int age;//Age private string classmate;//class// Non-parametric construction method Public employee () {}//has the method Public employee (string name,int age,string classmate ) {this.name = name;this.age = age;this.classmate = classmate;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public int getage () {return age;} Public void setage (int age) {this.age = Age;} Public string getclassmate () {return classmate;} Public void setclassmate (String classmate) {this.classmate = classmate;} /** * Dinner */public void eat () {System.out.println ("eat");} /** * learning */public abstract void study ();} Liberal Arts learners class liberalemployee extends employee{//construction Method Public liberalemployee () {}// Method Public liberalemployee (String name,int age,string classmate) {super (Name, age, classmate);} @Overridepublic void study () {system.out.println ("Learning Arts");}} Science student class scienceemployee extends employee{//Non-parametric construction method Public scienceemployee () {}// Method Public scienceemployee (String name,int age,string classmate) {super (Name, age, classmate);} @Overridepublic void study () {system.out.println ("Learning Science");}} Test class public class abstracttest2 {public static Void main (String[] args) {employee e = new liberalemployee ("The liberal arts student haha", 20, " Arts Class 01 class "); SYSTEM.OUT.PRINTLN ("Arts class Participant Name:" +e.getname () + ", Age:" +e.getage () + ", Class:" +e.getclassmate () "); E.eat (); E.study ();e = new scienceemployee ("Science students hehe", 18, "Science Class 01 class"); SYSTEM.OUT.PRINTLN ("Science Class student Name:" +e.getname () + ", Age:" +e.getage () + ", Class:" +e.getclassmate () "); E.eat (); E.study ();}}
package cn;/** * If we are developing a system, we need to design the employee class. * employees include 3 attributes: Name, work number, and salary. * Manager is also an employee, in addition to the attributes that contain employees, there is also a bonus attribute. * Use the idea of inheritance to design employee and manager classes. * requires that the class provide the necessary methods for property access. *///Employee Class abstract class employee{private string name;//name private String empno;//Number private double salary;//Salary Public employee () {}public employee (String Name,string empno,double salary) {This.empno = empno;this.name = name;this.salary = salary;} Public string getname () {return name;} Public void setname (String name) {this.name = name;} Public string getempno () {return empno;} Public void setempno (String empno) {this.empno = empno;} Public double getsalary () {return salary;} Public void setsalary (Double salary) {this.salary = salary;} /** * work */public abstract voiD work ();} Programmer class programmer extends employee{//Method Public programmer () {}//has a method for constructing a parameter public Programmer (string name,string empno,double salary) {super (name, empno, salary);} @Overridepublic void work () {system.out.println ("Write Code as required");}} Manager class manager extends employee{private double bonus;//Bonus//Non-parametric construction method public manager () {}//has the method Public manager (string name,string empno,double salary,double bonus) {super (name, empno, salary); this.bonus = bonus;} Public double getbonus () {return bonus;} Public void setbonus (Double bonus) {this.bonus = bonus;} @Overridepublic void work () {system.out.println ("Talk to customers about demand");}} Test class Public class abstracttest {public static void main (String[] args) {employee e = new programmer ("Programmer haha", "001", 5000); System.out.println ("Name:" +e.getname () + "number:" +e.getempno () + "Salary:" +e.getsalary ()); E.work ();e = new Manager ("Managers hehe", " 10000, 5000"); System.out.println ("Name:" +e.getname () + "number:" +e.getempno () + "Salary:" +e.getsalary () + "Bonus:" + ((Manager) e). Getsalary ()); E.work ();}}
5. Abstract class Minor problems
If there is no abstract method for a class, can it be defined as an abstract class? If you can, what's the point.
Yes, don't let the object be created.
The abstract keyword and the private keyword, the final keyword conflict, and the static keyword are meaningless.
This article is from the "11831428" blog, please be sure to keep this source http://11841428.blog.51cto.com/11831428/1858993
The abstract class of Java