Abstract methods and classes

1. abstract class
The purpose of an abstract class is a sign of some kind can not be initialized at it, can only be inherited polymorphism and produce, for example, Animal is animal, what is necessary is not initialized, the animal is? What animal? . By keyword abstract keyword identifies an abstract class.
1. The compiler does not instantiate an abstract class, not by syntax. Abstract class representatives can not have instantiate objects, but can still be used to declare a polymorphic reference type to use, but do not create the object.
2. In addition to the abstract class is inherited through the outside, there is no use, no value, no purpose
3. The abstract method
1. The abstract class means the class must be inherited, abstract methods to identify this method must be overwritten (rewritten), e.g. Animal class, an abstract written inside eat () method, the method does not write the body, only subclasses which for rewriting.
2 . If you declare an abstract method, then that class must be abstract. However, the abstract class may not contain specific abstract methods.
3 . The method of any particular class must abstract class. Of course it is transferred through to the next marked abstract one implementation inheritance tree.
4. Why should an abstract method? So there is no common code
When there is no way given in the parent class of common sense code, which means an abstract method is that even though you can not achieve a method, but you can still define a set of sub-class mutual agreement (to ensure that there are some of the subclass method)
 
The benefits of an abstract approach is that?
Polymorphism goal is to use the type of the parent class as a parameter to the method, the return type or the flag array. Through this mechanism, may be added in a new subclasses to the program, but do not modify or rewrite processing of these types of programs. Animal is the parent class for example, there are dozens of sub-categories, there is a method with parameters, if a multi-state, then the definition method when passed directly references the parent class, when an incoming call subclass object on it. Without polymorphism, that need to write dozens of methods, each incoming object for each sub-class.
 1 public abstract class Person {
 2     public  abstract void eat();
 3     public abstract void print();
 4 }
 5 class man extends Person{
 6     public void eat() {
 7         System.out.println("man");
 8     }
 9     public void print() {
10         System.out.println("man");
11     }
12     public void solo() {
13         System.out.println("男人");
14     }
15 }
16 class woman extends Person{
17     public void eat() {
18         System.out.println("woman");
19     }
20     public void print() {
21         System.out.println("man");
22     }
23     public void show() {
24         System.out.println("女人");
25     }
26 }
27 
28 public class PersonText {
29     public static void main(String[] args) {
30         personManner per=new personManner();
31         man ma=new man();
32         woman wo=new woman();
33         per.peek(ma);
34         per.peek(wo);
35     }
36 }
37 class personManner{
38     public void peek(Person pe) {
39         if (pe instanceof man) {
40             ((man) pe).solo();
41         }
42         if (pe instanceof woman) {
43             ((woman)pe).show();
44         }
45         pe.eat();
46         pe.print();
47     }
48 }

 

6.注意要点
抽象类:通过abstract修饰类(与final不能够同时出现)
特点:1.抽象类不能被实例化
2.抽象类中可以有构造器(构造器是为了给子类用,子类的构造方法会用到)
3.抽象类可以出现抽象方法,也可以出现非抽象方法
4.抽象类有构造器,但是不能被实例化// 契合面向对象的思想,抽象类没有实际对象所以不能实例化。
5.抽象类中的具体方法可以通过子类的继承关系,用子类对象调用,或者将抽象类中具体的方法用static修饰,然后可以直接用类名调用
 
 
抽象方法:通过abstract修饰的方法
特点:1.抽象方法不能有方法体
2.抽象方法必须被子类重写(除非子类的方法也是抽象方法)
3.抽象方法不能用private修饰,因为抽象方法就是用来被继承的,如果被private修饰就不能被子类继承了
4.抽象方法也不能用static修饰,因为抽象方法没有方法体,没有实际意义,调用就没有意义,如果用static修饰,虽然抽象类不能实例化对象,但是用static修饰之后可以直接用类名调用。

Guess you like

Origin www.cnblogs.com/had1314/p/11373837.html