You are on page 1of 7

Indian Institute of Information Technology, Pune

Department CSE

Name: Suktey Tuljaram Arjun Enrollment No: 112015145

Year: 2nd Semester: 4th

Date of Submission 3-4-2022 Subject Name JAVA

Theory Assignment – 6 (02/04/2022)


Q1 Describe the various forms of implementing interfaces.
Ans-1 There are 3 types of interfaces in java
- Marker interfaces
- Functional interfaces
- Normal interfaces

 Marker interfaces:
It is the empty interface. Marker interface does not have any
method in it.
There are 3 types of Marker interfaces:
1. Serializable interface: It is used to convert object into byte
stream.
2. Cloneable interface: It is used to clone the object.
3. Remote interface: It allows an object residing in one system
to access or invoke an object running on another system.
Code:

import java.util.*;
import java.io.*;
interface P {}
class basic implements P
{
void show() {System.out.println("hello");}
Indian Institute of Information Technology, Pune

}
public class marker_interface_1
{
public static void main(String args[])
{
basic x=new basic();
if(x instanceof P) x.show();
else System.out.println("NO\n");
}
}

 Function interface:
It is the interface which has only one abstract method. Functional
interface is also called as Single abstract method(SAM) interface.
Runnable , Comparable are the example of Functional interface.
Code:

import java.io.*;
import java.util.*;

@FunctionalInterface
interface Q { void show();}
public class function_interface_1
{
public static void main(String args[])
{
Q x=new Q()
{
public void show()
{
System.out.println("hello i am single function");
}
};
x.show();
}
}

 Normal interfaces:
It is the interface which has more than one abstract method.
Code:

import java.util.*;
import java.io.*;
Indian Institute of Information Technology, Pune

interface R
{
void show();
void colur(String s);
}
public class normal_interface_1 implements R
{
public static void main(String args[])
{
normal_interface_1 x=new normal_interface_1();
x.show();
x.colur("red");
}
@Override
public void show()
{
System.out.println("hi i am normal interface");
}
@Override
public void colur(String s)
{
System.out.println("i like "+s);
}
}

Q2 Discuss the various levels of access protection available


for packages and their implications.
Ans-2 There are 4 types of access protection available for packages & their
implication.
- Private
- Protected
- Public
- Default

 Private:
In this variable & class are declared private, such that they can’t be
access outside the class. similarly packages with private data
members also can’t called in different package
Example code:
Indian Institute of Information Technology, Pune

public class random_test


{
private static void add()
{}
private int abc;
}

 Protected:
In this variable & class are protected, such that they can be access
within the same package & the following sub-classes
Example code:

public class random_test


{
protected static void add()
{}
protected int abc;
}

 Public :
In this variable & class are public, such that they can be access from
anywhere within the package and outside the package.
Example code:

public class random_test


{
public static void add()
{}
public int abc;
}

 Default:
In this variable & class are not given any access modifiers, such that
they can be access from only same package .like protected but not
from sub-classes
Example code:

public class random_test


{
static void add()
{}
Indian Institute of Information Technology, Pune

int abc;
}

Q3 What is an exception? How do we define try, catch and


finally block.
Ans-3 It is an event that occurs during the execution of a program and disrupts
the normal flow of the program's instructions. Bugs or errors that we
don't want and restrict our program's normal execution of code are
referred to as exceptions.
To overcome this problem we use try, catch, finally, throw statements.
Try: it allows to define a block of code to be tested for errors while it is
being executed
Catch: it allows you to define a block of code to be executed, if an error
occurs in the try block

Throw: it is used to explicitly throw an exception from a method or any


block of code
Finally: it contains the block of code that must be executed whether an
exception occurs or not.
Code example:
public class random_test
{
public static void main(String args[])
{
try
{
int y=0,x=3/y;
if(x>0) throw new ArithmeticException("variable
accepted");
}
catch (ArithmeticException e)
{
System.out.println(e);
}
finally {System.out.println("program ends");}
}
}
output:
if y==0
Indian Institute of Information Technology, Pune

If y>0

Q4 Describe the functions of the File class?


Ans-4 Java File class represents the files and directory pathnames in an abstract
manner. This class is used for creation of files and directories, file
searching, file deletion
There are many file function which can be used while file handling in java
Function properties Return type
createNewFile() It atomically creates a new, empty file named Boolean
by this abstract pathname if and only if a file
with this name does not yet exist
canWrite() It tests whether the application can modify the Boolean
file denoted by this abstract pathname
canExecute() It tests whether the application can execute the Boolean
file denoted by this abstract pathname
canRead() It tests whether the application can read the file Boolean
denoted by this abstract pathname
isAbsolute() It tests whether this abstract pathname is Boolean
absolute.
isFile() It tests whether the file denoted by this abstract Boolean
pathname is a normal file.
getName() It returns the name of the file or directory String
denoted by this abstract pathname
getParent() It returns the pathname string of this abstract String
pathname's parent, or null if this pathname
does not name a parent directory
toPath() It returns a java.nio.file.Path object constructed File Path
from the this abstract path
getFreeSpace() It returns the number of unallocated bytes in Long
the partition named by this abstract path name.

This are some function that we can use while performing file handling

Q5 Distinguish between
Indian Institute of Information Technology, Pune

a. InputStream and Reader classes


b. OutputStream and Writer classes
Ans-5 (1)
InputStream Reader classes
It is byte based, it can be used to read It is character based, it can be used to
bytes read characters
Stream is used to binary input Reader is used to character input
It is used for reading binary files It is used for reading text files in
platform default encoding
FileInputStream,read() reads 1 byte at a FileReader.read() reads 2 bytes at a time
time
It must be used when we are reading It must be used when we have to read
audio, video or other multimedia files text files, pdfs or word doc.
(2)
outputstream Writer classes
It is byte based, it can be used to write It is character based, it can be used to
bytes read characters
Stream is used to binary output Writer is used to character output
It is used for writing binary files It is used for writing text files in platform
default encoding
FileOutputStream.write() writes 1 byte FileWriter.write() writes 2 bytes at a
at a time time
It must be used when we hae to feed or It must be used when we have to write
write some complex data into the file data into a text file, pdfs or word doc
Like audio,video etc

You might also like