158x Filetype PDF File size 0.53 MB Source: www.cmi.ac.in
Programming Language Concepts: Lecture Notes Madhavan Mukund Chennai Mathematical Institute 92 G N Chetty Road Chennai 600 017 http://www.cmi.ac.in/˜madhavan 2 Contents I Object-oriented programming 7 1 Data encapsulation with classes 9 1.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 1.2 Data encapsulation (public and private) . . . . . . . . . . . . . . . . . . . . 12 1.3 Static components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.4 Constants (the attribute final) . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.5 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 2 Java Interlude 1 Quick introduction to Java 21 2.1 Structure of a Java program . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.2 Compiling and running Java programs . . . . . . . . . . . . . . . . . . . . . 22 2.3 Basic Java syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.3.1 Scalar variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.3.2 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3.3 Compound statements . . . . . . . . . . . . . . . . . . . . . . . . . . 24 2.3.4 Final note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3 Subclasses and inheritance 29 3.1 Subclasses and superclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 3.2 Inheritance polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 3.3 Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 3.4 The Java class hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.5 Subtyping vs inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 4 Abstract classes 37 4.1 Abstract Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 4.2 Generic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.3 Multiple inheritance and interfaces . . . . . . . . . . . . . . . . . . . . . . . 40 5 Interfaces: Applications 43 5.1 Callback functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 5.2 Iterators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 3 5.3 Ports and interaction with “state” . . . . . . . . . . . . . . . . . . . . . . . . 51 6 Java Interlude 2 Parameter passing, cloning, packages 53 6.1 Parameter passing in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 6.2 Cloning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 6.3 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 6.4 The protected modifier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 II Exception handling 59 7 Exception handing 61 7.1 Java’s try-catch blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 7.2 Cleaning up with finally . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 7.3 Customized exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 7.4 A“cute” fact about finally . . . . . . . . . . . . . . . . . . . . . . . . . . 65 III Event driven programming 67 8 Event driven programming 69 8.1 Programming graphical user interfaces . . . . . . . . . . . . . . . . . . . . . 69 8.2 Some examples of event driven programming in Java . . . . . . . . . . . . . 71 8.2.1 Abutton. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 8.2.2 Three buttons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 8.3 The event queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 8.4 Custom events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 IV Reflection 81 9 Reflection 83 V Concurrent programming 89 10 Concurrent Programming 91 10.1 Race conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 10.2 Protocols based on shared variables . . . . . . . . . . . . . . . . . . . . . . . 94 10.3 Programming primitives for mutual exclusion . . . . . . . . . . . . . . . . . 96 10.4 Monitors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 10.5 Monitors in Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 10.6 Java threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 4
no reviews yet
Please Login to review.