Thinking in Java,fourth Edition (Java programming Idea, Fourth edition) learning Notes (ii) Introduction to Objects

Source: Internet
Author: User

The genesis of the computer revolution is a machine. The genesis of out programming languages thus tends to look like that machine.

The computer revolution originated in machines, so the production of programming languages began with the imitation of machines.

Computers is mind amplification tools and a different kind of expressive medium. A computer is a tool for the brain to extend, or a different type of Expression Media.

Object-oriented Programming (OOP) is part of this movement toward using the computer as an expressive medium.

Object-oriented programming is an integral part of the megatrends that make computers a medium of expression.

The chapter is background and supplementary material.

There is many concepts that is introduced here to give you a solid overview of OOP.

Many people do not feel confortable wading to object-oriented programming without understanding the big picture first. However, other people could not get the big picture concepts until they ' ve seen some of the mechanics first.

The progress of Absraction

It can be argued, the complexity of the problems you ' re able to solve are directly related to the kind and qua Lity of abstraction.

Assembly language is a small abstraction of the underlying machine

Many so-called "imperative" languages that followed (such as FORTRAN, BASIC and C) were abstracions of assembly language.< c1> but their primary abstraction still requires all think in terms of the structure of the computer rather than the Struc Ture of the problem you is trying to solve.

The programmer must establish the association between the machine model and the model of the problem it is actually bein G solved, and the fact that it's extrinsic to the programming language, produces programs that's difficult to write and Expensive to maintain, and as a side effect created the entire "programming methods" industry.

The alternative to modeling of the machine was to model the problem you ' re trying to solve, such as "all problems be ultimate LY lists ", or" all problems is algorithmic "; Each of these approaches is a good solution to the particular class of problem they ' re designed to solve,but they would Become awkward when you step outside of the domain.

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the proble M space. This representation are general enough, the programmer is not constrained to any particular type of problem

Elements in the problem space represent as objects in the solution space. (you'll also need other objects, that don ' t has problem-space analogs)

  OOP allows describe the problem in terms of the problem.

Each object look quite a bit like a little computer--it have a state, and it had operations that you can ask it to perform.

Alan Kay summarized five basic characteristics of Smalltalk, the first successful objecct-oriented language and one of the Languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming:

1. Everythiing is an object.

2. A program is a bunch of objects telling each of the other "do by sending messages.

You can think the a message as a request to call a method, belongs to a particular object.

3. Each objects have its own memory made up of the other objects.

4. Every object has a type.

The most important distinguishing characteristic of a class are "What messages can I send to it?"

5. All objects of a particular type can receive the same messages.

 A loaded statement a statement of meaningful

Substitutability is one of the powerful concepts in OOP.

An object has a state, behaviore and identity.

An object have an interface (this interface should refer to the API, that is, the method)

What we really does in object-oriented programming are create new data types.

A class describes a set of objects that has identical characteristics (data elements) and behaviors (functionality).

 Any program is a simulation of the system you ' re designing?? What system

Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the Program space and objects in the solution space.

Each object can satisfy only certain requests. The requested you can make of an objects be defined by its interface, and the type was what determines the interface.

An object provides services

Objects is "service providers". Your Prgram itself would provide services to the user, and it would accomplish this by using the service offered by other OB Jectes.

 Your goal is to produce (or even better, locate in existing code libraries) a set of objects that provide the ideal Servic Es to solve your problem.

Thinking of an object as a service provider have an additional benefit:it helps to improve the cohesiveness of the object.

One problem people has when designing objects are cramming too much functionality into one object.

In a good the object-oriented design, each object does one thing well, but doesn ' t-try to do too much.

The hidden implementation

Class creators and Client Programmers

The goal of the client Programms is to collect a toolbox full of classes to user for rapid application development.

The goal of the class creator is to bulid a class this exposes only what's necessary to the client programmer and keep Eve Rything else hidden. Then the class creator can change the hidden portion at would without worrying about the impact on anyone else.

The reason of access control:

1. Keep client Programmer ' s hands off portions they shouldn ' t touch.

2. Allow the Library designer to change the internal working of the class without worrying on how it would affect the CL Ient programmer.

Java uses three explicit keywords to set the boundaries in a class:public, private, and protected.

public--The following element is available to everyone.

Private--no one can access that element except you, the creator of the type, inside methods of this type.

A brick wall between class creator and the client programmer.

protected--acts like private, with the exception, a inheriting class have access to protected members.

    "Default" Access--Java also has a "default" access, which comes to play if you don ' t use one of the Aforementi oned specifiers.

Usually called package access because classes can access the members of other classes in the same package (library com Ponent), but outside of the package those same members appear to be private

Reusing the implementation

