Java keyword finishing two

Source: Internet
Author: User
Tags class manager

Abstrac and Interface

Abstract class: Abstraction

Abstract classes exist for inheritance, and if you define an abstract class and do not inherit it, you create this abstract class in vain, because you cannot use it to do anything. For a parent class, if one of its methods is implemented in the parent class without any meaning, it must be implemented differently according to the actual needs of the subclass, then the method can be declared as an abstract method, and this class becomes the abstract class.

The format is as follows

A class that contains an abstract method is called an abstract class, but it does not mean that there can be only abstract methods in an abstract class, which, like normal classes, can also have member variables and ordinary member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public or protected (because if you are private, you cannot inherit from the quilt class, the subclass cannot implement the method), and by default it is public.

2) An abstract class cannot be used to create an object, but it can have a construction method to help subclasses instantiate.

3) If a class inherits from an abstract class, the child class must implement the abstract method of the parent class. If the subclass does not implement an abstract method of the parent class, the subclass must also be defined as an abstract class.

The specific examples of abstract classes are as follows:

Define an abstract class, remember that it cannot be used to create an object abstract class employee{//declares a protected property protected String name;    protected String ID;        protected double pay;          How to construct an abstract class public Employee (String name,string id,double pay) {this.name = name;          This.id = ID;    This.pay = pay;    }//Abstract class normal member method public void print () {System.out.println ("name =" +name+ "id =" +id+ "pay =" +pay "); }//abstract class of abstract methods, subclasses must implement the method, otherwise the subclass is also abstract class public abstraction void work ();} A subclass of abstract class class Pro extends employee{public Pro (String name,string id,double pay) {//Call the constructor of the parent class to initialize the SU    Per (Name,id,pay);    }//Implements the abstract method in the parent class, so the subclass is no longer abstract class public void work () {System.out.println ("Pro work");    }}//abstract class Another subclass class Manager extends employee{private double bonus;      Public Manager (String name,string id,double pay,double Bonus) {//calls the constructor of the parent class to initialize super (Name,id,pay);    This.bonus = bonus;  }//Implements a method in the parent class, so the subclass is no longer abstract class public void work ()  {System.out.println ("Manager work");      }}//test class yuangongdemo{public static void Main (string[] args) {Pro P1 = new Pro ("Liding", "JFISD", 578.5);      P1.work ();      Manager P2 = new manager ("Lining", "technical department", 4578.5,786.75);    P2.work (); }}

  

Second, interface: Interface

Java interface is the declaration of a series of methods, is a collection of methods features, an interface only the methods of the characteristics of the method is not implemented, so these methods can be implemented in different places by different classes, and these implementations can have different behavior (function).

Main features of the interface:

1. The member variables in the interface are public, static, and final type, and must be initialized by the display.

2, the method in the interface is public, abstract type by default.

3, the interface can only contain public, static, final type member variables and public, abstract type member methods.

4, the interface does not have the method of construction, cannot be instantiated.

5. An interface cannot implement another interface, but it can inherit multiple other interfaces.

6, the interface must be through the class to implement its abstract method.

7, similar to the subclass inheriting the abstract parent class, when the class implements an interface, it must implement all the abstract methods in the interface, otherwise the class must be defined as an abstract class.

8. A class can inherit only one direct parent class, but can implement multiple interfaces.

Interface declarations and implementations are as follows:

Specific examples are as follows:

Multi-state function extension
Computer-based instances (computer motherboards)
Extension (NIC, sound card 、、、)

InterfacePci//interfaces for extended functions (rules){     Public Abstract voidopen ();  Public Abstract voidclose ();}classmainboard { Public voidrun () {System.out.println ("Mi Anboard Run"); }     Public voidUsepci (PCI p)//PCI p = new netcard ()//A reference to an interface type points to its own subclass object{p.open ();    P.close (); }        }classNetcardImplementsPci//Implementing the connection of the NIC{     Public voidOpen () {System.out.println ("Netcard Open"); }     Public voidClose () {System.out.println ("Netcard Close"); }}classSoundcardImplementsPci//Implementing a sound card connection{     Public voidOpen () {System.out.println ("Soundcard Open"); }     Public voidClose () {System.out.println ("Soundcard Close"); }}classDuoTaiDemo1//Test Class{     Public Static voidMain (string[] args) {mainboard MB=Newmainboard ();        Mb.run (); MB.USEPCI (Newnetcard ()); MB.USEPCI (Newsoundcard ()); }}
Summary: The difference between abstract classes and interfaces

1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;

2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

5) The method subclasses in the abstract class must be fully implemented, otherwise the subclasses are abstract classes, and the abstract method subclasses in the interface must all be implemented, and can be optionally implemented.

6) Abstract class is a template design pattern, and interface is a kind of behavior specification.

Java keyword finishing two

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.