Java object oriented programming

Time:2021-4-17

Three characteristics

1. Encapsulation, encapsulating related data into a “class” component
2. Inheritance is a kind of relationship between classes, which means that subclasses automatically share the properties and methods of the parent class
3. Polymorphism enhances the flexibility and reusability of software


class

1. The basic unit of Java language is class, similar to type.
2. Class is the abstraction of a kind of things.
3. It can be understood as template or design drawing.

object

Each object has three characteristics: object state, object behavior and object identification.
1. The state of an object is used to describe its basic characteristics.
2. The behavior of an object is used to describe its function.
3. Object identification means that an object has a unique address in memory to distinguish it from other objects.
4. Class is the abstraction of a kind of things, and object is the concrete implementation.

Using the new keyword to create an object, anonymous objects can only do one job at a time.

The relationship between class and object

1. Attribute + behavior
2. Class is used to describe things, the properties of things are treated as member variables, and the behavior is treated as member methods.


Object storage in memory

1. Generally speaking, local variables are stored in the stack, and the memory is released after the method is executed, which is characterized by first in and last out.
2. Objects (new things) are stored in the heap, and memory will be released when the objects are no longer used
3. Each element of heap memory has an address value
4. All attributes in an object have default values
Java object oriented programming


encapsulation

summary

Encapsulation refers to hiding the attributes and implementation details of objects, only providing public access to the outside world.

Benefits:
1. Improve security
2. Improve reusability

Private keyword is used to modify member variables and member functions. Privatized members can only be accessed in this class. If you want to modify or obtain member variables, you can only modify them by providing public get and set methods. If you want to call a member function, you can only call it indirectly by creating a public show method.


This keyword

concept

This represents a reference object of this class, which is equivalent to this creating an object of this class at the bottom.
This can call members of this class (member variables / methods).
In the constructor, this () must be placed on the first line.
When a local variable and a member variable have the same name, based on the principle of proximity, the local variable is preferred. If you want to use a member variable, you must use this.


inherit

concept

Inheritance is to derive new classes from existing classes. New classes can absorb the data properties and behaviors of existing classes and expand new capabilities. In life, for example, a son inherits his father’s property.

Java inheritance is a technology that uses the definition of existing classes as the basis to build new classes. The definition of new classes can add new data or new functions. It can also use the functions of parent classes, but it can’t selectively inherit parent class / super class / base class.

Improve the reusability of code, shorten the development cycle and reduce the development cost.

characteristic

1. The extensions keyword is used to implement the inheritance relationship.

2. It is equivalent to that the subclass copies the function of the parent class.

3. Java only supports single inheritance, such as is a relationship, which is a mandatory dependency relationship, also called strong coupling. This relationship is not advocated.

4. Inheritance can be passed on.

5. Cannot inherit private members and constructors of a parent class.

6. Inheritance is mostly used to modify functions. A subclass can expand functions and modify functions of the parent class while having the functions of the parent class.


Super keyword

1. The content of the parent class can be used with the super keyword

2. Super represents a reference object of the parent class

3. If used, it must appear on the first line of the call location

4. When there are no resources with the same name in the subclass and parent class, super is usually omitted. On the contrary, super must be used to call the content of the parent class


static

concept

1. Is a keyword in Java

2. Used to modify members (member variables and member methods)

characteristic

1. Can modify member variables, member methods

2. Load with the class, prior to the object

3. Only load once, it will always exist, no longer open up new space

4. Globally unique, globally shared

5. It can be called directly by the class name

6. Static can only call static, non static can call at will

7. Static cannot be shared with this or super, because there may be no object when there is static


final

concept

1. Is a keyword provided by Java

2. Final means final

3. Final can modify class, method, member variable

characteristic

1. The class modified by final cannot be inherited

2. Methods modified by final cannot be overridden

3. The variable modified by final is a constant and its value cannot be changed

4. Constant definition form: final data type constant name = value


polymorphic

concept

Polymorphism means that the same entity has multiple forms at the same time.

Mainly refers to the same object, at different times, representing different objects, refers to the object of a variety of forms.

The advantage is that different subclass objects can be regarded as parent classes, which can shield the differences between different subclass objects, write general code, make general programming, and unify calling standards.

characteristic

1. The premise of polymorphism is inheritance

2. There should be method rewriting

3. Parent class references point to child class objects, such as: animal a = new dog(); — small to large, upward transformation

4. In polymorphism, compile on the left and run on the right

