Object-Oriented java programming

Source: Internet
Author: User

Object-oriented features: inheritance, encapsulation, and Polymorphism
Inheritance definition: defines a new class named inheritance based on a class. The original class is called parent class, and the new class is called subclass.
Inheritance features:
1. subclass has attributes and methods of the parent class
2. Subclass can have its own new attributes and Methods
3. Subclass can override (overwrite) the method of the parent class
4. You can declare the parent class and create a subclass.
4.1. Only attributes and methods of this type can be called for the declared type (the created subclass has its own method and cannot be called)
4.2. What type is created and what type of method is run (essentially the object created)
4.3. What type is created, you can convert it to what type
Final (modifier class, attribute, method)
When a class is modified with final, this class cannot be inherited.
When a method is modified with final, it indicates that this method cannot be overwritten.
When an attribute variable is modified with final, it represents a constant. (Both static and uppercase letters are used ).
Note the following for Constructor:
1. Creating a subclass will certainly call the constructor of the parent class.
1.1. If no constructor is created for the subclass parent class, the subclass implicit constructor calls the parent class implicit constructor.
2. The subclass calls the non-argument (explicit or implicit) constructor of the parent class by default.
3. Subclass can only call the constructor existing in the parent class.
4. Use super to call the parent class constructor In the subclass constructor, and the super must be placed in the first line.
Encapsulation definition: Hiding attributes. The process of implementation details of methods and methods is called encapsulation.
Knowledge Used for Java Implementation encapsulation: Methods, packages, and access modifiers.
Access modifier:

Public> protected> default> private
Entity class:
The class name is the same as the database table name. The attributes in the class are the same as those in the database table field name, and the attributes are private. There are two public get and set methods corresponding to them, we call this class an entity class.
Object-oriented polymorphism:
Method overload: Several Methods in the class, with the same method name and different parameter lists
(For access modifiers, if the subclass overrides the method of the parent class, the access modifier can only be equal to or wider than the parent class .)
The two most direct manifestations of polymorphism are method rewriting and method overloading.
Abstract class: only the declaration of methods, no implementation of methods.
The subclass inherits from the three choices of the abstract class:
1. Implement all abstract methods defined by the parent class.
2. Define the custom method as abstract instead of implementing the abstract method of the parent class.
3. implement some abstract methods (some are not implemented) and define yourself as an abstract class.
Interface: (the interface uses the interface keyword declaration. All methods in the interface are abstract methods. Only the method declaration does not have the implementation of the method, but there is no abstract keyword)
Interface feature 1: The interface can only be declared and cannot be created. Create an interface implementation class
Interface Feature 2: one class can implement multiple interfaces
Interface feature 3: A class can inherit both classes and implement interfaces, but must be inherited before implementation
Interface Feature 4: interfaces can inherit interfaces, and one interface can inherit multiple interfaces
Two special interfaces:
Constant interface: defines several static final constants of the class, and there is no method.
Id interface: it is also known as an empty method interface. An interface does not contain any declaration of methods and variables.
Interface-oriented programming:
Interface-Oriented Programming core: what we care about in our project is not a class or an object, but whether this class or this pair has implemented the interface we defined.
Interface-Oriented Programming: declares an interface, creates an implementation class, and declares an interface for method parameters to truly run the transfer implementation class.
Advantages of interface-Oriented Programming: The program is flexible, easy to expand, maintenance and upgrade, and the program can be compiled with the interface.
Appendix: chat Polymorphism
First, polymorphism is a variety of forms, two manifestations: Method overloading method rewriting. (The interviewer has made a big difference between the concept of method overloading and method rewriting)
Second, declare the parent class and create a subclass as a multi-state service.
Third: abstract class. The interface is a multi-state service. Declares an abstract class, creates a subclass, declares an interface, and creates an implementation class. Abstract classes and interfaces can be understood as a special parent class. Here, we can understand the abstract methods that implement the abstract class or interface in the subclass as the method to override the parent class (method rewriting). Therefore, this is also a polymorphism, or the abstract class and interface are also a polymorphism service.
This article is from the "cainiao growth note" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.