Sie sind auf Seite 1von 4

1. What is the importance of using an interface? How it differs from an abstract class?

In interface, you declare the basic structure or skeleton of a class. That is why it is
implemented by another class and not extended.
Whatever you declare inside interface must be public static final. So, all the functions
have to be defined by the class implementing it. If you want a structure to be
implemented by many classes declare them in an Interface and carry on.
Rest all, may it be Abstract class or normal parent class, you need not define all the
functions, etc. in the child class.
So, basically Interface gives you a skeleton of how the other classes using it
must look like.

2. Write down an User defined exception class:


a. Create a user defined exception class MyException.
b. See in the Q2 above - Use the Employee Class and throw the MyException object in
the constructor of Employee class if Category is other than A, B, C, and D.
c. Write the code in main method that creates the object of Employee class and catch
the exception thrown.

3. Using two methods available in Java to write a thread–


a. Write down a thread class that prints a Fibonacci series
b. Write down a thread class that prints the even numbers from 0-100.

c. import java.util.*;
d. import java.io.*;
e. class Fibonacci extends Thread
f. {
g. private PipedWriter out = new PipedWriter();
h. public PipedWriter getPipedWriter()
i. {
j. return out;
k. }
l. public void run()
m. {
n. Thread t = Thread.currentThread();
o. t.setName("Fibonacci");
p. System.out.println(t.getName() + " thread started");
q. int fibo1=0,fibo2=1,fibo=0;
r. while(true)
s. {
t. try
u. {
v. fibo = fibo1 + fibo2;
w. if(fibo>100000)
x. {
y. out.close();
z. break;
aa. }
bb. out.write(fibo);
cc. sleep(100);
dd. }
ee. catch(Exception e)
ff. {
gg. System.out.println("Fibonacci:"+e);
hh. }
ii. fibo1=fibo2;
jj. fibo2=fibo;
kk. }
ll. System.out.println(t.getName() + " thread exiting");
mm. }
nn. }
oo. class Even extends Thread
pp. {
qq. private PipedWriter out1 = new PipedWriter();
rr. public PipedWriter getPipedWriter()
ss. {
tt. return out1;
uu. }
vv. public void run()
ww. {
xx. Thread t= Thread.currentThread();
yy. t.setName("Even");
zz. System.out.println(t.getName() + " thread Started...");
aaa. int even=1;
bbb. while(true)
ccc. {
ddd. try
eee. {
fff. if(even>100)
ggg. {
hhh. out1.close();
iii. break;
jjj. }
kkk. if(isEven(even))
lll. out1.write(even);
mmm. even++;
nnn. sleep(0);
ooo. }
ppp. catch(Exception e)
qqq. {
rrr. System.out.println(t.getName() + " thread exiting.");
sss. System.exit(0);
ttt. }
uuu. }
vvv. }
www. public boolean isEven(int n)
xxx. {
yyy. int m=n;
zzz. if(m%2==0))
aaaa. return true;
bbbb. }
cccc. }

4. Write short note on the following:


i. Super

Super keyword in java is a reference variable that is used to refer parent class object. Super is an implicit keyword

create by JVM and supply each and every java program for performing important role in three places.

 At variable level

 At method level

 At constructor level

Need of super keyword:

Whenever the derived class is inherits the base class features, there is a possibility that base class features are

similar to derived class features and JVM gets an ambiguity. In order to differentiate between base class features and

derived class features must be preceded by super keyword.

ii. Static
The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a
keyword that are used for share the same variable or method of a given class. This is used for a constant variable or a method that is
the same for every instance of a class. The main method of a class is generally labeled static.

No object needs to be created to use static variable or call static methods, just put the class name before the static variable or method
to use them. Static method can not call non-static method.
In java language static keyword can be used for following

5. variable (also known as class variable)


6. method (also known as class method)
7. block
8. nested class

i. final

The final keyword in java is used to restrict the user. The java final keyword can be used in many context. Final can be:

1. variable
2. method
3. class

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. We will have detailed learning of these. Let's first learn the basics of final keyword.

ii. throw and throws

Throws clause is used to declare an exception, which means it works similar to the try-catch block. On the
other hand throw keyword is used to throw an exception explicitly.

2. If we see syntax wise than throw is followed by an instance of Exception class and throws is followed by
exception class names.
For example:

throw new ArithmeticException("Arithmetic Exception");


and

throws ArithmeticException;
3. Throw keyword is used in the method body to throw an exception, while throws is used in method
signature to declare the exceptions that can occur in the statements present in the method.

Das könnte Ihnen auch gefallen