JAVA learning summary - object-oriented

Foreword: The knowledge in java object-oriented can be said to be the core part of the entire java foundation. It has been almost 2 months since I learned it, and it is time to review it. When I first started learning, I was very confused. After this summary, I have a lot of new insights. This is the so-called "everytime you know something, you will be happy to forget about it! Haha~"

 

1. What is a class? What is an object?

In simple terms, a class is an abstraction of the behavior and characteristics of a certain type of thing, and encapsulates it, the characteristics are called attributes, and the behaviors are called methods;

An object is a concrete instance of this class, and the object has all the properties and methods of this class

 

for example:

All people are a class, the attributes that people have: height and weight..., the methods that people have: eating, sleeping...

Specifically, a certain person is an object. For example, Xiaoming is an object. Xiaoming has height and weight, and can eat and sleep..

 

This is how objects are described in java programming thinking:

Objects have state, behavior , and identity . This means that each object can have internal data (they give the object's state) and methods (they produce behavior), and each object can be distinguished from other objects , Specifically, each object has a unique address in memory.

 

2. What is object-oriented? Why object-oriented?

Official word:

Object-oriented (Object Oriented) is an emerging programming method, or a new programming specification (paradigm), its basic idea is to use the basic concepts of objects, classes, inheritance, encapsulation, polymorphism and other basic concepts for programming . The software system is constructed from the objectively existing things (that is, objects) in the real world, and the natural way of thinking of human beings is used as much as possible in the system construction. Object-oriented is to solve the maintainability, extensibility, reusability of the system....

My understanding:

First of all, object-oriented is a way of designing programs, and it is a design idea.

Secondly, object-oriented saves code, has clear ideas, and is easy to expand. The objects you create are to provide you with services.

 

4, the three major characteristics of object-oriented:

1. Packaging

   Encapsulation is the first step in implementing object-oriented programming. Encapsulation is to put properties and methods in a class. When providing services to the outside world, hide the internal details of the object as much as possible, and only retain limited interfaces and methods;

  The meaning of
  encapsulation: The meaning of encapsulation is to protect or prevent code (data) from being accidentally destroyed by us. In object-oriented programming, a property is seen as a central element and is tightly integrated with the method that uses it, thereby protecting it from accidental modification by other methods.

  By setting certain access rights to the members of the class, the information hiding of the members in the class can be realized.

  • private: members of a class that are limited to private and can only be accessed by the class itself . If a class's constructor is declared private, other classes cannot generate an instance of that class.
  • default: Members of the class without any access 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 accessed by the class itself, its subclasses (including subclasses in the same package and in different packages), and all other classes in the same package.
  • public: The members of the class are limited to public and can be accessed by all classes .

     

Second, inheritance

Java classes can be divided into three categories:

  • Class: Defined using class, no abstract methods
  • Abstract class: defined using abstract class, with or without abstract methods
  • Interface: Defined using inerface, there can only be abstract methods, (or none), and the variables in the interface are modified by public static final by default

The following relationships exist between these three types:

  • Classes can extend: classes, abstract classes (all abstract methods must be implemented), but only one can extend, and multiple interfaces can be implemented (all interface methods must be implemented) ---> single inheritance and multiple implementations
  • Abstract classes can extend: classes, abstract classes (can implement all, part, or no abstract methods of the parent class), can implement multiple interfaces (can implement all, part, or no interface methods at all)
  • An interface can inherit from multiple interfaces

What subclasses get after inheritance:

  • The subclass directly owns the non-private properties and methods of the parent class
  • Subclasses can add their own properties and methods, that is, extend the parent class
  • Subclasses can override superclass methods

About the construction method:

  • Constructors cannot be inherited , subclasses can explicitly call the superclass's constructor through super()
  • When creating a subclass object, the compiler will automatically call the no-argument constructor of the parent class
  • If the superclass does not define a no-argument constructor, the subclass must explicitly call super() on the first line of the constructor
  • The class has a parameterless constructor by default. If other parameterized constructors are defined, the parameterless constructor will be invalid. Therefore, the parent class does not define a parameterless constructor, which does not mean that the parent class has not written a parameterless constructor. 

In Java, super is used to access the members of the parent class, and super is used to refer to the parent class of the current object . There are three situations in which super is used:

  • Access hidden member variables of parent class
  • Call the overridden method in the parent class
  • Call the constructor of the parent class

Meaning of inheritance:

  • Code reuse is one point, and the most important thing is upcasting, that is, the reference variable of the parent class can point to the subclass object, which is the basis of the most important feature of Java object-oriented polymorphism

 

Three, polymorphism

 

Before understanding polymorphism, understand the following points:

What is the same method:

 

  • A method can be composed of: modifier + return value + method name + parameter + exception of throw + method body 6 parts
  • Among them, only the method name and parameters are unique identifiers, which means that as long as the method name and parameters are the same, they are the same method
  • The so-called same parameters refer to the same number, type and order of parameters, any of which is different is a different method

What is overloading:

  • Overloading means that there are methods in a class (including the methods of the parent class) with the same method name but different parameters. The parameters can be different in the number, type or order of the parameters.
  • If only the exceptions of modifiers, return values, and throws are different, then these are two identical methods, which cannot be compiled, let alone overloaded.

What is rewrite:

  • The existence of the same method in the subclass as that of the parent class is an overriding. What is the same method, please keep in mind the previous description. The method name and parameters are the same, including the number, type, and order of parameters.

Rewritten rules:

  • The subclass cannot override the parent class's private method , and private is invisible to the subclass. If the subclass defines a method that is the same as the parent class's private method, it is a new method.
  • The modifier permission of the overridden method must be greater than or equal to the modifier permission of the overridden method (public > protected > default > private)
  • The exception thrown by overriding needs to be the same as the parent class or a subclass of the parent class exception, or the overriding method simply does not write throws
  • The return value of the overridden method must be the same as that of the overridden method, otherwise a compilation error will be reported
  • Static methods cannot be overridden as non-static methods , otherwise a compilation error occurs

What is polymorphism?

The same operation acts on different objects and can produce different execution results, which is polymorphism.

Polymorphism in Java is reflected in two aspects: static polymorphism (compile-time polymorphism) implemented by method overloading and dynamic polymorphism (run-time polymorphism) implemented by method overriding.

  • Compile-time polymorphism: During the compilation phase, which overloaded method is called, the compiler will call the corresponding method according to the different parameters .
  • Run-time polymorphism: When the parent class reference refers to the subclass object, the type of the referenced object determines whose method is called. The called method must be defined in the parent class, that is to say, it is reused by the subclass. write method
1  class Animal {
 2      public  void speak() {
 3          System.out.println("I am an animal" );
 4      }
 5  }
 6  
7  class Dog extends Animal {
 8      public  void speak() {
 9          System.out.println( "Wang Wang..." );
 10      }
 11  }
 12  
13  class Cat extends Animal {
 14      public  void speak() {
 15          System.out.println("Meow...");
 16      }
 17  }
 18  
19  public  class Test {
 20      public  static  void main(String args[]) { 21          Animal animal = new Dog();
 22          animal.speak(); // barking... 
23          animal = new Cat(); 
 24          animal.speak(); // Meow... 
25     }
 26 }

A slightly more complicated case:

1  class Animal {
 2      public  void speak1() {
 3          System.out.println("I am an animal.." );
 4          speak2();
 5      }
 6      public  void speak2() {
 7          System.out.println("I am is a ferocious animal" );
 8      }
 9  }
 10  
11  class Dog extends Animal {
 12      public  void speak1(String name) {
 13          System.out.println("Wang..." );
 14      }
 15      public void speak2() {
 16          System.out.println("I am a Teddy" );
 17      }
 18  }
 19  
20  public  class Test {
 21      public  static  void main(String args[]) {
 22          Animal animal = new Dog ();
 23          animal.speak1(); 
 24          // I am an animal..
 25          // I am a teddy 
26      }
 27 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474291&siteId=291194637