Home Java javaTutorial Object-oriented features in Java language

Object-oriented features in Java language

Oct 19, 2017 am 10:13 AM
java object For

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]
  {……}
Copy after login

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}                 //成员方法
  }
Copy after login

3. Member variables

Member variables are declared as follows:

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

Among them,

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

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}                //方法体
Copy after login

The meaning of the qualifiers in the method declaration:

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

◇ 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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Square Root in Java Square Root in Java Aug 30, 2024 pm 04:26 PM

Guide to Square Root in Java. Here we discuss how Square Root works in Java with example and its code implementation respectively.

Perfect Number in Java Perfect Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Random Number Generator in Java Random Number Generator in Java Aug 30, 2024 pm 04:27 PM

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Armstrong Number in Java Armstrong Number in Java Aug 30, 2024 pm 04:26 PM

Guide to the Armstrong Number in Java. Here we discuss an introduction to Armstrong's number in java along with some of the code.

Weka in Java Weka in Java Aug 30, 2024 pm 04:28 PM

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Smith Number in Java Smith Number in Java Aug 30, 2024 pm 04:28 PM

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

Java Spring Interview Questions Java Spring Interview Questions Aug 30, 2024 pm 04:29 PM

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Break or return from Java 8 stream forEach? Break or return from Java 8 stream forEach? Feb 07, 2025 pm 12:09 PM

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

See all articles