2. Classes implement the interface and provide implementation for the abstract methods
3. Interface reference can be used to call the implemented methods at runtime (polymorphism)
Multiple Inheritance using interfaces
1. A class can implement multiple interfaces
2. Each interface defines abstract methods that the class must implement
Nested interface
An interface declared within another interface or class. Used to group related interfaces for easier maintenance. Must be referred by the outer interface or class, cannot be accessed directly.
Variables in interface
Public, static, and final by default. No access modifier allowed except public. Must be initialized in the interface itself. Implementing class cannot modify the variables.
Stream
A sequence of data, composed of bytes. Java has 3 standard streams: System.out, System.in, System.err.
Java I/O Classes
Byte Streams (InputStream, OutputStream)
Character Streams (Reader, Writer)
Reading and writing Files (Byte Streams)
1. FileInputStream: read bytes from file
2. FileOutputStream: write bytes to file
3. BufferedInputStream: optimizes performance by buffering data in memory
4. DataInputStream/DataOutputStream: read/write primitive data types directly
Serialization
Process of writing the state of an object to a byte stream to save it in persistent storage. The class must implement Serializable interface.
Transient and static attributes are not serialized
Serializable
A class that can be converted to a stream of bytes and then reconstructed
If a class is Serializable, all of its sub classes are also serializable
Transient attributes
Attributes that will not be serialized
Static attributes
Attributes that will not be serialized
Writing object to a File
1. Create FileOutputStream
2. Create ObjectOutputStream
3. Write object using writeObject()
4. Close ObjectOutputStream
Student class
Implements Serializable interface
Student class has instance variables rno and name
Student class has a display() method to print the roll number and name
Retrieving and displaying objects from a file
1. Create FileInputStream
2. Create ObjectInputStream
3. Read objects using readObject()
4. Call display() method on each object
5. Close ObjectInputStream
Character Streams
Built on top of byte streams to facilitate handling of character (text) data
Reader
Abstract base class for reading (input) related classes in character streams
Writer
Abstract base class for writing (output) related classes in character streams
FileReader
Helps to read data from a file as characters (text)
BufferedReader
Used for performance optimization, has readLine() method to read a line at a time from the input buffer
Reading from a file using FileReader and BufferedReader
1. Create FileReader
2. Create BufferedReader
3. Read lines using readLine()
4. Print each line
5. Close BufferedReader
FileWriter
Helps to write data to a file as characters (text)
Writing to a file using FileWriter
1. Create FileWriter
2. Write characters using write()
3. Close FileWriter
File class
Abstract representation of file and directory pathnames, not used to actually read or write data
File class
Used for making new empty files, searching for files, deleting files, and making directories
Provides metadata information about a file
RandomAccessFile
Can open a file for both reading and writing, has methods to position the file pointer at a required position
Console class
Provides methods to accept input from the command line and write formatted output to the monitor
Reading a string from the console
1. Get Console object
2. Use readLine() method to read string
Reading a password from the console
1. Get Console object
2. Use readPassword() method to read password
Writing formatted output to the console
1. Get Console object
2. Use format() or printf() method to write formatted output