Java Object-Oriented foundation

Source: Internet
Author: User
Tags modifiers

Object-oriented (OOP)

1. Classes and objects.

Class:

Mainly consists of two parts: attributes (Attributes), function (method)

Properties: describes the characteristics of the type.

property, which is the variable inside the class.

Variable:

Can be divided into member variables and local variables. (mainly based on its scope )

member Variable : It can also be called a global variable. can be called anywhere inside the class.

Local Variables : called only in the method block that you define.

&& If the variable name of a local variable is the same as the member variable , The local variable is called first by the nearest principle. If you want to invoke a member variable, you need to use this. Variable name "for access.

method: Describes the functionality that the type has.

The method can be divided into the method of parameter and the non-parameter method.

a method of participation : The method with parameters. His parameters are called formal parameters (formal parameter). What are formal parameters? This is the parameter that you need to use in this method, or something to say. He exists only in form, or in imaginary things. Did not actually give him the value of the assignment. (finally , the argument is assigned a value, and this parameter has practical significance.) )

No parameter Method : A method without parameters.

creation of a method : (contains a parameter, no parameter)

Method Modifiers return value type Method Name (parameter type parameter name,... ) {

Method statement block (the logic you need to execute)

}

Object :

The entity of the class . Need to be created with the new keyword. (A class is abstract and objects are actually present.) Therefore, the object is the instantiation of the class .

The property method of the class , in fact, is the property method of the object . There will be more than one object in a class. The two are inseparable. But if you want to invoke the property method of the class, you must first create the object .

&& Object-oriented : The object you are targeting, to invoke the property method in the class to which he belongs . When you create a new object, all of his actions are already done in the class , and the objects that we see are shown.

Construction Method:

Definition: The method name is the same as the class name, and there is no return value type.

The constructor method can only be called at New , and is used to initialize the object.

This keyword :

Represents an instance of the current class. Used to differentiate between member variables and local variables, which must be placed on the first row when the constructor method is called. Calls to each other are not allowed.

Final keyword :

indicates "final", "non-modifiable". Used to decorate non-abstract classes , non-abstract class member methods , and variables. A final modified variable is a constant and must be assigned a value. Naming specification All letters need to be capitalized.

Static keyword:

Static modifiers in Java that can be used to decorate member variables (static variables ---->" Class variables , called with the class name directly), member Methods (static methods ----> class methods - not included This keywords) and code block (static code block).

A static method can only call a static method and cannot call a non-static method.

Super keyword:

Super is used to point to a reference to the parent class, and you can use the super keyword to invoke the non-private properties and methods of the parent class. Must be placed on the first line.

Object

ancestor classes of all classes, as long as they are The class in Java is his subclass.

Object is used to reclaim all objects.

Method overloads:

Method names are the same, method parameters are different. And method permissions, the return value is irrelevant.

method Overrides:(method Overrides)

method Name, return value, parameter list exactly the same. The subclass permission must be greater than the parent class.

&& Difference:

The concepts are different, overloaded in this class, overridden in subclasses and parent classes.

2, object-oriented thinking.

The three core ideas of object-oriented are: encapsulation , inheritance , polymorphism. (For class implementations, object-independent)

Package :----> Get,Set method.

Hide Implementation . --(Hide attribute method, implementation details)

Permissions: public > protected > default > private

    

Benefits: 1, code security, easy to modify.

2, reduce the complexity of software development

3. Avoid naming conflicts

Inheritance : ----> extends

Derive another class from one class that inherits all non-private properties and methods of the parent class.

Java can inherit only one single inheritance. A child class can have only one parent class.

Inheritance Relationship: Is-a, An object is a classification of another object.

Benefits: 1, simplify the understanding of things, clearly reflect the relationship between the structure of related classes.

2, provides the reusability of software, reduces the redundancy of code and data, and increases the reusability of the program.

3, by enhancing the consistency to reduce the interface between the modules and interface, greatly increasing the ease of maintenance of the program.

polymorphic: (Compile period see left, run period see right)

means that objects of different classes are allowed to respond to the same message . That is, the same message can be used in a variety of different ways depending on the calling object.

How to achieve:

1, to have inheritance.
2, to have a rewrite.
3. The parent class reference points to the subclass object. Upward transformation.

Benefits:

1, replaceable.

2. expandability.

3, interface.

4, flexibility.

5, simplification.

instanceof Keywords:

used to determine if the object on the left side of the instanceof belongs to an instance of the right class or subclass.

abstract class: ----> Abstract

A class does not have enough information to describe a specific object, but it requires other classes to support it, which is called an abstract class .

&& Precautions:

1. abstract methods must be in an abstract class .

2. Abstract classes cannot instantiate objects as new.

3. The abstract method must be overridden by subclasses .

4. a class with an abstract method must be defined as an abstract class . There are other ways to do this.

5. Abstract methods in subclasses cannot have the same name as the parent class abstract method.

6.abstract cannot be modified in the same way as final .

7. Abstract cannot be associated with private,static,final , or native the same method is decorated in parallel.

interface: ----> Interface----> Implements Implementation Interface

a set of methods that can only be included in an interface Abstract Methods and constants .

Java can only be inherited in a single, but more than one implementation.

naming rules: I Start or able end.

Interface Features:

1, the interface cannot be instantiated.

2, member variables can only be constants, coercion modifier:public static final , cannot with private and protected keyword coexistence.

3, no construction method.

4. The member method can only be an abstract method

5, A class can implement multiple interfaces, an interface can inherit multiple interfaces. "," delimited.

Benefits:

1. Java cannot inherit more, but it can be implemented more.

2, because the interface is an abstract method, the code is less coupled.

The necessity of the interface:

Scalability and maintainability.

&& The difference between an abstract class and an interface:

Member Differences:

Abstract Classes : There are construction methods, abstract methods, and other methods and properties.

interfaces: No construction methods, only abstract methods and constants

Relationship differences:

Classes and classes:

inheritance , can only be single-inheritance, multi-level inheritance.

Classes and Interfaces:

The implementation of the relationship , can be implemented alone, or more.

Interfaces and interfaces:

inheritance , which can be either single-inheritance or multiple-inheritance.

Different design concepts:

abstract class : is-a , the generic function is defined in the abstract class.

Interface : has-a , the extension function is defined in the interface.

  Design Pattern 6 Principles:

    1. Richter Replacement: subclasses do not rewrite the parent class's implementation method to override the parent class's abstract method.
2. Single principle: A class only does one function, one way to do it.
3. Open and close principle: Do not modify a class, but to inherit him, in going to expand.
4. Dimitri rule: at least know the principle, only communicate with friends (do not create objects in the local).
5. Interface Isolation: do not implement an abstract method in the presence of useless interfaces
6. Dependency inversion : Do not rely on specific classes, but rely on interfaces

Java object-oriented fundamentals

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.