Introduction to Software Engineering

advertisement
CE203 - Application Programming
Udo Kruschwitz
udo@essex.ac.uk
Autumn 2015
CE203 Part 0
1
Module Administration
• 10 lectures (2 hours each): Tuesday 09:00 (LTB 7)
• 10 labs (2 hours each) Tuesday/Wednesday (Lab 1)
• 2 assignments:
– Assignment 1 (10%) due 4th November (week 6)
– Assignment 2 (20%) due 7th December (week 11)
•
•
•
•
Test (10%) on 10th November (week 7)
Two hour exam in May/June (60% of the module credit)
GLAs: Ludvig Kihlman (lzkihl) / Youngjae Song (syoungb)
Module area: http://orb.essex.ac.uk/CE/CE203/
Autumn 2015
CE203 Part 0
2
Acknowledgements
• A lot of the material was prepared by Mike Sanderson
• Some parts were prepared by Martin Waite
• Additional material by me
Autumn 2015
CE203 Part 0
3
Recommended Reading
There are many books that cover the material in this course
(see last year’s recommendations in CE152) – I will refer to
Java How To Program (9th ed.), H.M. Deitel & P.J.Deitel
(Prentice Hall, 2012)
Introduction to Java Programming (8th ed.), Y.D. Liang
(Pearson, 2011)
If you choose to buy a different text-book you should check
if it uses at least Java 5.0 and covers the material listed in
the syllabus in the course catalogue entry for CE203.
(In Lab 1 you will find Java 8 installed now).
Autumn 2015
CE203 Part 0
4
Recommended Reading 2
Other good books:
• Learning Java (4th ed.), P. Niemeyer and D. Leuck
(O’Reilly, 2013)
• Java in a Nutshell (5th ed.), D. Flanagan (O’Reilly, 2005)
(reference book)
Autumn 2015
CE203 Part 0
5
Learning Outcomes
1. Demonstrate a knowledge of core Java application
libraries
2. Explain the event-driven model underlying Java GUIs
3. Write Java programs with interactive graphical user
interfaces (GUIs)
4. Write Java programs that interact with databases
5. Write Java programs that make efficient use of the Java
collections package.
6. Work with the Java Security Manager to create secure
program policies.
Autumn 2015
CE203 Part 0
6
Brief Outline
•
•
•
•
•
•
•
Review of object-oriented programming with Java
Applets
Exceptions
Collections
JDBC
Security
Guest lecture(s)
Autumn 2015
CE203 Part 0
7
Motivating Examples
. ..
.
.
.
.
.
.
.
Autumn 2015
CE203 Part 0
8
Today: A Bit of Revision
• What you should know by now
• Abstract from the language-specific issues to get a more
conceptual picture of object-oriented programming
• Understanding the principles helps seeing the overall
picture
Autumn 2015
CE203 Part 0
9
Java Characteristics
•
•
•
•
•
•
•
•
•
•
•
Simple
Object Oriented
Interpreted
Portable
Architecture-neutral
High-performance
Distributed
Robust
Secure
Multi-threaded
Dynamic
( Source: LIANG, Y.D., “Introduction to Java Programming”, 6th Edition, Prentice-Hall, 2007)
Autumn 2015
CE203 Part 0
10
Object Orientation – Classes & Objects
An object is an instance of a class
 many objects of the same class can exist in the same
program
A class can exist in a program even if no instance of it exists
 some classes (abstract classes) cannot be instantiated
at all
Autumn 2015
CE203 Part 0
11
Object Orientation - Attributes
•
•
•
•
Both class and object can have attributes
Attributes in Java are simply called variables
Class variables are shared by all instances of the class
Instance variables belong to a particular instance (or
object)
• The state of an object is given by the values of its instance
variables
Autumn 2015
CE203 Part 0
12
Object Orientation - Methods
• Methods can also be defined at either class or instance
level
• There are basically three kinds of methods
 access methods (get and set attribute values)
 service methods (to offer services to other objects)
 housekeeping methods (for internal use)
Autumn 2015
CE203 Part 0
13
Object Orientation – Information Hiding
• External access to an object’s attributes and methods is
controlled by means of the scope modifiers private,
public and protected
• Direct access to attributes is usually prohibited
• Access is usually gained through secure public methods
Autumn 2015
CE203 Part 0
14
Object Orientation:
Composition & Specialisation
Autumn 2015
CE203 Part 0
15
Parameterisation and References
• In Java, objects are “reference types” (unlike primitive
data types!)  the value passed when the actual
parameter is an object is a reference.
• Parameters of primitive type are always passed using call
by value  the value associated with the actual parameter
cannot be changed
• Parameters that are objects are always passed using call
by reference  the value(s) associated with the actual
parameter can be changed
Autumn 2015
CE203 Part 0
16
Method Overloading
We can supply several definitions for a method sum within
the same class definition, as long as each definition has a
different set of input types.
int sum (int i, int j)
{return i + j;}
// version 1
double sum (double e, double f) // version 2
{return e + f;}
int sum (int i, int j, int k)
{return i + j + k;}
Autumn 2015
CE203 Part 0
// version 3
17
Method Overloading 2
sum(4, 5)
sum(4.5, 5.5)
sum(4, 5, 6)
will call version 1 on the previous slide
will call version 2 on the previous slide
will call version 3 on the previous slide
This process is known as static binding, because the decision
about which version of the method to apply is made at the
compilation stage.
Autumn 2015
CE203 Part 0
18
Class Definitions
Class definitions provide a unit of scope:
 they encapsulate data and operations on that data
 they can specify the attributes and methods of an
