Programming language evolves always along with Compiler's evolvement

JVM as Additional Indirection

Introduction to Object

  • Everthing is an object. Think of an object as a fancy variable; it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem you’re trying to solve (dogs, buildings, services, etc.) and represent it as an object in your program.
  • A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular object.
  • Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects
  • Every object has a type. Using the parlance, each object is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?”
  • All objects of a particular type can receive the same messages. This is actually a loaded statement, as you will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerful concepts in OOP.
  • Booch offers an even more succinct description of an object: An object has state, behavior and identity.
  • An object has an interface: Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called objects or instances in object-oriented parlance) and manipulate those variables (called sending messages or requests; you send a message and the object figures out what to do with it).
  • The preceding diagram follows the format of the Unified Modeling Language (UML). An object provides services: While you’re trying to develop or understand a program design, one of the best ways to think about objects is as “service providers.”
    • Each class is represented by a box, with the type name in the top portion of the box,
    • any data members that you care to describe in the middle portion of the box,
    • and the methods (the functions that belong to this object, which receive any messages you send to that object) in the bottom portion of the box.
    • Often, only the name of the class and the public methods are shown in UML design diagrams, so the middle portion is not shown. If you’re interested only in the class name, then the bottom portion doesn’t need to be shown, either.
  • Thinking of an object as a service provider has an additional benefit: it helps to improve the cohesiveness of the object. High cohesion is a fundamental quality of software design: It means that the various aspects of a software component (such as an object, although this could also apply to a method or a library of objects) “fit together” well.
  • In a good object-oriented design, each object does one thing well, but doesn’t try to do too much.
  • Treating objects as service providers is a great simplifying tool, and it’s very useful not only during the design process, but also when someone else is trying to understand your code or reuse an object—if they can see the value of the object based on what service it provides, it makes it much easier to fit it into the design.
  • The hidden implementation: It is helpful to break up the playing field into class creators (those who create new data types) and client programmers (the class consumers who use the data types in their applications).
  • IOD(Interface-Oriented Design), Interface-Orented Programming
  • Keeps everything else hidden. Why? Because if it’s hidden, the client programmer can’t access it, which means that the class creator can change the hidden portion at will without worrying about the impact on anyone else. The concept of implementation hiding cannot be overemphasized.
  • Everything’s naked to the world.
  • The interface and implementation shall be clearly separated and protected. Thus you can change the internal working of the class without worrying about how it will affect the client programmer.
  • public means the following element is available to everyone.
  • The private keyword, on the other hand, means that no one can access that element except you, the creator of the type, inside methods of that type. private is a brick wall between you and the client programmer. Someone who tries to access a private member will get a compile-time error.
  • The protected keyword acts like private, with the exception that an inheriting class has access to protected members, but not private members.
  • Reusing the implementation: We call this “creating a member object.” Your new class can be made up of any number and type of other objects, in any combination that you need to achieve the functionality desired in your new class. Because you are composing a new class from existing classes, 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, as in “a car has an engine.”, with the same lifecycle
  • Aggregation is often referred to as a "whole-part" relationship, with the different lifecycle

This UML diagram indicates composition with the filled diamond, which states there is one car.

  • Use inheritance as lessly as possible.Instead, you should first look to composition when creating new classes, since it is simpler and more flexible. If you take this approach, your designs will be cleaner.
  • You have two ways to differentiate your new derived class from the original base class. The first is quite straightforward: You simply add brand new methods to the derived class. The second and more important way to differentiate your new class is to change the behavior of an existing base-class method. This is referred to as overriding that method.
  • When dealing with type hierarchies, you often want to treat an object not as the specific type that it is, but instead as its base type. This allows you to write code that doesn’t depend on specific types.
  • The function call generated by a non-OOP compiler causes what is called early binding, static binding, compile-time binding.
  • In some languages you must explicitly state that you want a method to have the flexibility of late-binding properties (C++ uses the virtual keyword to do this). In these languages, by default, methods are not dynamically bound. In Java, dynamic binding is the default behavior and you don’t need to remember to add any extra keywords in order to get polymorphism.
  • In Java, Overloadding with same method name and different argument, Polymorphism with same method name and same argument.
  • In C++, if one method defined in based class is redefined in derived class with same name and different argument, it will hide all methods with same name in based class when visiting this method by a object of derived class.
  • We call this process of treating a derived type as though it were its base type upcasting. The name cast is used in the sense of casting into a mold and the up comes from the way the inheritance diagram is typically arranged, with the base type at the top and the derived classes fanning out downward.
  • That is, you don’t want anyone to actually create an object of the base class, only to upcast to it so that its interface can be used. This is accomplished by making that class abstract by using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it. This is a tool to enforce a particular design.
  • The interface keyword takes the concept of an abstract class one step further by preventing any method definitions at all. The interface is a very handy and commonly used tool, as it provides the perfect separation of interface and implementation.
  • There are different philosophies at work here. C++ takes the approach that control of efficiency is the most important issue, so it gives the programmer a choice. For maximum run-time speed, the storage and lifetime can be determined while the program is being written, by placing the objects on the stack (these are sometimes called automatic or scoped variables) or in the static storage area. This places a priority on the speed of storage allocation and release, and control of these can be very valuable in some situations.
  • The second approach is to create objects dynamically in a pool of memory called the heap.
  • Object-oriented programming is often summarized as simply “sending messages to objects.”

