Thinking in JAVA notes-Chapter 1 Introduction to objects and thinking

Source: Internet
Author: User

Thinking in JAVA notes-Chapter 1 Introduction to objects and thinking

This chapter introduces the basic concepts and principles of object-oriented

1.1 Abstraction

My understanding: Hiding complex and useless attributes, leaving what the program needs

1.2 each object has an Interface

Question: The concept of interfaces and implementations and why the interface mechanism is introduced (see the specific chapter)

1.3 all objects provide services

1. decompose-top-down design principle: Consider what services are needed, and then consider what services the services are dependent on, until the required object exists or is simple enough for coding.

Advantages: Easy design and high cohesion (fit together)

2. One of the OOP design principles: make the object well complete a task, do not do too many tasks (design more objects to complete together)

Advantages: easy to reuse and easy to read

1.4 hide specific implementations

1. Separated class creators (API providers) and class users (client programmers)

Advantages: 1. It is easy to change internal implementation without affecting external use (immutable)

2. Reduce client program bugs (Security)

2. access control: only provides services and denies internal access.

3. Java uses three keywords to set Access Control

Public and private: only the class can be used internally. protected: inside the class, the same package and descendant classes can be used internally.

4. default access permission: if you do not know the keyword, you can access it in the package and reject it outside the package (actually equivalent to protected ?)

1.5 reuse of Reuisng (actually speaking, combination)

1. Combination/aggregation: Objects of other classes are used in the class, and the "has-a" relationship

2. Flexibility: the class objects are generally private and can be changed freely and dynamically.

3. OOP should first consider combination, and then inherit to avoid overly complicated design (related books such as design patterns and concepts of frameworks and design patterns)

1.6 inheritance

1. Inheritance: based on existing classes, copying, adding, and changing

2. A class can describe features and behaviors, and there is still a relationship between classes.

3. Subclass inherits all features, behaviors (including private types that cannot be operated) and interfaces of the parent class. All messages of the parent class can act on the subclass. The "is-a" relationship

4. Subclass can have new features and behaviors. More importantly, subclass can override/overriding the parent class method.

1.6.1 "Is a" relationship with "like" is a, Is lika

Is-a: there is no new method for subclass-substitution principle

Is-like-a: Add a new method to the subclass-not purely substitution

In my understanding, is-a is used to implement interfaces of the parent class using different methods. is-lika-a is suitable for parent classes that are abstract and child classes that are more specific, new Features and behaviors need to be added

1.7 Polymorphism

1. The subclass calls the parent class method. The Compiler does not determine which code to execute during the compilation time, and the object will execute the appropriate code.

2. Non-oop, pre-binding, absolute entry address for the compiler to generate Function Code

Oop: later binding, the compiler knows that the function exists, the execution parameters and return values are persistent, but the specific code is not clear, so that the running time can know the Execution Code. By default, java is dynamically bound. For details, see the section on polymorphism.

3. the upward Transformation (upcasting) transmits the subclass object to the method that requires the parent class object, which is safe and logical. The multi-State mechanism automatically determines the execution of the code at runtime, making the program design simpler and easier to expand.

1.8 single inheritance Structure

 

  

Related Article

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.