5. If you want to use subclass specific methods in polymorphism, in addition to the new subclass object method, you can also use downward modeling. The idea is the same as that in variables, big to small, strong to small.
For example: animal a = new dog();

Dog d=(Dog)a;

Benefits of polymorphism

1. Polymorphism allows us to use certain methods of an object without caring about its specific type.

2. It improves the expansibility and maintainability of the program


abnormal

summary

Object used to encapsulate error information.

Composition structure: type, prompt, line number.

Exceptional inheritance structure

Throwable – top level parent class

–Error: system error, unable to repair

–Exception: Repairable error

–RunTimeException

–ClassCastException

–ClassNotFoundException

exception handling

When an exception is encountered in a program, there are usually two ways to handle it: capture or throw it up.

When a method that throws an exception is called, the calling position can continue to throw it upward without any processing, or the exception can be caught.

1. Capture method:

try{

Code to capture

}Catch (exception type, exception name){

Treatment plan}

When catching an exception, if there are still many exceptions to be handled, but you don’t know the specific exception type, you can use exception. Because all exceptions are subclasses of exception.

In the development phase, print the trace information of the stack, using the printstacktrace () method in the exception
After the project goes online, you can use output statements to show users.

2. Throw mode:

Add code to the method where an exception will occur: throws exception type (multiple exceptions can be thrown, separated by, sign)

For example: public static void main (string [] args) throws exception {}

When throwing an exception, if there are still many exceptions to handle, but you don’t know the specific exception type, you can use exception. Because all exceptions are subclasses of exception.

3. When the code is executing, if there is an exception, whether it is thrown or caught, the subsequent code will not continue to execute. At this time, we can use the keyword finally to let the following code continue to execute. Generally, finally is also used with try {} catch() {}.


abstract class

concept

Java can define a method without method body, which is implemented by its subclass. The method without method body is called abstract method, and the class with abstract method is called abstract class.

Abstract class can be understood as a special class with only method declaration but no method body.

characteristic

1. Through the Java keyword Abstract implementation

2. You can modify methods or classes

3. Abstract classes can have no abstract methods (implemented by subclasses), in order not to let the outside world arbitrarily create new objects

4. If there are abstract methods in a class, the class must be defined as an abstract class

5. When a subclass inherits an abstract class, it either remains an abstract class or rewrites all abstract methods

6. It is often used in polymorphism

7. Abstract classes cannot be instantiated

8. Although an abstract class cannot be instantiated, that is, it cannot be a new object, it also has a construction method. The purpose is to make the subclass a new object. When the subclass is in a new object, the first line of the method body will have super () by default, and the construction method of the parent class will be called


Interface

concept

Java does not allow multiple inheritance, so if you want to implement the function of multiple classes, you can implement multiple interfaces.

Java interface and Java abstract class represent the abstract type, which is the concrete expression of the abstract layer we need to propose. OOP object-oriented programming, if we want to improve the reuse rate of the program, increase the maintainability and scalability of the program, we must be interface oriented programming, abstract oriented programming, and correctly use the interface, abstract class and other useful abstract types as the top level of Java structure.

Interface interface name {code }

characteristic

1. Interfaces are all abstract methods

2. Create an interface through the interface keyword

3. The implementation is realized by subclasses through implements. The normal way to write an implementation class is to add impl after the class name. An implementation class is an abstract implementation class if it does not rewrite all the abstract methods of the interface

4. It can be understood that an interface is a special abstract class

5. Interface breaks through the limitation of Java single inheritance

6. Interfaces and classes can be implemented more (separated by commas), and interfaces can be inherited more (separated by commas)

7. Interface is the rule of external exposure and a set of development specifications

8. The interface improves the function expansion of the program and reduces the coupling

9. There are no constructors or variables in the interface

10. Constant and abstract methods in the interface can be abbreviated, and the bottom layer will automatically splice the code in abbreviated form

11. All resources in the interface are public by default

12. To implement the construction method in the class, the hidden super () calls the construction method of the object class when creating the object

13. Common methods in the interface can be modified with default (common methods in the interface can be supported in JDK1.8)


Breakpoint mode debug

Used to track and debug code during the development phase

step:
1. Set breakpoint: double click the line of the code you want to see on the left
2. Activate breakpoint mode: click the bug icon in the tool class
3. Execute one line of code: F6 in ecplise and F8 in idea
4. End breakpoint mode: in ecplise, press F8, in idea, press small red box or Ctrl + F2