Classes and Interfaces in Java

Source: Internet
Author: User

1 , class:

A class is a collection of sets of objects with the same properties and methods that provide a uniform abstract description of all objects that belong to the class, including the properties and methods two main parts. In an object-oriented programming language, a class is a stand-alone program unit that should have a class name and include two main parts of properties and methods.

The class implementation in Java consists of two parts: class declaration and class body.

1.1 declaration format for the class:

[Pbulic] [Abstract/final] class ClassName [extends ParentName] [implements]

{iname1,iname2 ...} Where the modifier public,abstract,final describes the class's properties, classname the class name, superclassname the name of the class's parent class, and interfacenamelist the list of interfaces that the class implements.

1.1.1 access modifier (default, private,public,protected):
Private: Privately. can only be accessed by the code in the curly braces of the current class. Out of this curly brace, not accessible.
Public: publicly. can be accessed within the current class curly braces. can also be accessed by objects from other locations.

Default: Can be accessed in the current class curly braces. It can also be accessed in other classes in the current package. Out of the current package, it will not be accessible.

Protected: Can be accessed by a derived class in the same package and other packages.

1.1.2abstract:

1. Abstract class: Do not know what is specific to the class, there can be no abstract method. Abstract classes cannot be used directly with new.

Definition method: Abstract class name.

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

2. Abstract method: Do not know the specific thing is the way to do, there is no function body. Abstract returns the type function name (formal parameter). The class of an abstract method must be an abstract class.

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

After encountering abstract classes, there are 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.

1.1.3 Final :

Final can modify member variables, member methods, and classes.

Final modifier member variable--when represented is used.

Final decorated class. This class cannot be inherited.

Final modification method. This direction is not allowed to be rewritten.

1.2 class Body

Class classname{

[Public | protected | private] [Static] [final] type variableName;

Member variables

[Public | protected | private] [Static] [Final | abstract] returntype methodName ([paramlist]) {statements}//member method

}

1.2.1 member Variables

Definition: Access modifier type variable name.

The meaning of a member variable qualifier:

Private: Privately. can only be accessed by the code in the curly braces of the current class. Out of this curly brace, not accessible.
Public: publicly. can be accessed within the current class curly braces. can also be accessed by objects from other locations.

Default: Can be accessed in the current class curly braces. It can also be accessed in other classes in the current package. Out of the current package, it will not be accessible.

Protected: Can be accessed by a derived class in the same package and other packages.

Static: Statically variable

Final: constant;

The definition of the member variable, and the default value is used if the initial value is not assigned. A local variable within a function does not assign an initial value to a problem.

Member variables can be called by all member functions of the current class. The variables inside the function can only be used in the current curly braces.

1.2.1 member method access modifier return type function name (formal parameter) {function Body}

The implementation of the method also includes two parts: the method declaration and the method body.

1.2.1.1 Method Declaration

The meaning of a qualifier in a method declaration:

Static: Class method, which can be called directly from the class name

Abstract: Abstraction method, no method body

Final: Method cannot be overridden

Method declarations include method names, return types, and external parameters. The type of the parameter can be a simple data type, or it can be a composite data type.

  Method body

The method body is the implementation of the method, which includes the declaration of the local variable and all legitimate Java directives. The scope of the local variable declared in the method body is inside the method. If the local variable has the same name as the member variable of the class, the member variable of the class is hidden. In order to distinguish between parameters and member variables of a class, we must use this. This is used to refer to the current object in a method.

  Construction method

The construction method is a special method. Each class in Java has a constructor method that initializes an object of the class.

The construction method must be the same as the class name. No return type. The object is automatically invoked when it is instantiated. is generally public.

Overloads are often used to construct methods. Overloads of functions: function names are the same, parameters differ (quantity, type). Is independent of the return type. When the overloaded function is called, the function of the corresponding formal parameter is automatically searched and invoked with the case of the argument.

The constructor method can only be called by the new operator. When we're new to an object.

1. Execute the definition of the member variable.

2. The contents of the constructor.

2 , Interface:

Defined:

Interface interface Name [extends parent interface List]{

Final type constant name = value;

public void method Name (formal parameter);

}

Implementation of the interface

[Abstract] Class name extends parent class (only one) implements interface list {can be multiple, middle with ', ' separated.

When the interface is defined:

1. Interface as small as possible, too complex interface is a bad taste of the interface. --Interface separation principle.

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

Classes and Interfaces in Java

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.