4. Interface
A 100% abstract class
A contact for what a implementing
class can do.
Behavioural purposes
Class can implement multiple
interfaces
15. Time for some practical example
A rudimentary banking application
Objective: Create some customers
and create their saving and current
accounts. Display them on the
screen. That’s it!
16. Practical Example 2
Duck simulation game once again?
Objective: To show different type of ducks
on the screen (Time being each duck can
be represented by different text). Some
of the ducks can fly / quack while some
cannot. Code should first be implemented
for Mallard and rubber duck. It should be
expandable for other duck type.
17. Collections – some groundwork
toString() method (To be covered later)
“==“ VS “equals()”
== compares references. It’s a bit to bit
comparison.
However, equals method is overridden by
some classes to compares the values of
reference variables for ex: String, Integer
etc.
Example @ Eclipse
18. Collections – some groundwork 2
When to override equals?
For class Car?
Color
Engine
Make
2WD / 4WD
VIN
19. Collections – some groundwork 3
hashCode()
Object ID, not necessarily unique,
used my HashMap / HashSet etc to
store reference to the object.
hashCode is used to locate the
object in the memory.
20. Collections – An Introduction
What are collections?
What do we do with collections?
- Add objects
- Remove objects
- Find an Object
- Iterate through collection
23. Collections - Sorting
Collections.sort
OK – But what about classes – How
can we compare them?
Comparable Interface – compareTo
method
Comparator Interface – compare
method
Examples at Eclipse
24. Collections - Search
Searches are performed using binarySearch()
Successful searches return index of element being
searched
Collection / Array should be sorted to facilitate
search
If searched element is not there, then the return
value of search is = -(index at which if searched
element is inserted, sorted order will be
maintained) -1. Like a, c, d, e, f if we search for
b, return value will be -2
Array / Collection is sorted using comparator,
same comparator should be used in search.
25. Inner Classes
A Class can have:
Instance variable
Methods
Classes????
Yes, but what is the use?
26. Inner Classes – Purpose and Use
Chat client example:
Normal operation like typing,
sending, getting messages from
server can be done in ChatClass.
But, what about even handling
messages? Where should they be?
Options are:
Same class
Other class
Inner class
28. Inner Classes
Regular Inner Class (Eclipse)
Method Inner Class
Same as regular inner class with
following differences:
declared inside method
Cannot be instantiated outside method
Cannot use method local variables
Can not use modifiers like public,
private, protected, static
30. Threads
A thread is a single sequential flow of
control within a program.
Multi-threading
Types
Extending
thread class
Implementing runnable interface
34. Java I/O
I/O Streams
Byte Streams handle I/O of raw binary data.
Character Streams handle I/O of character data, automatically
handling translation to and from the local character set.
Buffered Streams optimize input and output by reducing the
number of calls to the native API.
Scanning and Formatting allows a program to read and write
formatted text.
I/O from the Command Line describes the Standard Streams and
the Console object.
Data Streams handle binary I/O of primitive data type and String
values.
Object Streams handle binary I/O of objects.
File I/O
File Objects help you to write platform-independent code that
examines and manipulates files.
Random Access Files handle non-sequential file access.
35. Stream IO
Decendents of classes:
InputStream
OutputStream
For example:
FileInputStream
FileOutputStream
36. Character IO
Wraps byte stream.
All character stream classes are
descended from
Reader
Writer
For Example:
FileReader
FileWriter
44. Serialization
Preserving state of object in a file
Called serialization or flattening of
objects
Three ways to do Serialization:
using
the default protocol
customizing the default protocol
creating our own protocol
45. Serialization using the default protocol
You have to implement interface
“Serializable”
This interface has no methods. So it
is just a marker interface.
ObjectOutputStream
46. Serialization using Customizing the
Default Protocol - I
What will happen if our class
contains objects of some other
classes which are not serializable?
47. Serialization using Customizing the
Default Protocol - II
We have to mark those classes as
serializable?
What if we don’t have access to
code of those classes?
In
case, it is not important to save
state of those objects, the way is to
mark the variables referring to those
objects as transient.
But, what if we also want to save state
of object.
48. Serialization using Customizing the
Default Protocol - III
So, What should we do to
instantiate transient variables? Call
our own method after deserialization?
Of course this is a solution. But,
what if the other developers does
not know about that?
What should we do now?
49. Serialization using Customizing the
Default Protocol - II
Provide following two methods in
your class:
private void writeObject(ObjectOutputStream out) throws
IOException;
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException;
Do your operations and then call:
defaultWriteObject();
defaultReadObject();
Note that these methods are private. So they are not inhereted,
overloaded. In fact, when VM sees that class provides implementation
of these methods it call these methods than default methods.
Remember, VM can call private methods, any other oject cannot.
Also, they can be used to make a call non-serializable even if super
class is serializable.
50. Serialization using Creating our Own
Protocol
Instead of implementing the Serializable interface, you
can implement Externalizable, which contains two
methods:
public void writeExternal(ObjectOutput out) throws IOException;
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException;
Just override those methods to provide your own
protocol. This protocol is entirely in your hands. An
example situation for that alternate type of
serialization: read and write PDF files with a Java
application. If you know how to write and read PDF
(the sequence of bytes required), you could provide the
PDF-specific protocol in the writeExternal and
readExternal methods.
53. Next Sessions – Servlets and JSP
Pre-requisites:
Basic knowledge of HTTP - http://en.wikipedia.org/wiki/HTTP
Basic Knowledge of HTML – Youtube video Basic HTML and CSS Tutorial. Howto
make website from scratch
Introduction to Enterprise applications http://www.scribd.com/doc/4052844/1-Introduction-to-Enterprise-A
Basic Knowledge of J2EE - Youtube videos JEE Introduction
JAVA Enterprise Edition Tutorial
Using the HttpSession
Object: Servlet and JSP Tutorials J2EE