Java object-oriented basic features II

Source: Internet
Author: User

First, the package

1. Concept: To make member variables in a class private, but to provide the public method to access these member variables. This realizes the member variables in the hidden class, thus ensuring the security of the data;

2. Example:

private String name;

Public String GetName () {for reading variable data

return name;

}

public void SetName (String name) {Used to set variable data

THIS.name = name;

}

Note: When naming these methods, use get and set plus variable names (capitalize the first letter of the variable name);

Second, polymorphic

1. Classification:

① static polymorphism: The way of implementation is the overloading of the method;

② Dynamic polymorphism: The way to implement is to rewrite the parent class;

Note: We are talking about polymorphism, which is generally referred to as dynamic polymorphism.

2. Transformation of reference data (the same syntax as the basic Data Transformation):

① upward Transformation: from sub-class to the parent class, can be automatically implemented;

② downward transformation: From the parent class to the sub-class, need to cast;

3. Instantof Keywords:

An instance of the object that is used to determine whether the object before Instantof is Instantof. (Is return true, not return false)

  

Third, abstract

1. Keywords: abstract

2. Abstract class:

Abstract class declared by abstract, cannot be instantiated. Therefore, it is necessary to have sub-class to reflect its role.

3. Abstract Method:

Abstract declaration of abstraction, there is no method body, can only appear in the abstract class, and in the subclass to be implemented only (abstract subclasses can not be implemented).

Iv. interface

1. Concept: is a more special kind of abstraction. All methods in an interface are abstract methods, and none of the methods provide a concrete implementation. Other classes are required to implement all of the defined methods.

2. Interface and class similarities and differences:

With:

① can have any number of methods;

② Save as ". Java" as the suffix name of the file, the interface name must be consistent with the file name;

③ the compiled file is a file with the suffix named ". Class";

④ the interface in the package, the corresponding bytecode file must be placed in a directory that matches the package name;

Vary

① interface cannot be instantiated;

The ② interface does not contain any constructors;

All methods in the ③ interface are abstract;

The ④ interface cannot contain instantiated member variables. The only member variable that can appear in the interface must be declared both as static and final (public static constant);

The ⑤ interface cannot be inherited by a class and can only be implemented by a class;

⑥ an interface can inherit multiple interfaces;

3. Declaration of the interface:

Grammar:

Public interface Interface Name {
Any number of Final,static member variables
Any number of abstract method declarations
}

Properties:

The ① interface is abstract by default. Therefore, when declaring, you do not need to use the keyword abstract;

Each method in the ② interface is abstract. Therefore, when declaring, there is no need to use the keyword abstract;

The methods in the ③ interface are generally used public, so public can also be omitted;

4. Implementation of the interface:

Keywords: implements;

Grammar:

public class class name [extends parent class name] implements interface name {}

Note: Multiple interfaces can be implemented at one time, with multiple interface names separated by commas, and classes that implement an interface (except abstract classes) must implement all the methods in the interface;

Java object-oriented basic features II

Related Article

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.