OOP JAVA M3 Ktunotes - in
OOP JAVA M3 Ktunotes - in
Module 3
Example: Any classes that we declare to be part of the package GECI must
store their .class files in a directory called GECI.
– Here, pkg1 is the name of a top-level package, and pkg2 is the name
of a subordinate package inside the package pkg1 separated by a dot
(.). Here square bracket denotes that it is optional.
• E.g.
import pack1; // import the package pack1
import java.io.*; // import all the classes from the package java.io
import java.util.Date; //import the Date class from the package java.util
System.out.println("-->");
System.out.println(name+": $"+bal);
}
}
PUBLIC
Interfaces specify what a class must do and not how. It is the blueprint of the
class.
An Interface is about capabilities like a Car may be an interface and any class
implementing Car must be able to (or must implement) start(). So it specifies a
set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all
functions specified in the interface, then the class must be declared abstract.
interfacename obj=object of
implementing class;
The top level interface must either be declared as public or use the default access
level.
Downloaded from Ktunotes.in
New features added in interfaces in JDK 8
● Prior to JDK 8, interface could not define
implementation. We can now add default
implementation for interface methods.
● This default implementation has special use
and does not affect the intention behind
interfaces.
● Suppose we need to add a new function in an
existing interface.
● Obviously the old code will not work as the
classes have not implemented those new
functions. So with the help of default
implementation, we will give a default body for
the newly added functions. Then the old codes
will still work
Downloaded from Ktunotes.in
New features added in interfaces in JDK 8
● Another feature that was added in
JDK 8 is that we can now define
static methods in interfaces which
can be called independently without
an object. Note: these methods are
not inherited.
– in some cases,
character streams are more efficient
than byte stream
Note:
Usually character stream class names will end with
er [Reader , Writer]
• Each time that read( ) is called, it reads a character from the input stream and
returns it as an integer value.
• To read a string from the keyboard, use the version of readLine( ) that is a
member of the BufferedReader class.
System.out for the output stream and flush the stream after
each new line.
PrintWriter pw = new PrintWriter(System.out, true);
1. try - The block of code which can produce exception is kept under try block
2. throw - To throw an exception from a method to its calling method
3. throws - To declare a method which can throw exception
4. catch - This block catches the exception from try block (at least one catch)
5. finally - This block is after the catch block, executes always
Downloaded from Ktunotes.in
Exception handling
try { // block of code
}
catch ( ExceptionType1 e) {
// Exception handling routine for ExceptionType1 (optional)
}
catch (ExceptionType2 e ) {
// Exception handling routine for ExceptionType2 (optional)
}
. .
.
catch (ExceptionType_n e) {
// Exception handling routine for ExceptionType_n }
finally {
Downloaded from Ktunotes.in
No exception Exception
try{
//File Operation statements
}
catch(FileNotFoundException e)
{
System.out.println("File not found"); //print this message if file is not found
}
FileInputStream fileobject;
fileobject = new FileInputStream(“Sample.txt");
//statements for reading the file
fileobject.close();
FileInputStream fileobject;
fileobject = new FileInputStream(“Sample.txt");
2. Next,we can use a version of read( ) that is defined within FileInputStream. int
read( ) throws IOException
int c=fileobj.read();
• Each time read() called, it reads a single byte from the file and returns the byte as
an integer value.
– read( ) returns –1 when the end of the file is encountered.
– read() can throw an IOException.
Downloaded from Ktunotes.in
Reading from a file
1. First, we have to create FileOutputStream class object and pass filename as the
parameter to the constructor.
2. Using write function store the byte value in file
int c=65;
FileOutputStream fileobject;
fileobject = new FileOutputStream(“Sample.txt");
fileobject.write(c);
• Here lower order will be stored.So this will store ASCII value of 65 that is letterA in
file Sample.txt Downloaded from Ktunotes.in
Writing to a file
FileReader(String filePath)
FileReader(File fileObj)
– They can throw a FileNotFoundException. Here, filePath is the full path name of
a file, and fileObj is a File object that describes the file
• Similarly, during the process of deserialization, all of these objects and their
references are correctly restored when deserialization is done at the top.
Case 2: If a superclass is not serializable then subclass can still be serialized : Even
though superclass doesn’t implements Serializable interface, we can serialize subclass
object if subclass itself implements Serializable interface. So we can say that to
serialize subclass object, superclass need not to be serializable.
● GeeksforGeeks - www.geeksforgeeks.org
● Wikipedia - www.wikipedia.org
● Tutorialspoint - www.tutorialspoint.com