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,

Slides:



Advertisements
Similar presentations
Event handling and listeners What is an event? user actions and context event sources and listeners Why should my programs be event- driven? User interaction.
Advertisements

pa 1 Porting BETA to ROTOR ROTOR Projects Presentation Day, June by Peter Andersen.
Mouse Listeners We continue our examination of GUIs by looking at how to interact with the mouse –Just as Java creates Events when the user interacts with.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces.
1 Inheritance. 2 One class inherits from another if it describes a specialized subset of objects Terminology: inheritschild class subclass –the class.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Event Handling Events and Listeners Timers and Animation.
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,
1 Java Object oriented features Object oriented features Polymorphic type Polymorphic type Inheritance Inheritance Information Hiding Information Hiding.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
L EC. 01: J AVA FUNDAMENTALS Fall Java Programming.
Address Book in JAVA By What is Address Book Address Book is book or database used for storing entries called contacts Each contact.
OOP Languages: Java vs C++
GUI Programming in Java
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
ACM/JETT Workshop - August 4-5, ExceptionHandling and User Interfaces (Event Delegation, Inner classes) using Swing.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
JavaBeans Components. To understand JavaBeans…  Proficient experience with the Java language required  Knowledge of classes and interfaces  Object-Oriented.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
University of Houston-Clear Lake Proprietary© 1997 Evolution of Programming Languages Basic cycle of improvement –Experience software difficulties –Theory.
(c) University of Washington07b-1 CSC 143 Java Events, Event Handlers, and Threads Reading: Ch. 17.
Introduction to Object Oriented Programming CMSC 331.
CSE 501N Fall ‘09 20: Event Handling and Inner Classes 17 November 2009 Nick Leidenfrost.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Graphics and Event-Driven Programming in Java John C. Ramirez Department of Computer Science University of Pittsburgh.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
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.
We will talking about story of JAVA language. By Kristsada Songpartom.
Introduction to Java Beans CIS 421 Web-based Java Programming.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
UID – Event Handling and Listeners Boriana Koleva
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
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.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CSCE 314 Programming Languages Reflection Dr. Hyunyoung Lee 1.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Event Handling and Listeners in SWING The practice of event handling.
JAVA TRAINING IN NOIDA. JAVA Java is a general-purpose computer programming language that is concurrent, class-based, object-oriented and specifically.
Sung-Dong Kim, Dept. of Computer Engineering, Hansung University Java - Introduction.
Chapter 32 JavaBeans and Bean Events
Inner Classes.
Topic: Inner Classes Course : JAVA PROGRAMMING Paper Code: ETCS-307 Faculty : Dr. Prabhjot Kaur Reader, Dept. of IT 1.
Chapter 36 JavaBeans and Bean Events
Java Beans Sagun Dhakhwa.
Inner Classes 11/14/ Dec-04 inner_classes.ppt.
GUI Programming using Java - Mouse Events
Inner Classes 29-Nov-18.
Events, Event Handlers, and Threads
Inner Classes 17-Apr-19.
Inner Classes 21-Apr-19.
Inner Classes 1-May-19.
Inner Classes 11-May-19.
Inner Classes 18-May-19.
Inner Classes.
Inner Classes 25-Oct-19.
Presentation transcript:

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, APL) –Java is garbage-collected (like LISP, Eiffel, Modula-3) –Java can be compiled (like LISP) –Java is object-oriented (like C++, Ada, Eiffel) A successful hybrid A reasonable general-purpose language for non-real- time applications Work in progress: language evolving rapidly

Original design goals (white paper 1993) Simple Object-oriented (inheritance, polymorphism) Distributed Interpreted multithreaded Robust Secure Architecture-neutral a language with threads, objects, exceptions and garbage-collection can’t really be simple!

Portability Critical concern: write once-run everywhere Consequences: –Portable interpreter (hardware instruction set independent) –definition through virtual machine: the JVM –run-time representation has high-level semantics –supports dynamic loading –(+) 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

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

A new construct: interfaces Allow otherwise unrelated classes to satisfy a given requirement Describes capabilities –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) –better model than multiple inheritance More costly at run-time (minor consideration)

