009 abstract interface polymorphism

Keywords: Java

Content: abstract, interface, polymorphism

#########################################################################################################

1. Abstract

If the method is abstract, then the class must also be abstract
Abstract class cannot pass new object
Only when the subclass covers all abstract methods can the subclass create objects through new. If it does not cover all abstract methods, then the subclass is still an abstract class
Abstract keyword abstract and final, private, static cannot coexist

 1 class ChouXiang 
 2 {
 3     public static void main(String[] args) 
 4     {
 5         Programmer p = new Programmer("jiang","ji357",7600);
 6         Manager m =new Manager("x","g009",12000,60000);
 7         p.work();
 8         m.work();
 9         p.show();
10         m.show();
11     }
12 }
13 
14 abstract class Employee
15 {
16     private String name;
17     private String id;
18     private double salary;
19 
20     Employee(String name,String id,double salary)
21     {
22         this.name = name;
23         this.id = id;
24         this.salary = salary;
25     }
26     abstract void work();
27     void show()
28     {
29         System.out.println("name:"+name+", id:"+id+", salary:"+salary);
30     }
31 }
32 
33 class Programmer extends Employee
34 {
35     Programmer(String name,String id,double salary)
36     {
37         super(name,id,salary);
38     }
39     void work()
40     {
41         System.out.println("code");
42     }
43 }
44 
45 class Manager extends Employee
46 {
47     private double bonus;
48     Manager(String name,String id,double salary,double bonus)
49     {
50         super(name,id,salary);
51         this.bonus = bonus;
52     }
53     void work()
54     {
55         System.out.println("manage");
56     }
57 }
eg1

###########################################################################################################

2. interface
There are two types of interface members: 1. Global variables; 2. Abstract methods

interface Inter{
    public static final int PI = 3.141592653589793 ;
    public abstract void show1();
    public abstract void show2();
}

class De implements Inter{
    public void show1(){}
    public void show2(){}
 1 class  JieKouD
 2 {
 3     public static void main(String[] args) 
 4     {
 5         SubInter s = new SubInter();
 6         s.show1();
 7         s.show2();
 8     }
 9 }
10 
11 interface InterA
12 {
13     public abstract void show1();
14 }
15 
16 interface InterB
17 {
18     public abstract void show2();
19 }
20 
21 class SubInter implements InterA,InterB
22 {
23     public void show1()
24     {
25         System.out.println("show1");
26     }
27     public void show2()
28     {
29         System.out.println("show2");
30     }
31 }

############################################################################################################

3. Polymorphism
Polymorphism: that is to say, after inheritance, type problems, such as Zi class and fu class, Fu f = new Zi(); this kind of F object can't use the unique method of Zi class, when it is converted to Zi type by coercion, it can use the method of subclass.

The common basic functions of user like description
Interfaces are used to define the extra functions of things

 

 

 

 



Posted by cartoonjunkie on Sun, 05 Apr 2020 12:44:48 -0700