Abstract class, interface, abstract class Interface

Source: Internet
Author: User

Abstract class, interface, abstract class Interface

Why use abstract classes:

If the subclass is not correctly rewritten, no prompt is displayed.

Assume that the code of the parent class is as follows:

public class USB {     public void QiDong(){                 }     public void TingZhi(){              }}

The code in the subclass is as follows: rewrite the method in the parent class

Public class UPan extends USB {public void Qidong () {// Qidong here is not a rewriting of the parent class function System. out. println ("U disk started");} public void TingZhi () {System. out. println ("the USB flash disk stops running ");}}

Such code will not report errors in Eclipse;

Abstract class: a class that does not know what it is.

Description: abstract class name

public abstract class USB {}

Abstract method: I don't know what the specific method is;

Writing Method: abstract return type function name (parameter); abstract method does not need to write function body

In a derived class, certain abstract methods are not implemented, but the derived class must be declared as an abstract class.
Abstract classes can have abstract methods or non-abstract methods. Abstract classes can also contain member variables.

public abstract class USB {      public  String JiaGe;     public abstract void QiDong();
public abstract void TingZhi(); }

 

Interface:

The role of the interface: It mainly serves as a constraint. It can be understood that the code format of a large frame can only be written in the specified format.

Definition: file-New-interface // The first letter of the interface name plus an uppercase I

When defining an interface, the interface name should be an adjective or descriptive word.

Interface content as few as possible for convenient and flexible use-interface separation principle

Interface Name extends parent interface list {final type constant name = value; // specifies the format in which to write data. If the format is different from the format, public void method name (parameter) will be reported );}

Interface implementation:

Abstract class name extends parent class implements interface list {
}

 

As mentioned above, the interface serves as a constraint, so let's take a look at how it is constrained.

Create an interface-enter the following code in the interface

public interface IJiSuan {    public int a=0;        public void Suan(int a, int b);}

Then we create a new class-class add interface, click OK, the following code is automatically generated;

Public abstract class YunSuan implements IJiSuan {@ Override public void Suan (int a, int B) {// method stub automatically generated by TODO }}

If we change the method name to suan, an error is automatically reported.

 

Final can modify member variables (constants), member methods (not allowed to be rewritten), classes (not allowed to be inherited)

  

 

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.