Java Object Oriented

Posted by haironfire on Sun, 19 Dec 2021 11:33:50 +0100

Object-oriented is a programming idea, as opposed to process-oriented

Both process-oriented and object-oriented are ideas, and object-oriented is relative to process-oriented. Process-oriented, emphasizing functional behavior, with functions as the smallest unit, consider how to do it. Object-oriented encapsulates functionality into objects, emphasizes functionally enabled objects, takes class/object as the smallest unit, and considers who does it.

Classes and objects are the core concepts of object-oriented. Class is an abstract and conceptual definition of a class of things. An object is each individual of this kind of thing that actually exists and is therefore called an instance.

Object-oriented programming focuses on the design of classes, which are the design of members of classes.

One of the members of a class: attributes
Attributes are member variables, declared directly in the class, with modifiers private, static, final, etc., with default initialization values, loaded into heap space or static domain (static modifier domain)

When an object is created, various types of member variables are automatically initialized.

Class member two: method
A method is a class or object used to perform a function operation. Also known in some languages as functions or procedures. The purpose of encapsulating functionality as a method is to achieve code reuse and simplify code. Methods in Java cannot exist independently; all methods must be defined in classes.
The method is called by the method name and will not execute until it is called.

_Method is called once and executed once
In the absence of a specific return value, if the return value type is represented by the keyword void, then the return statement may not be necessary in the body of the method. If used, use only to end the method.
_When defining a method, the result of the method should be returned to the caller and handled by the caller.
Methods can only call methods or properties, not define methods within methods.

Third member of class: constructor
Characteristics of Constructors
It has the same name as the class
It does not declare a return value type. (unlike declaring void)
Cannot be modified by static, final, synchronized, abstract, native, and cannot have return statement return values

Function of constructor: Create object, initialize object

Fourth member of class: code block
Role of code blocks: Initializing Java classes or objects

Code blocks in a class can only be modified by static Decorations, called static code blocks,
Not Passed static Modified, non-static code block.
Static code block:
1. You can have an output statement.
2. Class properties, class declarations can be initialized.
3. Non-static properties cannot be initialized. That is, you cannot call non-static properties and methods.
4. If there are multiple static blocks of code, they are executed in top-down order.
5. Static code blocks execute before non-static code blocks.
6. Static code blocks are loaded as classes are loaded and executed only once.
Non-static code block:
1. You can have an output statement.
2. Class properties, class declarations can be initialized.
3. In addition to calling a nonstatic structure, you can also call static variables or methods.
4. If there are multiple non-static blocks of code, they are executed in top-down order.
5. Every time an object is created, it is executed once. And precedes the constructor.

Five members of class: internal class
When an internal part of an object requires a complete structure to describe, and the internal complete structure only serves the external thing, then the internal class is best used for the entire internal complete structure.
In Java, the definition of one class is allowed to reside inside another class, which is called an internal class and an external class.
_Inner class is generally used within the class or statement block that defines it and must be given a full name when it is referenced externally.

The role of a member internal class as a member of a class:
 Unlike external classes, Inner class It can also be declared as private or protected;
 The structure of an external class can be invoked
 Inner class Can be declared as static , but you can no longer use the outer class's non static Member variables;

Role of member internal classes as classes:
 Structures such as attributes, methods, constructors can be defined internally
 Can be declared as abstract Class, so it can be inherited by other internal classes
 Can be declared as final Of
 Build after compilation OuterClass$InnerClass.class Byte code file (also applicable to local internal classes)

Features of Local Internal Classes
Internal classes are still separate classes, which are compiled into separate classes after compilation. Class file, but preceded by the class name and the $symbol of the external class, as well as the number of the number.
Can only be used in a method or block of code that declares it, and it is declared before it is used. This class cannot be used anywhere else.
Local internal classes can use members of external classes, including private ones.
_Local internal classes can use local variables of external methods, but must be final. It is caused by the different declaration periods of local internal classes and local variables.
Local internal classes and local variables are similar in status; public,protected, default, private cannot be used
_Local internal classes cannot be decorated with static and therefore cannot contain static members

Be careful:
1. wrong static Members in a class within a member cannot be declared as static Only in external classes or static Members
 Can only be declared in an internal class static Members.
