CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 1

Download Report

Transcript CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES LECTURE 1

CS441
CURRENT TOPICS IN
PROGRAMMING LANGUAGES
LECTURE 1
GEORGE KOUTSOGIANNAKIS
George Koutsogiannakis / Summer 2011
1
Course Objective
• Practice building reliable GUI (Graphical
User Interface) using Java.
• Create Client Server architectures :
– Networking protocols.
– Design of Internet Client Server enterprises
(applications).
• Java will be used to build Client/Server architectures.
Other platforms follow similar approaches to Java.
2
Course Material
• THE PRESENTATIONS PROVIDE A LIST OF TOPICS
DISCUSSED IN CLASS ON THE DAY OF THE LECTURE.
– A SORT DISCUSSION IS PRESENTED IN THE POWER POINT
PRESENTATION.
• YOU MUST EITHER KEEP NOTES IN THE LIVE CLASS OR, IF
YOU ARE A REMOTE STUDENT, WATCH THE TAPE FOR THE
COMPLETE COVERAGE OF THE TOPICS.
• IN ADDITION YOU CAN DOWNLOAD THE POWER POINT
PRESENTATIONS FROM THE COURSE’ S WEB SITE:
http://www.cs.iit.edu/~cs441/schedule.html
3
Course Material
• THIS SUMMARY DOES NOT NECESSARILLY LIMIT THE
TOPICS DISCUSSED IN CLASS. TOPICS NOT LISTED HERE
CAN BE DISCUSSED (FOR INSTANCE SAMPLE COD
EWRITTEN ON THE WHITE BOARD). IT ONLY SERVES AS A
GUIDELINE!.
• PROGRAMMING EXAMPLES ARE GIVEN FOR SOME TOPICS
IN
http://www.cs.iit.edu/~cs441/handoutsnew.html
• SUPPLEMENT YOUR UNDERSTANDING OF THE MATERIAL
BY ALSO READING THE RECOMMENDED CHAPTERS FROM
THE ASSIGNED TEXTS, NOTED ON THE SCHEDULE PAGE OF
THE COURSE’S WEB SITE.
4
COURSE ADMINISTRATION
•
TOPICS DISCUSSED:
– COURSE WEB SITE:
http//www.cs.iit.edu/~cs441
– USAGE OF BLACKBOARD
– TOOLS NEEDED FOR THE COURSE:
http://www.cs.iit.edu/~cs441/tools.html
– OFFICE HOURS
– ASSIGNMENTS / PROJECT.
– COURSE SYLLABUS
• THIS IS AN INTERMEDIATE TO ADVANCE LEVEL COURSE IN JAVA. BASIC
KNOWLEDGE OF JAVA IS ASSUMED. MUST KNOW HOW TO COMPILE AND
INTERPRET. MUST KNOW HOW TO USE NUMEROUS FILES TOGETHER.
5
COURSE ADMINISTRATION
• REVIEW OF COURSE’ S WEB SITE:
–
–
–
–
–
–
COURSE GRADING
COURSE ASSIGNMENTS
DOWNLOADING OF ASSIGNMENTS
DEADLINES FOR ASSIGNMENT
TOOLS REQUIRED
OTHER
• METHOD OF ASSIGNMENTS SUBMISSION.
– BLACKBOARD’ S INDIVIDUAL ASSIGNMENT FOLDERS- DO NOT
SUBMIT TO DIGITAL DROP BOX!!!!
• ETHICS
6
JAVA FRAMEWORKS AND
ACCEPTABLE PLATFORMS
• DISCUSSION OF VARIOUS JAVA FRAMEWORKS AVAILABLE
AND THEIR FUNCTIONALITY:
– JDK1.6 STANDARD EDITION UPDATE 23 or latest update.
RECOMMEND THAT YOU DOWNLOAD AND INSTALL ON YOUR
COMPUTER. EARLIER VERSIONS ARE ACCEPTABLE.
– JAVA ENTERPRISE EDITION 6 (EE 6) . INCLUDES A JDK PLUS
OTHER APIs.
– WINDOWS ENVIRONMENT IS THE PREFERRED ENVIRONMENT
FOR ASSIGNMENTS AND PROJECTS. I WILL BE CHECKING
YOUR WORK USING WINDOWS.
• If another environment is used you can demo your assignment
to me on your laptop.
7
JAVA FRAMEWORKS AND
ACCEPTABLE PLATFORMS
– DURING THE FIRST PART OF THE COURSE EDIT PLUS IS THE
PREFERRED IDE FOR WRITNG PROGRAMS. DO NOT USE
NetBeans IN THE FIRST HAL F OF THE COURS EASSIGNMENTS
UNLESS THE ASSIGNMENT SPECIFICALLY SAYS SO.
– YOU CAN USE OTHER JAVA FRIENDLY IDEs AS LONG AS THEY
DON’T USE THEIR OWN JDK.
• YOU MUST INSTALL A VERSION OF THE JDK AND LINK YOUR IDE TO THAT
INSTALLATION.
• DO NOT USE ECLIPSE AS IT USES ITS OWN JDK. IF YORU PROGRAM DOES
NOT WORK BECAUSE YOU USED ECLIPSE TIME WILL BE WAISTED
DEBUGGING IT (IT WILL BE RETURNE DTO YOU).
– PREFERRED DATABASES: MySQL / ACCESS/DERBY
– WEB SERVER: TOMCAT VERSION 6.0 AND UP
– NET BEANS 6.5 AND UP (LATEST VERSION IS 6.9) . NOTE THAT
EACH VERSION HA SITS OWN ISSUES AND PROBLEMS.
8
JAVA PROGRAMS
•
DIVISION OF JAVA PROGRAMS
– APPLICATIONS.
– APPLETS.
– SERVER SIDE JAVA- SERVLETS, JAVA SERVER PAGES, JAVA
SERVER FACES.
– ENTERPRISE JAVA BEANS
– WEB SERVICES
– DISTRIBUTED SERVICES USING RMI OR RMI OVER IIOP (OR
JAVA IDL).
•
SOME PROGRAMS REQUIRE SPECIAL TOOLS NOT AVAILABLE IN THE
STANDARD EDITION JDK (AVAILABLE IN ENTERPRISE EDITION)
– SERVLETS AND JAVA SERVER PAGES
– WEB SERVICES
– ENTERPRISE JAVA BEANS
9
APPLETS AND BROWSERS
• BROWSERS AND WEB SERVERS (SERVER / CLIENT )
• APPLETS AND THE BROWSER.
• APPLETS AND SECURITY.
– BROWSER ACTS AS A SANDBOX.
• WEB SERVER AND APPLETS.
• WEB SERVER AND SERVLETS.
• WEB SERVER AND JAVA SERVER PAGES.
10
Applets
SERVER SIDE
CLIENT SIDE
#1
#1
#1
Web Server
Internet
Browser
myApplet.html
myApplet.class
#2
#2
#2
Notice that the Java
code is executed by the
client (Browser)
• #1 Http request/response for html file//file is sent to Browser
• Browser interprets the html script
• # 2 Http request/response for file myApplet.class// file is sent to Browser
• Browser interprets java bytecodes
• File is destroyed when Browser is closed.
Servelts
Java Client Application
Servlet executes on
the server side
#1
#1
#1
Web Server
Internet
myJavaApplication.class
myServelt.class
#4
#3
#3
#2
• #1 Http request sent to Web Server for servelt file execution
• #2 Servelet executes on the Server side and returns response to Web Server
• # 3 Http response formed by the Web Server based on the response from Servlet
Http response is sent to Client Application
• # 4 Http response passes contents to input stream of the client application
(myJavaApplication.class)
12
Java Server Pages
Java Client Application
#1
#1
#1
Web Server
Internet
myJavaApplication.class
JSP contains java code and
Other formats such as html
, XML, special tags.
myJSP.class
#4
#3
#3
#2
• #1 Http request sent to Web Server for jsp file execution
• #2 java code of jsp executes on the Server side initially
• # 3 Http response formed by the Web Server based on the response from jsp. The java part of jsp
is converted to a Servlet
Http response is sent to Client Application
• # 4 Http response passes contents to input stream of the client application
13
(myJavaApplication.class). The browser interprets the particular format sent (html, XML etc.).
Other communication forms
• html -> applet -> servlet
– Client (Browser or some Java application program) interprets
–
–
–
–
html which calls an applet from the server.
Applet bytecodes are interpreted by the browser (or java
application program). Applet calls back to the server for the
execution of a servlet.
Server executes the servlet.
The response of the servlet is sent back to the browser 9or java
application program) in some format (like html).
Browser (or java application program) interprets response.
14
Other communication forms
• html-> servlet-> applet
– In this example of different modules communicating with each
other, first the html code interpreted by the client (Browser or a
Java application program) calls for a servlet to be executed by the
server.
– The server executes the servlet and sends the http response to the
client.
– The client interprets the response and as a result a call to an applet
is made (to the server).
– The server sends the applet to the client.
– The client interprets the applet code.
15
Other communication forms
• In this course we will examine some of the
previous communication architectures as
well as more complicated architectures on
the server side i.e.
– The servlet could be calling another server called a
RMI server which in turn queries a data base.
– Or, the servlet calls a session Enterprise Java Bean
which in turn calls an Entity bean which in turn queries
a database.
16
INHERITANCE REVIEW
• JAVA DOES NOT SUPPORT MULTIPLE INHERITANCE
• SINGLE INHERITANCE IS APPLIED IN JAVA VIA KEYWORD
extends
– MULTIPLE INHERITANCE CAN BE SIMULATED BY USING INTERFACES
17
A Sample Vehicle Hierarchy
This hierarchy is depicted using a Unified Modeling
Language (UML) diagram.
In UML diagrams, arrows point from the subclass to
the superclass.
18
Superclasses and Subclasses
• A big advantage of inheritance is that we
can write code that is common to multiple
classes once and reuse it in subclasses
• A subclass can define new methods and
instance variables, some of which may
override (hide) those of a superclass
19
Inheritance Rules
Superclass
Members
Inherited
by
subclass?
Directly Accessible by Directly Accessible by
Subclass?
Client of Subclass?
public fields
yes
yes, by using field
name
yes
public methods
yes
yes, by calling method
from subclass methods
yes
protected fields yes
yes, by using field
name
no, must call accessors
and mutators
protected
methods
yes
yes, by calling method
from subclass methods
no
private fields
no
no, must call accessors
and mutators
no, must call accessors
and mutators
no
no
private methods no
20
Inheritance Rules for Constructors
Superclass
Members
Inherited
by
subclass?
Directly Accessible by
Subclass?
Directly Accessible by
Client of Subclass Using
a Subclass Reference?
constructors
no
yes, using
super( arg list )
no
in a subclass constructor
21
Subclass Constructors
• Constructors are not inherited.
• However, the subclass can call the constructors
of the super class to initialize inherited fields.
• Implicit invocation
– The default constructor of the subclass automatically calls
the default constructor of the superclass.
• For explicit invocation, use this syntax:
super( argument list );
If used, this statement must be the first statement in the
subclass constructor.
22
Multiple Inheritance
• The concept of multiple inheritance is shown below (even
though Java does not allow it).
Class
A
Class
B
Class
C
Class
D
23
abstract Classes and Methods
An abstract class is a class that is not
completely implemented.
Usually, the abstract class contains at least
one abstract method.
– An abstract method specifies an API but does
not provide an implementation.
– The abstract method is used as a pattern for a
method the subclasses should implement.
24
More on abstract Classes
An object reference to an abstract class can be
declared.
– We use this capability in polymorphism, which will
be discussed later.
An abstract class cannot be used to instantiate
objects (because the class is not complete).
An abstract class can be extended.
– subclasses can complete the implementation and
objects of those subclasses can be instantiated
25
Defining an abstract class
To declare a class as abstract, include the
abstract keyword in the class header:
accessModifier abstract class ClassName
{
// class body
}
26
Example Hierarchy
We can define a Figure
hierarchy.
• The superclass is Figure,
which is abstract. (In the
UML diagram, Figure is
set in italics to indicate
that it is abstract.)
• We will derive two
subclasses: Circle and
Square.
27
Subclasses of abstract Classes
• A subclass of an abstract class can implement
all, some, or none of the abstract methods.
• If the subclass does not implement all of the
abstract methods, it must also be declared as
abstract.
• Our Circle subclass adds a radius instance
variable and implements the draw method.
• Our Square subclass adds a length instance
variable and implements the draw method.
28
Restrictions for Defining
abstract Classes
• Classes must be declared abstract if the
class contains any abstract methods
• abstract classes can be extended
• An object reference to an abstract class can
be declared
• abstract classes cannot be used to
instantiate objects
29
Restrictions for Defining
abstract Methods
• abstract methods can be declared only within
an abstract class
• An abstract method must consist of a method
header followed by a semicolon
• abstract methods cannot be called
• abstract methods cannot be declared as private
or static
• A constructor cannot be declared abstract
30
The
Figure
Class
public abstract class Figure
{
private int x;
private int y;
private Color color;
// usual constructors, accessors,
// and mutators
// abstract draw method
public abstract void draw( Graphics g );
}
All classes in the hierarchy will have an (x, y)
coordinate and color. Subclasses will
implement the draw method.
31
Polymorphism
An important concept in inheritance is that an
object of a subclass is also an object of any of
its super classes.
• That concept is the basis for an important OOP
feature, called polymorphism.
• Polymorphism simplifies the processing of
various objects in the same class hierarchy
because we can use the same method call for
any object in the hierarchy using a super class
object reference.
32
Polymorphism Conditions
conditions for polymorphism:
– The Figure, Circle, and Square classes are in
the same hierarchy.
– The non-abstract Circle and Square classes
implement the draw method.
– We assigned the Circle and Square objects to
Figure references.
– We called the draw method using Figure
references.
33
Example
• Suppose we have a class FigureClient.
• This class uses the previous classes to actually draw figures.
i.e. (assuming that all classes are in the same folder)
public class FigureClient
{
public static void main ( String [] rags)
{
Circle circle=new Circle();
Square square=new Square();
Figure figure; //just declaration, no instantiation is allowed
figure=circle;
figure.draw(); // a circle will be drawn
figure=square;
figure.draw(); // a square will be drawn now.
…………………………………………………………………………………
34
Superclasses and Subclasses
• A big advantage of inheritance is that we
can write code that is common to multiple
classes once and reuse it in subclasses
• A subclass can define new methods and
instance variables, some of which may
override (hide) those of a superclass
35
Interfaces
A class can inherit directly from only one class, that is, a
class can extend only one class.
To allow a class to inherit behavior from multiple sources,
Java provides the interface.
• An interface typically specifies behavior that a class will
implement. Interface members can be any of the
following:
 classes
 constants
 abstract methods
 other interfaces
36
Interface Syntax
To define an interface, use the following syntax:
accessModifier interface InterfaceName
{
// body of interface
}
All interfaces are abstract; thus, they cannot be
instantiated. The abstract keyword, however,
can be omitted in the interface definition.
37
Finer Points of Interfaces
• An interface's fields are public, static, and
final. These keywords can be specified or
omitted.
• When you define a field in an interface, you
must assign a value to the field.
• All methods within an interface must be
abstract, so the method definition must consist
of only a method header and a semicolon. The
abstract keyword also can be omitted from the
method definition.
38
Inheriting from an Interface
To inherit from an interface, a class declares that it
implements the interface in the class definition,
using the following syntax:
accessModifier class ClassName
extends SuperclassName
implements Interface1, Interface2, …
• The extends clause is optional.
• A class can implement 0, 1, or more interfaces.
• When a class implements an interface, the class
must provide an implementation for each method in
the interface.
39
Example
We define an abstract class Animal with one
abstract method :
public abstract void draw( Graphics g );
We define a Moveable interface with one abstract
method:
public interface Moveable
{
int FAST = 5; // static constant
int SLOW = 1; // static constant
void move( ); // abstract method
}
40
Derived Classes
• TortoiseRacer class
– extends Animal class
– implements Moveable interface
– implements draw and move methods
• TortoiseNonRacer class
– extends Animal class
– (does not implement Moveable interface)
– implements draw method only
41
Multiple Inheritance
• Suppose we have abstract class Student (template class).
– This class has abstract method getGPA which returns the gpa.
• Suppose we also have abstract class Employee (template class)
– This class has abstract method getSalary which returns the salary.
• Class StudentEmployee wants to inherit both classes and implement both
the gpa and the salary.
• It can not do that because multiple inheritance is not allowed
public class StudentEmployee extends Student extends Employee
ABOVE IS NOT LEGAL!!!!!!!
42
Multiple Inheritance
• Let us leave Employee intact and instead remove the
abstract method getGPA from the Student class.
– Thus there is no need for Student to be abstract any
more.
• Let us create an interface :
public interface StudentInterface
defines the method getGPA
• Let us create a class StudentImpl which inherits Student
and implements interface StudentInterface. It implements
the method getGPA which calculates the gpa.
43
Multiple Inheritance
• Now class StudentEmployee extends Employee and it also implements
the interface StudentInterface. Therefore it implements the method
getGPA but kind of indirectly (via the implementation of the method in
the StudentImpl class):
StudentImpl st=new StudentImpl();
public float getGPA()
{
return st.getGPA();
}
StudentEmployee object can invoke other methods of class Student
since StudentImpl also inherits student.
It also can use the class Employee since it inherited that class directly.
44
Multiple Inheritance
Abstract
Employee
class
Class
StudentEmplo
yee
Student class
Interface
StudentInter
face
StudentImpl
Uses StudentImpl
45
Example of Multiple Inheritance
• Example of a Multiple Inheritance
Implementation is posted on the Web Site
Examples page.
• In the example our goal it create a class
called Expenses that inherits from both
classes Employee and OperatingExpenses.
Class CorporationTest tests the multiple
inheritance.
46
Example of Multiple Inheritance
•
•
•
•
•
•
Class Employee is abstract because method compensation is abstract.
Classes Engineer, Management, AdministartivePersonel inherit Employee
and implement compensation according to their requirements.
Interface ExpenseInterface defines method getOperatingExpenses
Class Expense extends Employee and implements interface
ExpenseInterface
Class OperatingExpenses has methods that calculates different operating
expenses for the corporation.
Class ExpenseImpl extends OperatingExpenses and implements the
interface ExpenseInterface
47
Administrative
Personel
Engineer
Abstract class
Employee
Management
OperatingExpenses
ExpensesInterface
OperatingExpensesImpl
Expenses
Uses
OperatingExpensesImpl
48
Example of Multiple Inheritance
• Study program “Multiple Inheritance
Example” available in the Examples (page)
of the course’ s web site.
49