Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Java Object oriented features Object oriented features Polymorphic type Polymorphic type Inheritance Inheritance Information Hiding Information Hiding.

Similar presentations


Presentation on theme: "1 Java Object oriented features Object oriented features Polymorphic type Polymorphic type Inheritance Inheritance Information Hiding Information Hiding."— Presentation transcript:

1 1 Java Object oriented features Object oriented features Polymorphic type Polymorphic type Inheritance Inheritance Information Hiding Information Hiding Modules Modules Interface vs. Implementation Interface vs. Implementation Separate Compilation Separate Compilation Security Security Software Engineering Software Engineering Maintenance – No global variables, type checks Maintenance – No global variables, type checks High level functionality – Well defined Framework High level functionality – Well defined Framework Documentation – Automatic Documentation – Automatic Organization/Modules -- Organization/Modules -- Interfaces, contracts, and implementation Interfaces, contracts, and implementation

2 2 The Java Programming Language

3 3 Why Java? Originally developed at Sun for embedded consumer devices. Originally developed at Sun for embedded consumer devices. Cable TV set-top boxes, etc. Cable TV set-top boxes, etc. Developed by James Gosling Developed by James Gosling Code distributed to devices via a wide area network. Code distributed to devices via a wide area network. Many different kinds of devices Many different kinds of devices Security Security Reliability Reliability Popularity of WWW in early 1990s Popularity of WWW in early 1990s Sun retargets Oak as a general programming language Sun retargets Oak as a general programming language Renamed to Java Renamed to Java Applets for the Web pages Applets for the Web pages

4 4 How is Java different from other languages Less than you think: Less than you think: Java is an imperative language (like C++, Ada, C, Pascal) Java is an imperative language (like C++, Ada, C, Pascal) Java is interpreted byte code (like LISP, APL) Java is interpreted byte code (like LISP, APL) Java is garbage-collected (like LISP, Eiffel, Modula-3) Java is garbage-collected (like LISP, Eiffel, Modula-3) Java can be compiled (like LISP) Java can be compiled (like LISP) Java is object-oriented (like C++, Ada, Eiffel) Java is object-oriented (like C++, Ada, Eiffel) But introduces important new features But introduces important new features Java includes documentation standards in language Java includes documentation standards in language Java includes security features Java includes security features Java includes reflection to reveal meta-information Java includes reflection to reveal meta-information A successful hybrid for a specific-application domain A successful hybrid for a specific-application domain A reasonable general-purpose language for non-real- time applications A reasonable general-purpose language for non-real- time applications

5 5 Original design goals (white paper 1993) Simple (easy to learn and use) Simple (easy to learn and use) Object-oriented (inheritance, polymorphism) Object-oriented (inheritance, polymorphism) Distributed (intended for networks) Distributed (intended for networks) Interpreted(by Java Virtual Machine, JVM) Interpreted(by Java Virtual Machine, JVM) Multithreaded(built-in synchronization primitive) Multithreaded(built-in synchronization primitive) Robust(strongly typed) Robust(strongly typed) Secure(run-time security checks) Secure(run-time security checks) Architecture-neutral (JVM for many platforms) Architecture-neutral (JVM for many platforms) A language with threads, objects, exceptions and garbage-collection can’t really be simple!

6 6 Portability Critical concern: write once-run everywhere Critical concern: write once-run everywhere Intended for distribution over wide area network Intended for distribution over wide area network Consequences: Consequences: Portable interpreter Portable interpreter definition through virtual machine: the JVM definition through virtual machine: the JVM run-time representation has high-level semantics run-time representation has high-level semantics supports dynamic loading supports dynamic loading (+) high-level representation can be queried at run-time to provide reflection (+) high-level representation can be queried at run-time to provide reflection (-) Dynamic features make it hard to fully compile, safety requires numerous run-time checks (-) Dynamic features make it hard to fully compile, safety requires numerous run-time checks

7 7 Contrast with conventional systems languages (C, C++, Ada) Most Conventional languages are fully compiled: Most Conventional languages are fully compiled: run-time structure is machine language (not portable) run-time structure is machine language (not portable) minimal or no run-time type information minimal or no run-time type information language provides low-level tools for accessing storage language provides low-level tools for accessing storage safety requires fewer run-time checks because compiler safety requires fewer run-time checks because compiler (least for Ada and somewhat for C++) can verify correctness statically. Languages require static binding, run-time image cannot be easily modified Languages require static binding, run-time image cannot be easily modified Different compilers may create portability problems Different compilers may create portability problems

8 8 Language Changes Improvements since Java was introduced in 1995 Improvements since Java was introduced in 1995 Event Model Event Model Nested Classes Nested Classes Parameterized classes (C++ templates, Ada generics) in Java 1.5 (Beta) Parameterized classes (C++ templates, Ada generics) in Java 1.5 (Beta) Many Java Technologies (JavaBeans, Telephony, …) Many Java Technologies (JavaBeans, Telephony, …) (Distinction between libraries and language is diminishing) (Distinction between libraries and language is diminishing) But Java Still has omissions: But Java Still has omissions: No operator overloading (syntactic annoyance) No operator overloading (syntactic annoyance) No enumeration types (using final constants is clumsy) No enumeration types (using final constants is clumsy)

9 9 JavaDoc Automatically generates user documentation Automatically generates user documentation Special comments containing keywords Special comments containing keywords /**  this introduces a special comment /**  this introduces a special comment ** @author Guy Steele ** @author Guy Steele **/ **/ Keywords Keywords @return – return value @return – return value @param – parameter @param – parameter @throws – describes an exception that method throws @throws – describes an exception that method throws Comment associated with class or method immediately following the comment Comment associated with class or method immediately following the comment Can include HTML tags Can include HTML tags Makes it easier to document code Makes it easier to document code Helps keep documentation current Helps keep documentation current Extensible: record rationale, maintenance instructions, etc. Extensible: record rationale, maintenance instructions, etc.

10 10 Interfaces Separate specification from implementation Separate specification from implementation Allow related classes to satisfy a given requirement Allow related classes to satisfy a given requirement Orthogonal to inheritance Orthogonal to inheritance inheritance: an A is-a B (has the attributes of a B, and possibly others) inheritance: an A is-a B (has the attributes of a B, and possibly others) interface: an A can-do X (and other unrelated actions) interface: an A can-do X (and other unrelated actions) better model for multiple inheritance better model for multiple inheritance More costly at run-time (minor consideration) More costly at run-time (minor consideration)

11 11 Interface Comparable public interface Comparable { public int CompareTo (Object x) throws ClassCastException; // returns -1 if this < x, // 0 if this = x, // 1 if this > x }; Implementation has to cast x to the proper class. Implementation has to cast x to the proper class. Any class that may appear in a container should implement Comparable Any class that may appear in a container should implement Comparable

12 12 Interfaces and event-driven programming A high-level model of event-handling: A high-level model of event-handling: graphic objects generate events graphic objects generate events mouse click, menu selection, window close... mouse click, menu selection, window close... an object can be designated as a handler an object can be designated as a handler a listener, in Java terminology a listener, in Java terminology an event can be broadcast to several handlers an event can be broadcast to several handlers several listeners can be attached to a source of events several listeners can be attached to a source of events a handler must implement an interface a handler must implement an interface actionPerformed, keyPressed, mouseExited.. actionPerformed, keyPressed, mouseExited..

13 13 Events and listeners class Calm_Down extends Jframe { private Jbutton help := new Jbutton (“HELP!!!”); private Jbutton help := new Jbutton (“HELP!!!”); // indicate that the current frame handles button clicks // indicate that the current frame handles button clicks help.addActionListener (this); help.addActionListener (this); // if the button is clicked the frame executes the following: // if the button is clicked the frame executes the following: public void actionPerformed (ActionEvent e) { public void actionPerformed (ActionEvent e) { if (e.getSource () == help) { if (e.getSource () == help) { System.out.println(“can’t be that bad. What’s the problem?”); System.out.println(“can’t be that bad. What’s the problem?”); } } }

14 14 Introspection, Reflection, and Typeless programming public void DoSomething (Object thing) { public void DoSomething (Object thing) { // what can be do with a generic object? // what can be do with a generic object? if (thing instanceof gizmo) { if (thing instanceof gizmo) { // we know the methods in class Gizmo // we know the methods in class Gizmo …. …. Instanceof requires an accessible run-time descriptor in the object. Instanceof requires an accessible run-time descriptor in the object. Reflection is a general programming model that relies on run-time representations of aspects of the computation that are usually not available to the programmer. Reflection is a general programming model that relies on run-time representations of aspects of the computation that are usually not available to the programmer. More common in Smalltalk and LISP. More common in Smalltalk and LISP.

15 15 Separate Compilation Compilers need to know type of objects defined elsewhere at compile time Compilers need to know type of objects defined elsewhere at compile time Ada separates spec and body Ada separates spec and body C and C++ use header files C and C++ use header files Java constructs class files which contain: Java constructs class files which contain: Method descriptions, including method signatures and Byte code Method descriptions, including method signatures and Byte code A constant pool with names and constant values A constant pool with names and constant values Reflection information Reflection information Package structure related to Package structure related to Network implementation Network implementation File system File system

16 16 Reflection and Metaprogramming Given an object at run-time, it is possible to obtain: Given an object at run-time, it is possible to obtain: its class its class its field names (data members) as strings its field names (data members) as strings the classes (types) of its fields the classes (types) of its fields the method names of its class, as strings the method names of its class, as strings the types of the methods the types of the methods It is then possible to construct calls to these methods It is then possible to construct calls to these methods This is possible because the JVM class file provides a high-level representation of a class, with embedded strings that allow almost complete disassembly. This is possible because the JVM class file provides a high-level representation of a class, with embedded strings that allow almost complete disassembly. Protected by Java’s security system Protected by Java’s security system

17 17 Reflection classes Java.lang.Class Java.lang.Class Class.forName(className) // creates a Class object Class.getMethods () // returns array of method objects Class.getConstructor (Class[] parameterTypes) //returns the constructor with those parameters Class.isInterface() // true if the “class” is an interface Reflection Classes Reflection Classes Allows access to objects at run-time Allows access to objects at run-time Java Beans Java Beans High level components High level components Communicate with events Communicate with events

18 18 Reflection and beans The beans technology requires run-time examination of foreign objects, in order to build dynamically a usable interface for them. The beans technology requires run-time examination of foreign objects, in order to build dynamically a usable interface for them. Class Introspector builds a method dictionary based on simple naming conventions: Class Introspector builds a method dictionary based on simple naming conventions: public boolean isCoffeeBean ( ); // is... predicate public boolean isCoffeeBean ( ); // is... predicate public int getRoast ( ); // get... retrieval public int getRoast ( ); // get... retrieval public void setRoast (int darkness) ; // set… assignment public void setRoast (int darkness) ; // set… assignment getBeanInfo(Class.fromName(“CoffeeBean”)); getBeanInfo(Class.fromName(“CoffeeBean”)); Returns a BeanInfo object Returns a BeanInfo object

19 19 Security Essential for distributing code over the internet Essential for distributing code over the internet Untrusted code ran in “Sandbox” on original Java Untrusted code ran in “Sandbox” on original Java Now Java has fine-grained security system Now Java has fine-grained security system Restricts access to system resources Restricts access to system resources reading or writing files, system exit, etc. reading or writing files, system exit, etc. Based on a Policy that grants permissions Based on a Policy that grants permissions Code base (e.g. a URL) Code base (e.g. a URL) Signer (names a set of public keys) Signer (names a set of public keys) Permissions (e.g. reading files) Permissions (e.g. reading files) ClassLoader controls which classes have which permissions ClassLoader controls which classes have which permissions

20 20 Declarative Programming

21 21 Prolog Logic Programming Logic Programming Theorem Proving Theorem Proving Program is a series of statements (axioms) and a goal Program is a series of statements (axioms) and a goal Execution attempts to prove goal Execution attempts to prove goal Reach goal from axioms Reach goal from axioms Uses inference Uses inference

22 22 Horn Clauses A Horn clause is of the form A Horn clause is of the form C  B 1, B 2, B 3, …, B n-1, B n C  B 1, B 2, B 3, …, B n-1, B n C is the Head or Consequent C is the Head or Consequent B i is an term of the Body B i is an term of the Body When B i is true for all i = 0..n. then C is true When B i is true for all i = 0..n. then C is true If C  A, B and D  C then D  A, B If C  A, B and D  C then D  A, B Terms are usually parameterized predicates Terms are usually parameterized predicates Rainy(Seattle) Rainy(Seattle) True iff it rains in Seattle True iff it rains in Seattle If flowers(X)  rainy(X) and rainy(NY) then flowers(NY) If flowers(X)  rainy(X) and rainy(NY) then flowers(NY) Sta

23 23 Unification Process of substituting expressions for variables Process of substituting expressions for variables A constant unifies only with itself A constant unifies only with itself Two structures unify iff predicate is the same, the number of parameters is the same and corresponding parameters unify (recursively) Two structures unify iff predicate is the same, the number of parameters is the same and corresponding parameters unify (recursively) Variables unify with: Variables unify with: Values – variable instantiated with value Values – variable instantiated with value Other Variables – names alias each other Other Variables – names alias each other Unification also used in ML type inference Unification also used in ML type inference

24 24 A Prolog Program A Program is a sequence of statements A Program is a sequence of statements rainy(seattle) rainy(seattle) rainy(rochester) rainy(rochester) cold(rochester) cold(rochester) snowy(x) :- rainy(X), cold(X) snowy(x) :- rainy(X), cold(X) A Query is applied to the program A Query is applied to the program ?- snowy(C) ?- snowy(C) C = rochester C = rochester Interpreter may return only the first or all values satisfying the query Interpreter may return only the first or all values satisfying the query

25 25 Applications Limitations Limitations Unification can take exponential time Unification can take exponential time Subset of first order logic Subset of first order logic not is inability to prove, not false not is inability to prove, not false Can be used for Can be used for Problems for which efficient and straightforward procedural solutions are not available Problems for which efficient and straightforward procedural solutions are not available Natural Language Natural Language AI and Expert Systems AI and Expert Systems Relationships (student of, ancestor of) Relationships (student of, ancestor of) Relational Database-like applications Relational Database-like applications


Download ppt "1 Java Object oriented features Object oriented features Polymorphic type Polymorphic type Inheritance Inheritance Information Hiding Information Hiding."

Similar presentations


Ads by Google