2. External classes access members of member internal classes, requiring "internal classes".Members or Internal Class Objects.Members'Way
3. A member internal class can directly use all members of an external class, including private data
4. Consider declaring an internal class static when you want to use it in the static member part of an external class

One of the object-oriented features: encapsulation and hiding
Programming pursues high cohesion and low coupling. High cohesion: the internal data manipulation details of a class are self-contained and external interference is not allowed; Low coupling: Only a small number of methods are exposed for use.

Hide the internal complexity of the object, only expose simple interfaces to the outside world for easy external calls, so as to improve the scalability and maintainability of the system.

Java operates on this property by declaring the data private and then providing a public method: getXxx() and setXxx() to accomplish the following:
Hide implementation details in a class that do not need to be exposed;
Users can only access data through pre-defined methods, which can easily incorporate control logic to limit the unreasonable operation of attributes;

Object-oriented feature two: inheritance

When there are identical attributes and behaviors in multiple classes, extracting them into a single class eliminates the need for multiple classes to define these attributes and behaviors.
Just inherit that class.

By using the keyword extends, class b can inherit class a, class b is called a subclass, and class A is called a parent or superclass.
Inheritance reduces code redundancy and improves code reuse.
The emergence of inheritance is more conducive to the expansion of functions.
The emergence of inheritance gives rise to a relationship between classes and provides a precondition for polymorphism.

_Subclasses inherit the parent class and inherit its methods and properties.
In subclasses, methods and properties defined in the parent class can be used, as well as new data and methods can be created.
In Java, the inherited keyword uses "extends", meaning that the subclass is not a subset of the parent class but an "extension" of the parent class.

/*
Instantiation of Subclass Objects
*/

public class Grandp {

	public Grandp() {
		System.out.println("Grandp Parameterless constructor");
	}
	
	public static void main(String[] args) {
		new Sun();
	}

}

class Father extends Grandp{
	public Father (String name) {
		System.out.println("Constructor 1 of the parent class, name: " + name);
	}
	public Father(String name, int age) {
		this(name);//Constructor 1 was called
		System.out.println("Constructor 2 of the parent class, age: " + age);
	}
}

class Sun extends Father{
	public Sun() {
		super("Tom",2);
		System.out.println("Sun Parameterless constructor");
	}
	
}

/*
output
*/

Grandp Parameterless constructor
 Constructor 1 of the parent class, name: Tom
 Constructor 2 of the parent class, age: 2
Sun Parameterless constructor

Object-oriented feature three: polymorphism
Polymorphism, the most important object-oriented concept, is reflected in Java:
Object Polymorphism: References to parent classes point to objects of subclasses

Java There are two types of reference variables: compile-time type and run-time type. The compile-time type is determined by the type used when the variable is declared.
The runtime type is determined by the object actually assigned to the variable. Abbreviation: When compiling, look to the left; When running, look to the right.

1 Polymorphism occurs when compile-time and run-time types are inconsistent
2 In polymorphic cases, look to the left: Look at the reference of the parent class (there is no method specific to the subclass in the parent class), "Look to the right": Look at the object of the subclass (the method that the subclass actually runs to override the parent class)

If a reference type variable is declared as the type of the parent class but actually refers to a subclass object, then the variable can no longer access the properties and methods added to the subclass.

Virtual method calls:
A method with the same parameter name as the parent class is defined in the subclass. In polymorphic cases, the method of the parent class is called a virtual method.
The parent dynamically invokes the method belonging to the subclass, depending on the different subclass objects assigned to it.
Such a method call cannot be determined at compile time.
Overload means that multiple methods with the same name are allowed with different parameters. Compiler does not use this method
 The same parameter table modifies the name of the method with the same name. For the compiler, these methods with the same name are
 Different methods. Their call addresses are bound at compile time. Java Overloads can include parent classes
 And subclasses, that is, subclasses can overload methods of different parameters of the same name as the parent class.
So: for overloading, the compiler has already determined which method to call before calling it.
This is called "early binding" or "static binding";
For polymorphism, the interpret runner will not determine what specific call to make until the moment the method is called
 Method, which is called Late Binding or Dynamic Binding.

Topics: Java JavaEE