entire class
 they can specify the attributes and methods of
instances of the class
Class definitions are hierarchical:
 they can be written as extensions of existing class
definitions
 they can inherit or redefine parts of the superclass
definitions
Autumn 2015
CE203 Part 0
19
Class Definitions 2
The class definition is (in part) a specification for an object
An object is an instance of a class just as the number 3
is an instance of the type int
 Objects are created dynamically (at run time) using the
keyword new and come into existence when memory
is allocated for them on the heap
 Objects cease to exist when they are no longer
required (the space they occupied is automatically
reclaimed by the garbage collector)
Autumn 2015
CE203 Part 0
20
Class Definitions 3
The class definition is (in part) a specification for the entire
class
 Any member (variable or method) introduced with the
keyword static becomes part of the static context,
and exists throughout program execution
 the class can exist independently of objects of the class
 static members can be used without creating objects
Autumn 2015
CE203 Part 0
21
Class Definitions 4
• A static variable is known as a class variable
(one set of class variables is shared by all objects of the
class)
• A non-static variable is known as an instance variable
(each object of the class has its own set of instance
variables)
Autumn 2015
CE203 Part 0
22
Inheritance
The class definition inherits method and variable definitions
• methods and variables defined in the superclass are
automatically included in the definitions of its subclasses
• the inheritance is transitive, so classes inherit the attributes
of the superclass of their superclass, and so on.
• since every class is derived originally from the class
Object, all objects inherit Object’s methods, e.g.
toString
• inherited members of the superclass can be redefined in
the subclass (variable shadowing, method overriding)
Autumn 2015
CE203 Part 0
23
Accessibility
The public and private modifiers may be omitted from
class definitions and variable and method declarations. By
default, their scope becomes the enclosing package.
Autumn 2015
CE203 Part 0
24
Accessibility Rules
1. With no modifier, access is permitted from anywhere
in the enclosing package.
2. The public modifier permits access from beyond the
enclosing package.
3. The private modifier prevents access from outside
the enclosing class.
4. The most restrictive modification applies
Autumn 2015
CE203 Part 0
25
Accessibility Examples
• An unmodified member cannot be accessed from outside
the package even if it is contained in a public class.
• A public member cannot be accessed from outside the
package if it is contained in an unmodified class.
Autumn 2015
CE203 Part 0
26
Classes and Instances
As soon as the program starts, the class descriptions are
activated:
• space for class variables is allocated at a location
within static memory
• the space remains allocated to those variables until the
program stops
• classes exist throughout program execution
• class members (variables and methods) can always be
used
Autumn 2015
CE203 Part 0
27
Classes and Instances 2
When a new instance (object) is created with new
• space for the instance variables is allocated on the heap
(new returns a reference to that space), the reference
may be assigned to more than one variable
• if, at any point, there are no “live” references, the space
is reclaimed
• objects only exist while there are existing references to
them
• instance members (variables and methods) can only be
used when an instance exists
Autumn 2015
CE203 Part 0
28
Specialisation
Specialisation hierarchies and Interfaces are closely related
topics
 They both provide support for polymorphism
(the ability to define methods that can operate on many
different types) which is implemented by means of
dynamic binding (which means that the decision about
which version of the method should be applied is made
while the program is executing)
Autumn 2015
CE203 Part 0
29
Specialisation 2
Redefinition of methods is called method overriding.
A superclass method can be completely redefined, or
added to. When adding extra statements to the superclass
method, the superclass method is called first using the
super reference, e.g.
 if a superclass method meth is added to in a subclass,
then in the subclass the first statement in the meth
definition is the call super.meth(), and then further
statements are added for the additional subclass code
Autumn 2015
CE203 Part 0
30
Specialisation – Dynamic Binding
When a method has been redefined (possibly more than
once) in a hierarchy of classes, and that method is called on
an object, then the closest definition (going up the hierarchy
from the object’s class) is applied, for example:
Autumn 2015
CE203 Part 0
31
Specialisation – Dynamic Binding 2
Example (continued):
Autumn 2015
CE203 Part 0
32
Specialisation – Dynamic Binding 3
Now consider the following situation:
It will not be known until runtime whether the user will
choose “cat” or “dog”, i.e.the decision about which version
of noise to use cannot be made by the compiler.
Autumn 2015
CE203 Part 0
33
Specialisation – Dynamic Binding 4
The decision about which version of the method to use is
made when the method is called at run-time, and is therefore
referred to as dynamic binding.
Dynamic binding is possible in Java because the internal
representation of an object includes information about its
class definition.
Dynamic binding supports the following kind of
polymorphism.
Autumn 2015
CE203 Part 0
34
Specialisation – Dynamic Binding 5
Animal[] animalArray = new Animal[4];
…
animalArray[0] = felix;
animalArray[1] = rover;
animalArray[2] = daisy;
animalArray[3] = nanny;
…
for (int i = 0; i < 4; i++)
System.out.println
(animalArray[i].noise());
Autumn 2015
CE203 Part 0
35
Abstract Classes
Abstract classes are partial specifications:
• they (typically) contain at least one abstract method
(a prototype method definition with no body)
• and they cannot be instantiated.
Subclasses of the abstract class can be instantiated if they
provide full definitions for the abstract methods.
The top most levels of a large hierarchy may consist of
several abstract classes.
Autumn 2015
CE203 Part 0
36
Download