Principles of Programming
Languages
Unit-4
INHERITANCE,
PACKAGES AND
EXCEPTION
HANDLING USING
JAVA
Prepared By Mr. Vipin K. Wani
Syllabus
2
Inheritances: member access and inheritance, super class references
Using super multilevel hierarchy constructor call sequence method
overriding , dynamic method dispatch, abstract classes , Object class.
Packages and Interfaces: defining a package, finding packages and
CLASSPATH, access protection, importing packages, interfaces
(defining, implementation, nesting, applying), variables in interfaces,
extending interfaces, instance of operator.
fundamental, exception types, uncaught exceptions, try, catch, throw,
throws, finally, multiple catch clauses, nested try statements, built-in
exceptions, custom exceptions (creating your own exception sub
classes).
Managing I/O:Streams, Byte Streams and Character Streams,
Predefined Streams, Reading console Input, Writing Console Output,
Print Writer class
Prepared By Mr. Vipin K. Wani
Inheritance
3
Method Overriding (so runtime polymorphism can be
achieved).
For Code Reusability.
Prepared By Mr. Vipin K. Wani
INHERITANCE IN JAVA
4
class Subclass-name extends Superclass-name
{
//methods and fields
}
• The extends keyword indicates that you are making a new class that
derives from an existing class. The meaning of "extends" is to increase
the functionality.
• In the terminology of Java, a class which is inherited is called parent or
super class and the new class is called child or subclass.
Prepared By Mr. Vipin K. Wani
Inheritance (IS-A) vs. Composition (HAS-A) Relationship
Prepared By Mr. Vipin K. Wani
INHERITANCE IN JAVA
6
class Employee{ float
salary=40000;
}
class Programmer extends Employee{ int
bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
} }
Output : Programmer salary is:40000.0
Bonus of programmer is:10000
Prepared By Mr. Vipin K. Wani
INHERITANCE IN JAVA
7
class Operation{
int square(int n){
return n*n; } }
class Circle{
Operation op;//aggregation
double pi=3.14;
double area(int radius){
op=new Operation();
int rsquare=op.square(radius);//code reusability
(i.e. delegates the method call).
return pi*rsquare;
}
public static void main(String args[]){
Circle c=new Circle();
double result=c.area(5);
System.out.println(result);
}
}
Prepared By Mr. Vipin K. Wani
INHERITANCE IN JAVA
8
Prepared By Mr. Vipin K. Wani
METHOD OVERRIDING
9
Prepared By Mr. Vipin K. Wani
SUPER KEYWORD
10
Prepared By Mr. Vipin K. Wani
FINAL IN JAVA
11
The final keyword can be applied with
the variables, a final variable that have
no value it is called blank final variable
or uninitialized final variable. It can be
initialized in the constructor only. The
blank final variable can be static also
which will be initialized in the static
block only.
Prepared By Mr. Vipin K. Wani
STATIC VS DYNAMIC BINDING
12
Prepared By Mr. Vipin K. Wani
ABSTRACT CLASS
13
Prepared By Mr. Vipin K. Wani
Interface
14
Prepared By Mr. Vipin K. Wani
Interface
15
Prepared By Mr. Vipin K. Wani
Interface
16
Prepared By Mr. Vipin K. Wani
Interface
17
Prepared By Mr. Vipin K. Wani
Interface
18
Prepared By Mr. Vipin K. Wani
POINTS TO REMEMBER FOR INTERFACE
19
Like abstract classes, interfaces cannot be used to create objects
(in the example above, it is not possible to create an "Animal"
object in the MyMainClass)
Interface methods do not have a body - the body is provided by the
"implement" class
On implementation of an interface, you must override all of its
methods
Interface methods are by default abstract and public
Interface attributes are by default public, static and final
An interface cannot contain a constructor (as it cannot be used to
create objects)
Prepared By Mr. Vipin K. Wani
Interface
20
interface TestInterface
{
public void square(int a); // abstract method
default void show() // default method
{
System.out.println("Default Method Executed");
} }
class TestClass implements TestInterface
{
// implementation of square abstract method
public void square(int a)
{ System.out.println(a*a); }
public static void main(String args[])
{
TestClass d = new TestClass();
d.square(4); // default method executed
d.show(); } }
Prepared By Mr. Vipin K. Wani
Package
21
Prepared By Mr. Vipin K. Wani
Packages in Java
22
Prepared By Mr. Vipin K. Wani
Packages in Java
23
Prepared By Mr. Vipin K. Wani
Packages in Java
24
Prepared By Mr. Vipin K. Wani
Packages in Java
25
Prepared By Mr. Vipin K. Wani
Packages in Java
26
Prepared By Mr. Vipin K. Wani
EXCEPTION
27
Prepared By Mr. Vipin K. Wani
EXCEPTION
28
Prepared By Mr. Vipin K. Wani
EXCEPTION
29
Prepared By Mr. Vipin K. Wani
EXCEPTION
30
Prepared By Mr. Vipin K. Wani
EXCEPTION
31
Prepared By Mr. Vipin K. Wani
EXCEPTION
32
Prepared By Mr. Vipin K. Wani
JAVA IO
33
Prepared By Mr. Vipin K. Wani
JAVA IO
34
Prepared By Mr. Vipin K. Wani
OUTPUTSTREAM
35
Prepared By Mr. Vipin K. Wani
INPUTSTREAM
36
Prepared By Mr. Vipin K. Wani
Stream
37
A stream is a sequence of data. In Java, a
stream is composed of bytes. It's called a
stream because it is like a stream of water
that continues to flow.
In Java, 3 streams are created for us
automatically. All these streams are
attached with the console.
1) System.out: standard output stream
2) System.in: standard input stream
3) System.err: standard error stream
Prepared By Mr. Vipin K. Wani
Stream
38
OutputStream : Java application uses an
output stream to write data to a destination;
it may be a file, an array, peripheral device or
socket.
InputStream : Java application uses an input
stream to read data from a source; it may be
a file, an array, peripheral device or socket.
Prepared By Mr. Vipin K. Wani
Stream
39
import java.io.PrintWriter;
class Main
{
public static void main(String[] args)
{ String data = "This is a text inside the file.";
try {
PrintWriter output = new PrintWriter("output.txt");
output.print(data);
output.close();
} catch(Exception e)
{ e.getStackTrace(); }
}
}
Prepared By Mr. Vipin K. Wani
Stream
40
import java.io.File;
import java.io.PrintWriter;
public class PrintWriterExample {
public static void main(String[] args) throws Exception {
PrintWriter writer = new PrintWriter(System.out);
writer.write(“HELLO TO ALL");
writer.close();
PrintWriter writer1 = new PrintWriter(new File("D:\\
testout.txt"));
writer1.write("Like Java, Spring, Hibernate, Android, PHP et
c.");
writer1.flush();
writer1.close();
}
}Prepared By Mr. Vipin K. Wani
Stream
41
public class ReadFileUsingFileInputStream
{
public static void main(String[] args)
{
File file = null;
FileInputStream fileInputStream = null;
try {
file = new File("D:/data/file.txt");
fileInputStream = new FileInputStream(file);
int line; while ((line=fileInputStream.read()) != -1) {
System.out.print((char)line);
}} catch (Exception e)
{ e.printStackTrace(); }}}
Prepared By Mr. Vipin K. Wani
Referances
42
1. DEUGO || JAVA GEMS , Cambridge University Press
2. Java The Complete Reference, 9th Edition, Herbert Schildt, TMH
3. https://onlinecourses.nptel.ac.in/noc20_cs08/preview
4.https://onlinecourses.swayam2.ac.in/aic20_sp13/preview
5. https://onlinecourses.nptel.ac.in/noc19_cs84/preview
6. http://www.javatpoint.com/
7. https://beginnersbook.com/java-tutorial-for-beginners-with-examples/
8. https://www.tutorialspoint.com/java/index.htm
9. https://www.w3schools.com/java/default.asp
Prepared By Mr. Vipin K. Wani
Thank You…!
43
Prepared By Mr. Vipin K. Wani