Interface Properties Within an interface, the methods must have a signature but no body. Cannot declare an object of an interface. Classes can “implement” an interface. They must define every one of the methods in the interface.

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.

Create a thread of execution by creating an object of class extending Thread class Doodler extends Thread { // must override the run method of a thread public void run( ) {... // scribble something } } … Doodler gary = new Doodler ( ); gary.start( ); // calls the run method

Create a thread of execution by creating an object o1 that implements run and constructing a thread with o1 as actual class Simple_Gizmo { …} class Active_Gizmo extends Simple_Gizmo implements Runnable { public void run ( ) {…} } // a thread can be constructed from anything that implements run: Thread thing1 = new Thread (new Active_Gizmo ( )); Thread thing2 = new Thread (new Active_Gizmo ( )); thing1.start( ); thing2.start ( );

How this is done Thread() – allocates a new Thread object (used in the extends case). Thread(Runnable target) – allocates a new thread object (used when implementing Runnable). Which to use? If you are using inheritance for other (non-threading) purposes, then you must choose the implements option.

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

Built-in interfaces for event handlers public interface MouseListener { void mousePressed (MouseEvent event); void mouseReleased (MouseEvent event); void mouseClicked (Mouseevent event); void mouseEntered (Mouseevent event); void mouseExited (MouseEvent event); } Typically, handler only needs to process a few of the above, and supply dummy methods for the others

Adapters: a coding convention class mouseAdapter implements mouseListener { public void mousePressed (MouseEvent event) { } ; public void mouseREleased (MouseEvent event) { }; public void mouseClicked (Mouseevent event) { }; public void mouseEntered (Mouseevent event) { }; public void mouseExited (MouseEvent event) { }; }; class MouseClickListener extends Mouseadapter { public void mouseClicked (MouseEvent event {…}; // only the method of interest needs to be supplied }

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

Event handlers and nested classes Inner classes make it possible to add local handlers to any component class reactive_panel extends Jpanel { // a swing component JButton b1; Public reactive_panel (Container c) { b1 = new JButton (“flash”); add (b1); MyListener ml = new Mylistener ( ) ; b1.addActionListener (ml); private class MyListener implements ActionListener { public void actionPerformed (ActionEvent e) { …} }

Introspection, reflection, and typeless programming public void DoSomething (Object thing) { // what can be do with a generic object? if (thing instanceof gizmo) { // we know the methods in class Gizmo …. 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. Common in Smalltalk and LISP, but Java offers.

Reflection and metaprogramming Given an object at run-time, it is possible to obtain: –its class –its fields (data members) as strings –the classes of its fields –the methods of its class, as strings –the types of the methods It is then possible to construct calls to these methods This is possible because the JVM provides a high- level representation of a class, with embedded strings that allow almost complete disassembly.

Typical Applications of Reflection Object serialization: when you want to send an object across a network or to a file, the system must break the object into a byte stream. Then build up on the other end. Reflection enables a piece of code to look at the class of an object to determine its structure. Reflection does not allow one piece of code to determine the private data members of an object however.

Some ways to do introspection java.lang.Class –Class.getMethods () returns array of method objects –Class.getConstructor (Class[ ] parameterTypes) returns the constructor with those parameters java.lang.reflect.Array –Array.NewInstance (Class componentType, int length) java.lang.reflect.Field java.lang.reflect.Method All of the above require the existence of run-time objects that describe methods and classes

Another use of reflection: Java Beans Beans goal: reusable platform-independent components. A component is called a “bean”. Beans can be composed using a builder tool. The introspector examines beans for design patterns to discover features. This relies on reflection. Using reflection, the Introspector assembles information about a class, e.g. methods, ancestors, and properties that can change at design time.

JavaBeans example: Create a bean just as in a general class. Must implement serializable (which is the interface to flatten an object to a byte stream). Call it a bean in a manifest file. You can set properties (such as object size) using a property editor, because introspection allows the editor to ask for an object’s properties. No additional programming!

Java is rich in libraries The power of the language is in the large set of libraries in existence. The language is successful if programmers find libraries confortable: JFC and the Swing package Pluggable look and Feel Graphics Files and Streams Networking Enterprise libraries: CORBA, RMI, Serialization, JDBC