Code reuse is one of the greatest advantages that object-oriented programming language provide.

The Reuse a class:

1. Just use an object of the class directly

2. Place an object of the that class inside a new class. (Creating a Member object)

This concept is called composition. (If the composition happens dynamically, it ' s usually called aggregation). Composition is often referred to as a "has-a" relationship.

Composition combination: Indicates that two objects are strong relationships between the whole and the part, and that part of the life cycle cannot go beyond the whole, or be absent from the whole. The "part" of a combined relationship cannot be shared across the whole.

Aggregation aggregation: Indicates that two objects are weak relationships between the whole and the part, and part of the life cycle can go beyond the whole. such as computers and mice.

Composition comes with a great deal of flexibility. The member objects of your new class is typically private.

1. Allows those member without disturbing existing client code.

2. You can all change the member objects at run Time,to dynamically changed the behavior of your program

Inheritance doesn ' t has the flexibility.

Should first look to composition when create new classes,since it's simpler and more flexible

Inheritance

Inheritance expresses this similarity between type by using the concept of base types and derived types.

You create a base type to represent the core of your ideas on some objects in your system. From the base type, you derive other types of the express the different ways, which this core can be realized.

Casting the solution in the same terms as the problem are very useful,not need a lot of intermediated models

The type equivalence via inheritance is one of the fundamental gateways in understanding the meaning of object-oriented PR Ogramming.

The differentiate your new derived class from the original base class:

1. Simply add brand new methods to the derived class.

2. Change the behavior of a existing Base-class method. (overriding)

Is-a vs. Is-like-a relationships

A test for inheritance are to determine whether you can state the is-a relationship about the class and has it make sense.

Is-a: Inheritance override only base class methods. (Pure substitution)

Is-like-a: Add new methods to a derived type

Interchangeable objects with polymorphism

Type hierarchies classification structure

When dealing with type hierarchies, you often want to treat a object not as the specific type that it was, but instead as Its base type.

Early binding pre-bind

In OOP, the program cannot determine the address of the "the" code until run time, so some other scheme was necessary when a mess Sent to a generic object.

To solve the program, object-oriented languages use the concept of late binding.

Early binding:the compiler generates a call to a specific function name, and the Runtim system resolves the A Bsolute address of the code to being executed.

Late Binding:java use a special bit of code in lieu of the absolute call. This code calculates the address of the method body, using information stored in the object. (Dynamic binding)

In java,dynamic binding are the default Behaviore and you don ' t need to remember to add any extra keywords (c + + uses Virtua l keyword) in the order to get polymorphism.

Upcasting (upward transformation)-the process of treating a derived type as though it were its base type

The name "cast" is used in the sense of casting into a mold

The ' up ' comes from the inheritance diagram are typically arranged, with the base type at the top and the derived C Lasses Fanning out downward.

The singly rooted hierarchy (single inheritance structure)

In Java, all classed be ultimately inherited from a single base class, and the name of this ultimate base class is simply Object.

Containers (Collection)

In Java, the library have different types of containers for different needs:list (to hold sequences), Map (to associate OB Jects with other objects), Set (for each of the type of object), and more components such as queues, trees, stacks, E Tc.

There is reasons that need a choice of containers:

1. Containers provide different types of interfaces and external behavior.

2. Different containers has different efficiencies for certain operations.

parameterized types (generics)

Befor Java SE5, containers held the one universal type in Java:object. The singly rooted hierarchy means that everything are an object, so a container to hold Object can hold anything. But, sine the container held is only Objects and when you added a object reference into the

Container it is upcast to Object, and thus losing its character. So you need downcasting, but it's not safe. It ' s not completely dangerous, however, because if you downcast to the wrong thing you ' ll get a runtime error called an EX Ception.

Downcasting and the runtime checks require extra time for the running program and extra effort from the programmer.

The solution is called a parameterized type mechanism. (Java se5,called generics)

A parameterized type (compiler-level technology) is a class, the compiler can automatically customize to work with particular Types.

Object Creation & Lifetime

When to destroy object?

C + + takes the approach that control of efficiency are the most important issue, so it gives the programmers a choice.

Java uses dynamic memory allocation

Garbage collector

With Java, the garbage collector are designed to take care of the problem of releasing the memory (although this does ' t Inc Lude other aspects of cleaning up an object).

Exception handling:dealing with errors

expection handling isn ' t an object-oriented feature.

Concurrent Programming

There is a catch in concurrency:shared resources.

To solve the Problem:task locks a resource, completes it task, and then releases the lock.

Java and the Internet

  

Thinking in Java,fourth Edition (Java programming Idea, Fourth edition) learning Notes (ii) Introduction to Objects

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.