Java basics 9-abstract class, java-Abstract
Java basics 9-abstract class 1. Introduction to abstract classes
/*
Abstract class:
Abstract: general, fuzzy, and incomprehensible! Not specific.
Features:
1. MethodOnly declarations are not implementedThis method isAbstract Method, Need to beAbstract.
Abstract methods must be defined inAbstract class. This class must also be abstract.
2. abstract classes cannot be instantiated. Why? It is meaningless to call the abstract method.
3. An abstract class must have its subclass covering all abstract methods before it can be instantiated.
Otherwise, this subclass is still an abstract class.
1. Is there any constructor in the abstract class?
Yes. It is used to initialize the subclass object.
2. can abstract classes not define abstract methods?
Yes. But it is rare that the purpose is not to allow this class to create objects. AWT adapter objects are of this type.
Methods In this class usually have method bodies, but there is no content.
Abstract class Demo
{
Void show1 ()
{}
Void show2 ()
{}
}
3. Cannot abstract keywords coexist with those keywords?
Private
Static does not work. If it is static, no object is needed, and the abstract class is the object.
Not final
4. Similarities and Differences between abstract classes and general classes.
Similarities:
Abstract classes and general classes are used to describe things, and members are set internally.
Different:
1. The general class has enough information to describe things.
Abstract classes may not provide sufficient information to describe things.
2. abstract methods cannot be defined in a general class. Only non-abstract methods can be defined.
Abstract classes can define abstract methods and non-abstract methods.
3. General classes can be instantiated.
Abstract classes cannot be instantiated.
5. Must an abstract class be a parent class?
Yes. The subclass can be instantiated only after its method is overwritten by the subclass.
*/
Abstract class Demo
{
Abstract/* abstract */void show ();
}
/*
Class DemoA extends Demo
{
Void show ()
{
System. out. println ("demoa show ");
}
}
Class DemoB extends Demo
{
Void show ()
{
System. out. println ("demob show ");
}
}
*/
Abstract class dogs
{
Abstract void roar ();
}
Class dog extends dogs
{
Void roar ()
{
System. out. println ("Wang ");
}
}
Class wolf extends dogs
{
Void roar ()
{
System. out. println ("zookeeper ");
}
}
Class AbstractDemo
{
Public static void main (String [] args)
{
System. out. println ("Hello World! ");
}
}
Ii. abstract class instances
1/* 2 employee example: 3 requirement: programmers in the company have names, employee IDs, salaries, and work content. 4 In addition to the name, employee ID, salary, bonus, and work content, the Project Manager. 5. perform data modeling on the given requirements. Analysis: 10 first, find the objects involved in this issue. 11. The term extraction method is adopted. 12 programmer: 13 attributes: name, employee ID, salary, 14 behavior: work. 15 MANAGER: 16 attributes: name, employee ID, salary, and bonus. 17. Action: work. 18 19 20 programmers and managers do not have direct inheritance relationships, but 21 22 programmers and managers have common content. 23 can be extracted. Because they are employees of the Company 24 25 can extract programmers and managers. Establish a system. 26 27 */28 29 // describe employees. 30 31 abstract class Employee 32 {33 private String name; 34 private String id; 35 private double pay; 36 Employee (String name, String id, double pay) 37 {38 this. name = name; 39 this. id = id; 40 this. pay = pay; 41} 42 43 public abstract void work (); 44 45} 46 47 48 // describes programmers. 49 class Programmer extends Employee 50 {51 Programmer (String name, String id, double pay) 52 {53 super (name, id, pay); 54} 55 public void work () 56 {57 System. out. println ("code... "); 58} 59} 60 61 // description manager. 62 class Manager extends Employee 63 {64 private int bonus; 65 Manager (String name, String id, double pay, int bonus) 66 {67 super (name, id, pay ); 68 this. bonus = bonus; 69} 70 public void work () 71 {72 System. out. println ("manage"); 73} 74} 75 76 77 78 79 80 81 class AbstractTest 82 {83 public static void main (String [] args) 84 {85 System. out. println ("Hello World! "); 86} 87} 88 89 90 class Person 91 {92 private String name; 93 private int age; 94 95 Person (String name, int age) 96 {97 this. name = name; 98 this. age = age; 99} 100 public String getName () 101 {102 return name; 103} 104 public void setName (String name) 105 {106 this. name = name; 107} 108} 109 class Student extends Person110 {111 Student (String name, int age) 112 {113 super (name, age ); 114} 115 116} 117 class Worker extends Person118 {119 Worker (String name, int age) 120 {121 super (name, age); 122} 123 124}