Abstract interface polymorphism, abstract Polymorphism

Source: Internet
Author: User
Tags class manager

Abstract interface polymorphism, abstract Polymorphism

Content: abstract, interface, and Polymorphism

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

1. Abstract

The method is abstract, so the class must be abstract.
Abstract classes cannot pass the new object
Only after the subclass covers all abstract methods can the subclass create an object through new. If the subclass does not cover all abstract methods, the subclass is still an abstract class.
Abstract keywords abstract, final, private, and 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 Employee15 {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 Employee34 {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 Employee46 {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 InterA12 {13     public abstract void show1();14 }15 16 interface InterB17 {18     public abstract void show2();19 }20 21 class SubInter implements InterA,InterB22 {23     public void show1()24     {25         System.out.println("show1");26     }27     public void show2()28     {29         System.out.println("show2");30     }31 }
View Code

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

3. Polymorphism
Polymorphism: that is, the type problem after inheritance, such as the Zi class and Fu class, Fu f = new Zi (); then the f object cannot use the special method of the Zi class, you can use the subclass method by forcibly converting to the Zi type.

Common basic functions of describing things by users
Interface is used to define additional functions of a transaction

 

 

 

 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.