Four features of Java Foundation

The last article talked about the installation of JDK and the configuration of Java environment. This article mainly talked about the features of Java.

The prime minister said that programming languages are divided into process oriented and object-oriented, and Java is an object-oriented programming language.

What is process oriented programming? Is a process as a unit, focusing on the final result. For example, when Xiao Ming wants to go from Zhengzhou to Beijing, his first step is to buy a train ticket, then take a bus to the railway station and take the train to Beijing. This is process oriented programming. You must do every step before you can have the final result.

In this way, the disadvantages of process oriented programming are obvious: 1. It is not conducive to program maintenance. 2. There is a strong correlation between functions, that is, each step you change may involve several steps of change.

So what is object-oriented? It is to regard a thing as a whole and describe it from two aspects: attribute and function

The characteristics of object-oriented programming are:

1. The idea of object-oriented programming is closer to people's normal thinking mode

2. Object oriented programming comes from life and serves life

3. The idea of process oriented will certainly be used in the idea of object-oriented programming

4. Object oriented features: abstraction, encapsulation, inheritance and polymorphism

This is about to understand what is an object.

Any specific thing is an object. In Java programming thought, it is said that everything is an object

Any object is unique, even if two similar or similar things are two different objects, just like two identical leaves can not be found in the world, any object will exist uniquely once it is created. Moreover, the object is not necessarily a real object. Any law, policy, virtual object, etc. are an object.

Abstract

Is the common attributes (characteristics) and methods of the same thing (function / behavior) to extract, summarize and summarize. For example, cars have wheels, engines, etc. These are the attributes of cars. Cars can run, carry people and load, etc. These are the functions of cars. In this way, these functions and attributes of cars can be extracted and written in a class for things like cars.

Encapsulation

Writing attributes and methods in a class in abstraction is encapsulation, and encapsulation is to ensure the security of abstract features and methods. Encapsulation is the process of packaging. Note that encapsulation is not absolute encapsulation. If other programs want to obtain encapsulated data, they can only be obtained through the interface or method specified by the program.

inherit

Features: inheritance should have a certain hierarchical structure and transmissibility

Subclasses inherit the properties and methods of the parent class, except private properties and constructor methods

In addition to the properties and methods inherited from the parent class, a subclass can also have its own properties and methods

In Java, only single inheritance is supported (that is, a subclass can only have one parent class, but a parent class can have multiple subclasses)

Whether the inheritance relationship between two classes is established can be judged by "is-a" (which is one)

Subclasses cannot inherit the construction method of the parent class: the construction method of the parent class creates the object of the parent class. It is thought that inheritance must have a certain hierarchy and have a certain amount of transitivity. If a subclass inherits the construction method of the parent class, it violates this article, so a subclass cannot inherit the construction method of the parent class.

How to implement inheritance

In Java, the extension keyword is used to implement inheritance and syntax

[access modifier] class subclass extends parent class {}

The role of inheritance

Improve code reusability

A source file can contain multiple classes, but only one class can have public. The class name of the class with public access modifier should be the same as the file name of the source file, and the main method should also be written in the class with public.

When creating a subclass object, you must first execute the construction method of the parent class to create the parent object, and then call the construction method of the child class to create the subclass object. Moreover, when creating a subclass object, whether calling a parameterized or parameterless constructor, the JVM will create the parent object by default instead of the parameterless constructor of the parent class.

polymorphic

Polymorphism refers to the multiple manifestations of the same thing in different situations

The manifestations of polymorphism are: Method rewriting, method overloading, interface and interface inheritance, class and class inheritance

Method overloading: in the same class, there are multiple methods with the same method name but different parameter lists, which is method overloading. Different parameter lists include different number, type and order of parameters. Both ordinary methods and construction methods can be overloaded. Method overloading will determine which method to call according to the passed parameters. The return values are different. In other cases, it is impossible to construct method overloading

Method Rewriting: when a subclass inherits from the parent class, the method in the parent class is inherited by the subclass. The method name, return value type and parameters are exactly the same, but the method body is different, it means that the method in the parent class is rewritten by the subclass.

Purpose: when the method in the parent class cannot meet the needs of the child class, the child class can extend the method of the parent class

Note: the scope of access modifiers of method override subclasses cannot be narrowed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>