Home  >  Article  >  Java  >  Object-oriented features in Java language

Object-oriented features in Java language

一个新手
一个新手Original
2017-10-19 10:13:031978browse

Basic characteristics of object-oriented

1. Encapsulation

Encapsulation is to combine the properties and services of an object into an independent and identical unit, and to hide the internal details of the object as much as possible. It contains two meanings:

 ◇ Put all the objects Properties and all services are combined to form an indivisible independent unit (the object).

 ◇ Information hiding, that is, hiding the internal details of the object as much as possible, forming a boundary (or forming a barrier) to the outside world, and retaining only a limited external interface to connect it with the outside.

The reflection of the principle of encapsulation in software is: it requires that parts other than the object cannot access the internal data (properties) of the object at will, thereby effectively avoiding "cross-infection" of external errors and making it Software errors can be localized, greatly reducing the difficulty of error detection and troubleshooting.

2. Inheritance

The object of a special class has all the properties and services of its general class, which is called the inheritance of the general class by the special class.

A class can be a special class of multiple general classes. It inherits properties and services from multiple general classes. This is called multiple inheritance.

In the Java language, we usually call the general class the parent class (superclass, super class), and the special class the subclass (subclass).

3. Polymorphism

Polymorphism of objects means that after properties or services defined in a general class are inherited by a special class, they can have different data types or show different behaviors. This allows the same property or service to have different semantics in the general class and its various special classes. For example: the "Drawing" method of "Geometry Figure", "Ellipse" and "Polygon" are both subclasses of "Geometry Figure", and their "Drawing" method functions are different. Object-oriented programming method

OOA-Object Oriented Analysis

OOD-Object Oriented Design

OOI-Object Oriented Implementation Object-oriented implementation

Object-oriented features of Java language

Class is an important composite data type in Java and is the basic element that makes up a Java program. It encapsulates the status and methods of a class of objects and is the prototype of this class of objects. The implementation of a class consists of two parts: class declaration and class body

1. Class declaration:

 [public][abstract|final] class className [extends superclassName] [implements interfaceNameList]
  {……}

Among them, the modifiers public, abstract, and final describe the attributes of the class, className is the class name, superclassName is the name of the parent class of the class, and interfaceNameList is the list of interfaces implemented by the class

2. Class body

The class body is defined as follows:

class className
  {[public | protected | private ] [static]
  [final] [transient] [volatile] type
  variableName;                 //成员变量
  [public | protected | private ] [static]
  [final | abstract] [native] [synchronized]
  returnType methodName([paramList]) [throws exceptionList]
   {statements}                 //成员方法
  }

3. Member variables

Member variables are declared as follows:

  [public | protected | private ] [static]
  [final] [transient] [volatile] type
  variableName;                 //成员变量

Among them,

  static: 静态变量(类变量);相对于实例变量
  final: 常量
  transient: 暂时性变量,用于对象存档,用于对象的串行化,见对象的串行化一节
  volatile: 贡献变量,用于并发线程的共享

4. Member method

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

  [public | protected | private ] [static]
  [final | abstract] [native] [synchronized]
  returnType methodName([paramList])
  [throws exceptionList]            //方法声明
   {statements}                //方法体

The meaning of the qualifiers in the method declaration:

  static: 类方法,可通过类名直接调用
  abstract: 抽象方法,没有方法体
  final: 方法不能被重写
  native: 集成其它语言的代码
  synchronized: 控制多个并发线程的访问

◇ Method declaration

The method declaration includes the method name, return type and external parameters. The type of the parameters can be a simple data type or a composite data type (also known as a reference data type).

For simple data types, Java implements value transfer. The method receives the value of the parameter, but cannot change the value of these parameters. If you want to change the value of a parameter, use a reference data type, because the reference data type passes to the method the address of the data in the memory, and the operation of the data in the method can change the value of the data (Shanghai Shangxuetang java training original, please read more Pay attention, follow-up JAVA related technical articles will be provided one after another).

The above is the detailed content of Object-oriented features in Java language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn