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

Slides:



Advertisements
Similar presentations
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Advertisements

Portability and Safety Mahdi Milani Fard Dec, 2006 Java.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Lab Information Security Using Java (Review) Lab#0 Omaima Al-Matrafi.
Lab#1 (14/3/1431h) Introduction To java programming cs425
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Reasons to study concepts of PL
How is Java different from other languages Less than you think: –Java is an imperative language (like C++, Ada, C, Pascal) –Java is interpreted (like LISP,
Programming Languages Structure
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
Abstract Data Types and Encapsulation Concepts
How is Java different from other languages Less than you think: –Java is an imperative language (like C++, Ada, C, Pascal) –Java is interpreted (like LISP,
By Logan Phipps Hal student.  This power point explains some common programming languages enjoy  When done click on the home button to return to home.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Java Software Solutions Lewis and Loftus Chapter 2 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Software Concepts -- Introduction.
OOP Languages: Java vs C++
Overview. Copyright © 2006 The McGraw-Hill Companies, Inc. Chapter 1 Overview A good programming language is a conceptual universe for thinking about.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Programming Languages and Paradigms Object-Oriented Programming.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java Language and SW Dev’t
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
Sadegh Aliakbary Sharif University of Technology Fall 2012.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 Computer Systems -- Introduction  Chapter 1 focuses on:  the structure of a Java application  basic program elements  preparing and executing a program.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
1 Introduction Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages.
Java Security Nathan Moore CS 665. Overview Survey of Java Inherent Security Properties Java Runtime Environment Java Virtual Machine Java Security Model.
Java 2 security model Valentina Casola. Components of Java the development environment –development lifecycle –Java language features –class files and.
Introduction to Object Oriented Programming CMSC 331.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
What Is Java? According to Sun in a white paper: Java: A simple, object-oriented, network-savvy, interpreted, robust, secure, architecture-neutral, portable,
Chapter 2: A Brief History Object- Oriented Programming Presentation slides for Object-Oriented Programming by Yahya Garout KFUPM Information & Computer.
Computer Programming 2 Why do we study Java….. Java is Simple It has none of the following: operator overloading, header files, pre- processor, pointer.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
How is Java different from other languages Less than you think: ◦ Java is an imperative language (like C++, Ada, C, Pascal) ◦ Java is interpreted (like.
JAVA INTRODUCTION. What is Java? 1. Java is a Pure Object – Oriented language 2. Java is developing by existing languages like C and C++. How Java Differs.
What is Java? Object Oriented Programming Language Sun Microsystems “Write Once, Run Everywhere” Bytecode and Virtual Machine Java Platform (Java VM and.
Introduction Mehdi Einali Advanced Programming in Java 1.
1 Chapter 11 © 1998 by Addison Wesley Longman, Inc The Concept of Abstraction - The concept of abstraction is fundamental in programming - Nearly.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Introduction to Programming 1 1 2Introduction to Java.
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
SESSION 1 Introduction in Java. Objectives Introduce classes and objects Starting with Java Introduce JDK Writing a simple Java program Using comments.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Fundamental of Java Programming (630002) Unit – 1 Introduction to Java.
Abstract Data Types and Encapsulation Concepts
Representation, Syntax, Paradigms, Types
11.1 The Concept of Abstraction
Programming Language Concepts (CIS 635)
Advanced Programming Fall 2017.
Representation, Syntax, Paradigms, Types
Units with – James tedder
Representation, Syntax, Paradigms, Types
Representation, Syntax, Paradigms, Types
Java Evolution Java History. Java Features.
11.1 The Concept of Abstraction
Chapter 11 Abstraction - The concept of abstraction is fundamental in
Presentation transcript:

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 The Java Programming Language

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 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 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 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 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 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 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 Guy Steele Guy Steele **/ **/ Keywords – return – return – – – describes an exception that method – 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 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 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 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 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 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 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 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 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 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 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 Declarative Programming

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 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 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 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 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