What is Java object-oriented?
Java object-oriented explanation
Preface: I have been involved in project development for a long time. Recently, I have begun to have the idea of going back and writing about the basic knowledge I have learned before. First, I just started to learn programming, and I stumbled and groped forward. When I first started to learn, I didn’t understand many things at all. Later, after practicing more, some things gradually became clear; second, after a certain amount of practice, , only by going back and learning some basic things can you understand it more thoroughly; thirdly, some things are basic but very important, and they are worth doing well.
1. Object-oriented
Object Oriented is an emerging Programming method, or a new programming paradigm (paradigm), the basic idea is to use basic concepts such as objects, classes, inheritance, encapsulation, and polymorphism to design programs. Construct software systems based on objectively existing things (i.e. objects) in the real world, and use human natural thinking as much as possible in system construction.
2. Object
Object is an object used to describe objective things in the system Entity is a basic unit of the system. An object consists of a set of properties and a set of services that operate on the set of properties.
The instantiation of a class can generate objects. The life cycle of an object includes three stages: generation, use, and elimination.
When there is no reference to an object, the object becomes a useless object. Java's garbage collector automatically scans the dynamic memory area of objects, collects and releases unreferenced objects as garbage. When the system runs out of memory or calls System.gc() to require garbage collection, the garbage collection thread runs synchronously with the system.
3. Class
A class is a group of objects with the same properties and methods A collection that provides a unified abstract description for all objects belonging to the class, including two main parts: attributes and methods. In object-oriented programming languages, a class is an independent program unit. It should have a class name and include two main parts: attributes and methods.
The class implementation in Java consists of two parts: class declaration and class body.
Class declaration
[public][abstract|final] class className [extends superclassName] [implements interfaceNameList]{……}
Among them, the modifiers public, abstract, and final explain 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.
Class body
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 }//成员方法 }
Member variable qualifier meaning:
▶ static: static variable (class variable)
▶ final: constant; transient: temporary variable, used for object archiving and serialization of objects
▶ volatile: Contribution variable, used for sharing among concurrent threads
The implementation of the method also includes two parts: method declaration and method body.
Method declaration
The meaning of qualifiers in method declaration:
▶ static: class method, which can be called directly through the class name
▶ abstract: abstract method, no method body
▶ final : Method cannot be overridden
▶ native: Integrate code from other languages
▶ synchronized: Control access by multiple concurrent threads
Method declaration includes 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 memory, and the operation on the data in the method can change the value of the data.
Method body
The method body is the implementation of the method, which includes the declaration of local variables and all legal Java instructions. The scope of local variables declared in the method body is within the method. If a local variable has the same name as a class member variable, the class member variable is hidden.
In order to distinguish parameters from class member variables, we must use this. this is used in a method to refer to the current object, and its value is the object that called the method. The return value must be consistent with the return type, or exactly the same, or a subclass thereof. When the return type is an interface, the return value must implement the interface.
Construction method
▶ The construction method is a special method. Every class in Java has a constructor method that initializes an object of that class.
▶ The constructor has the same name as the class name and does not return any data type.
▶ Overloading is often used in constructors.
▶ The constructor can only be called by the new operator
4. Basic object-oriented features
Encapsulation
##Encapsulation is to hide the internal details of the object as much as possible and form a boundary to the outside. Only limited interfaces and methods are reserved to interact with the outside world. The principle of encapsulation is to prevent parts other than the object from accessing and manipulating the object's internal properties at will, thereby preventing the outside world from damaging the object's internal properties.
You can hide the information of members in a class by setting certain access rights to the members of the class.
▶ private: Members of a class that are restricted to private can only be accessed by the class itself. If the constructor of a class is declared private, other classes cannot generate an instance of the class.
▶ default: Members of a class without any access permission restrictions belong to the default (default) access state and can be accessed by the class itself and classes in the same package.
▶ protected: Members of a class that are limited to protected can be protected by the class itself, its subclasses (including subclasses in the same package and different packages) and the same Accessible by all other classes in the package.
▶ Public: Members of a class that are limited to public can be accessed by all classes.
Inheritance
The object of the subclass has all the attributes of the parent class and methods are called inheritance of the parent class by the subclass.
▶ In Java, a parent class can have multiple subclasses, but a subclass can only inherit one parent class, which is called single inheritance.
▶ Inheritance enables code reuse.
▶ All classes in Java are obtained by directly or indirectly inheriting the java.lang.Object class.
▶ Subclasses cannot inherit member variables and methods with private access rights in the parent class.
▶ Subclasses can override the methods of the parent class, that is, name member variables with the same name as the parent class.
In Java, access to parent class members is achieved through super. Super is used to refer to the parent class of the current object. There are three situations when super is used:
▶ Access the hidden member variables of the parent class, such as: super.variable;
▶ Call the parent class Overridden methods in the class, such as: super.Method([paramlist]), super() call the parent class constructor;
▶ Call the parent class constructor, such as: super([paramlist]);
Polymorphism
The polymorphism of an object means that after the attributes or methods defined in the parent class are inherited by the subclass, they can have different properties. data type or exhibit different behavior. This allows the same property or method to have different semantics in the parent class and its various subclasses. For example: the "Drawing" method of "Geometry Figure", "Ellipse" and "Polygon" are both subclasses of "Geometry Figure", and their "Drawing" method functions are different.
Java's polymorphism is reflected in two aspects: static polymorphism achieved by method overloading (compile-time polymorphism) and dynamic polymorphism achieved by method overriding ( runtime polymorphism).
▶ Compile-time polymorphism: During the compilation phase, which overloaded method is specifically called, the compiler will statically determine the corresponding method to be called based on the different parameters.
▶ Runtime polymorphism: Since the subclass inherits all attributes of the parent class (except private ones), the subclass object can be used as the parent class object. Wherever a parent class object is used in the program, a subclass object can be used instead. An object can call methods of a subclass by referencing an instance of the subclass.
Overloading
##▶ Method overloading allows the class to handle different data types in a unified way s method.
▶ Multiple methods can be created in a class, they have the same name, but have different parameters and different definitions. When calling methods, the specific number and type of parameters passed to them determines which method to use.
▶ The return value types can be the same or different. The return type cannot be used as a criterion for distinguishing overloaded functions.
Overriding
##▶ The subclass rewrites the method of the parent class. If a method in a subclass has the same method name, return type, and parameter list as its parent class, we say the method is overriding.▶ If you need the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.
▶ The access modification permissions of subclass functions cannot be lower than those of the parent class.
The above is the detailed content of What is java object-oriented. For more information, please follow other related articles on the PHP Chinese website!