Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )"— 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 is destroyed when the subprogram returns. An ARI has a number of fields. Dynamic link and static link are two of them. Both of them are pointers pointing to the ARI of other subprograms. The meaning of the names reflects that the former needs information in execution time and the latter only needs information in compilation time.

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

4 Implementing subprograms This information 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 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 underlying object-oriented programming: – data abstraction; – inheritance; and – polymorphism.

13 Data abstraction and encapsulation The encapsulation of an object is the representation of its most important features. For example, the encapsulation of a car may be a vehicle with four wheels. An abstract data type is the encapsulation of an object which includes all the subprograms that are used to manipulate it.

14 Data abstraction and encapsulation If all users of the object were restricted from manipulating it using these subprograms only, then the users’ code would be independent of the implementation of the object. This has two advantages: – The implementation of the abstract data type can be changed without affecting the users’ code. Thus these changes would not propagate to other parts of the system. This of course has one

15 condition, i.e., the interfaces of the subprograms should remain unchanged. – The implementation of the object is naturally separated from other parts 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 it through a set of subprograms only, we have to find a way to prevent them from accessing the data structure of the data type directly. This is done by enforcing information hiding, i.e., removing the information concerning the internal attributes of the data type from any files released 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 classes from an existing one. The new class may have additional attributes and methods. A new method would override an old one if the same name and parameter list were used. Overriding an existing method in a derived class is an important mechanism in fine-tuning an existing code.

19 Inheritance and polymorphism However, this mechanism would not be of much use without polymorphism. Polymorphism is the ability to invoke the correct version of an overridden method when the corresponding message is passed to an object. Polymorphism is closely related to dynamic binding.

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

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

22 Inheritance and polymorphism Polymorphism would ensure that the overridden methods would be invoked correctly.


Download ppt "MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )"

Similar presentations


Ads by Google