Java abstraction, interface, and final

Source: Internet
Author: User

AbstractAbstract class: Do not know what is the specific type of things. Abstract class name

1. Abstract classes cannot be directly new.

2. Abstract classes can have no abstract methods.

3. In an abstract class, there can be abstract methods or non-abstract methods. You can also have member variables in an abstract class.

4. In a derived class, you can not implement some abstract methods, but derived classes must be declared as abstract classes.

Second, abstract method: Do not know the specific is the way to do something. Abstract return type function name (formal parameter);

1. The abstract method has no function body.

2. A class that has an abstract method must be an abstract class.

Encountered abstract class, two usages:

1. Find out if there is a derived class for it, with its derived class.

2. Write a class yourself, derived from this abstract class.

 Package com.itnba.may.Demo1229;  Public Abstract class USB {   // abstract class     // Two abstract methods     Public abstract void Qidong ();         Public Abstract void Tingzhi ();}
Interface

Can be understood as a special kind of abstract class. There are only constants and abstract methods (no need to write abstract).

Interfaces can be implemented more (inherited), and abstract classes can only be inherited by units.

Defined:

Interface interface Name [extends parent interface List]{
Final type constant name = value;
public void method Name (formal parameter);
}

 Public Interface USB {   // defines an interface public    void  qidong ();         Public void Tingzhi ();}
Implementation of the interface:

[Abstract] Class name extends parent class implements interface name, interface name,.... {

}

 Public class Implements usb{        @Override    publicvoid  Qidong () {        System.out.println ("Mouse Run" );    }      @Override    publicvoid  Tingzhi () {        System.out.println ("Mouse unplugged" );    }     // if a method with an interface is not rewritten, the class is converted to abstract abstraction. }
When the interface is defined:

1. The interface as small as possible, the function is too complex. --Interface separation principle.

2. When the interface is defined, the interface name, as far as possible adjectives, or describe the nature of the word.

Final modification uses final to modify member variables, member methods, and classes.

1.final Decorated class: This class cannot be inherited.

2.final modified member Variable: can be used as represented.

3.final Retouching method: Cannot overwrite (override) The method of the quilt class, but can be inherited.

Java abstraction, interface, and final

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.