Java Knowledge series 1的更多相关文章

  1. Java Knowledge series 4

    JVM & Bytecode Has-a or Is-a relationship(inheritance or composition) 如果想利用新类内部一个现有类的特性,而不想使用它的接 ...

  2. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  3. Java Knowledge series 5

    Interface from user, not from implementor.(DIP) Interface-Oriented Programming. Interface or Abstrac ...

  4. Java Knowledge series 3

    JVM & Bytecode Abstract & Object Object in Java (1) 所有东西都是对象object.可将对象想象成一种新型变量:它保存着数据,但可要求 ...

  5. Java Knowledge series 2

    JVM Analysis & Design The object-oriented paradigm is a new and different way of thingking about ...

  6. Java SE series:2. enhance your java basis! [doc chm: jdk6api Chinese reference]

    1. javaee(Web) and Android 2. how to use eclipse and break point debuging in eclipse, as to java web ...

  7. Java SE series:1. environment configure and Hello world! [We use compiler and packager to create an application!]

    1. cli (command line interface) and gui (graphic user interface) use javahome path, search classpath ...

  8. C++ Knowledge series 1

    Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...

  9. C++ Knowledge series STL & Const

    Thank to the pepole who devote theirself to the common libs. STL(http://www.cplusplus.com/reference/ ...

随机推荐

  1. IOS 浏览器上设置overflow: auto 不可滚动

    项目中最近遇到一个bug,在ios上出现的问题:原页面是在某一块地方滚动,但是改版后,滚动区域改为最外层元素,最外层包裹了一层class为main的div .main { position: fixe ...

  2. composer.json 配置设置

    配置文件的值为 key:val 必须双引号包裹 一.配置文件 名字 name包名称由用户名名称和仓库名称组成包版本限制来请求Monolog软件包 1.0.*.这意味着1.0开发分支中的任何版本,或大于 ...

  3. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  4. Jenkins自动化CI CD流水线之2--用户权限管理

    一. 背景 针对开发.运维.测试针对不同角色进行不同权限划分, 基于插件: Role-based Authorization Strategy来实现. 一. 安装 安装该插件: 系统管理->管理 ...

  5. eclipse中找不到base64包的解决方法

    eclipse中找不到base64包的解决方法 2017年08月26日 11:05:26 yzp_leo 阅读数:634 标签: javaeclipsebase64更多 个人分类: 日记   ecli ...

  6. 小众软件:录屏局部放大神器 ZoomIt

    名称 ZoomIt 功能 屏幕放大录制工具 说明 此款软件解决了以下几点诉求: 我们在录制软件使用教学的时候,有些操作位置细节的放大需要(局部放大) 我们在给别人讲解PPT的时候,需要标注文字,或者画 ...

  7. JavaWeb xss攻击

    出处: http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross Site Scripting) 跨站脚 ...

  8. JUnit 判断方法抛出的异常

    :比方案1更详细,可以进一步判断抛出的异常的报错信息是否符合预期 不用上面那个属性,用 try - catch(因为判断了报错信息,所以不用判断异常的类型了吧) ( 注释:MyAssert类是我自定义 ...

  9. Unity 将一个类序列化并以 ".asset" 类型存储在 Resources 文件夹下

    概念: 序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通过从存储区中读取或反序列化对象的状态 ...

  10. PlayMaker Int的两个数进行比较 Int Compare

    Integer 1 和 Integer 2 进行比较,Integer 1 和  Integer 2 相等的时候 执行 ChangeToGreen; Integer 1 比  Integer 2 大的时 ...