1 / 22

MT311 Java Application Development and Programming Languages

MT311 Java Application Development and Programming Languages. Li Tak Sing( 李德成 ). Implementing subprograms.

Download Presentation

MT311 Java Application Development and Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MT311 Java Application Development and Programming Languages Li Tak Sing(李德成)

  2. Implementing subprograms • When a subprogram is called, an activation record instance (ARI) is created in the stack. It isdestroyed when the subprogram returns. An ARI has a number of fields. Dynamiclink and static link are two of them. Both of them are pointers pointingto the ARI of other subprograms. The meaning of the names reflects thatthe former needs information in execution time and the latter onlyneeds information in compilation time.

  3. Implementing subprograms • In fact, the dynamic link of asubprogram always points to the ARI of the calling subprogram. As asubprogram can be called by different subprograms, to which ARI adynamic link will point is not known at compile time. That is the reasonwhy it is called dynamic. On the other hand, the static link of the ARI ofa subprogram always points to the ARI of the static parent.

  4. Implementing subprograms • Thisinformation is known at compile time and therefore is called static.

  5. ARI • Other fields in ARI are parameters and local variables. • So, consider the following subprogram:procedure proc1(int i) var j,k:integer; begin ..... end;

  6. ARI • The ARI would have the following fields: • The storage for the parameter i (assume that it is passed by value) • The storage for local variables j and k. • The static link which points to the ARI of the static parent of the procedure. • The dynamic link which points to the ARI of the calling procedure. • return address

  7. ARI

  8. ARI • The ARI of the main program contains the following: • global variables

  9. 1. program A; 2. var a1: integer; 3. procedure B; 4. var b1: integer; 5. begin 6. ... 7 end; 8. procedure C; 9. var c1: integer; 10. procedure D; 11. var d1: integer; 12. begin 13. ... 14. end;

  10. 15. procedure E; 16. var e1: integer; 17. begin 18. C; 19. e1 := c1; 20. end; 21. begin 22. ... 23. end; 24. begin 25. ... 26. end;

  11. ARI • Draw the ARIs if the at this moment we have the following procedure call sequence: • A -> E ->C->D

  12. Object Oriented Programming • the three fundamental concepts underlyingobject-oriented programming: • data abstraction; • inheritance; and • polymorphism.

  13. Data abstraction and encapsulation • The encapsulation of an object is the representation of its mostimportant features. For example, the encapsulation of a car may be avehicle with four wheels. An abstract data type is the encapsulation ofan object which includes all the subprograms that are used tomanipulate it.

  14. Data abstraction and encapsulation • If all users of the object were restricted frommanipulating it using these subprograms only, then the users’ codewould be independent of the implementation of the object. This has twoadvantages: • The implementation of the abstract data type can be changedwithout affecting the users’ code. Thus these changes would notpropagate to other parts of the system. This of course has one

  15. condition, i.e., the interfaces of the subprograms should remainunchanged. • The implementation of the object is naturally separated from otherparts of the system. Therefore the object can be tested individually.This would increase the reliability of the system.

  16. Information hiding • In order to ensure that users of an abstract data type manipulate itthrough a set of subprograms only, we have to find a way to preventthem from accessing the data structure of the data type directly. This isdone by enforcing information hiding, i.e., removing the informationconcerning the internal attributes of the data type from any filesreleased to the users.

  17. Information hiding • In Java, this is done by access modifier.

  18. Inheritance and polymorphism • Inheritance allows the programmer to inherit or derive new classesfrom an existing one. The new class may have additional attributes andmethods. A new method would override an old one if the same nameand parameter list were used. Overriding an existing method in aderived class is an important mechanism in fine-tuning an existingcode.

  19. Inheritance and polymorphism • However, this mechanism would not be of much use withoutpolymorphism. Polymorphism is the ability to invoke the correctversion of an overridden method when the corresponding message ispassed to an object. Polymorphism is closely related to dynamicbinding.

  20. Dynamic binding • Dynamic binding means that the binding of a value to an item is to bedone when a program is run. In this case, when a method of an object isinvoked, the runtime environment will first check the true type of theobject and then invoke the method that is defined for this object type.

  21. Inheritance and polymorphism • Inheritance and polymorphism are important in making softwarereusable. Without them, existing code could hardly be reusable becausesituations change from project to project. With inheritance andpolymorphism, we can fine-tune an existing code by deriving newclasses and add new attributes, new methods and override existingmethods in the new classes.

  22. Inheritance and polymorphism • Polymorphism would ensure that theoverridden methods would be invoked correctly.

More Related