Java QB2
Java QB2
➢ class person
{
String name;
int age;
void accept(String n,int a)
{
name=n;
age=a;
}
void display()
{
System.out.println("name--->"+name);
System.out.println("age--->"+age);
}
}
class employee extends person
{
String emp_designation;
float emp_salary;
void accept_emp(String d,float s)
{
emp_designation=d;
emp_salary=s;
}
void emp_dis()
{
System.out.println("emp_designation-->"+emp_designation);
System.out.println("emp_salary-->"+emp_salary);
}
}
class single_demo
{
public static void main(String ar[])
{
employee e=new employee();
e.accept("ramesh",35);
e.display();
e.accept_emp("lecturer",35000.78f);
e.emp_dis();
}
}
2. Explain method overloading with example.
➢ Method Overloading means to define different methods with thesame name but
different parameters lists and different definitions.It is used when objects are
required to perform similar task but using different input parameters that may vary
either in number or type of arguments. Overloaded methods may have different
return types. It is a way of achieving polymorphism in java.
int add( int a, int b) // prototype 1
int add( int a , int b , int c) // prototype 2
double add( double a, double b) // prototype 3
Example:
class Sample
{
int addition(int i, int j)
{
return i + j ;
}
String addition(String s1, String s2)
{
return s1 + s2;
}
double addition(double d1, double d2)
{
return d1 + d2;
}
}
class AddOperation
{
public static void main(String args[])
{
Sample sObj = new Sample();
System.out.println(sObj.addition(1,2));
System.out.println(sObj.addition("Hello ","World"));
System.out.println(sObj.addition(1.5,2.2));
}
}
3. Define a class person with data member as Aadharno, name, Panno implement
concept of constructor overloading. Accept data for 5 object and print it.
➢ import java.util.Scanner;
@Override
public String toString() {
return "Person{" +
"aadharNo='" + aadharNo + '\'' +
", name='" + name + '\'' +
'}';
}
System.out.println(person1);
System.out.println(person2);
}
}
4. Explain method overriding with suitable example.
➢ Method Overriding in Java: If subclass (child class) has the same method as declared
in the parent class, it is known as method overriding in java. If subclass provides the
specific implementation of the method that has been provided by one of its parent
class, it is known as method overriding. Method overriding is used for runtime
polymorphism.
Example:
class Vehicle{
void run(){System.out.println("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run()
{
System.out.println("Bike is running safely");
}
public static void main(String args[]){
Bike2 obj = new Bike2();
obj.run();
}
5. Describe final method and final variable with respect to inheritance.
➢ final method: making a method final ensures that the functionality defined in this
method will never be altered in any way, ie a final method cannot be overridden.
E.g.of declaring a final method:
final void findAverage() {
//implementation
}
EXAMPLE
class A
{ final void show()
{
System.out.println(“in show of A”);
}
}
class B extends A
{
void show() // can not override because it is declared with final
{
System.out.println(“in show of B”);
}
}
final variable: the value of a final variable cannot be changed. final variable behaves
like class variables and they do not take any space on individual objects of the class.
E.g. of declaring final variable:
final int size = 100;
6. Describe use of “super” and “this” with respect to inheritance.
➢ Using inheritance, you can create a general class that defines traits common to a set
of related items. This class can then be inherited by other, more specific classes, each
adding those things that are unique to it. In the terminology of Java, a class that is
inherited is called a superclass. The class that does the inheriting is called a subclass.
Therefore, a subclass is a specialized version of a superclass.
Whenever a subclass needs to refer to its immediate superclass, it can do so by use of
the keyword super.
Super has two general forms. The first calls the super class constructor. The second is
used to access a member of the superclass that has been hidden by a member of a
subclass. super() is used to call base class constructer in derived class. Super is used
to call overridden method of base class or overridden data or evoked the overridden
data in derived class.
Newborn State
Runnable State
Running State
Blocked State
Dead State
Thread should be in any one state of above and it can be move from one state to
another by different methods and ways.
a) Newborn state: When a thread object is created it is said to be in a new born state.
When the thread is in a new born state it is not scheduled running from this state it
can be scheduled for running by start() or killed by stop(). If put in a queue it moves
to runnable state
b) Runnable State: It means that thread is ready for execution and is waiting for the
availability of the processor i.e. the thread has joined the queue and is waiting for
execution. If all threads have equal priority then they are given time slots for
execution in round robin fashion. The thread that relinquishes control joins the queue
at the end and again waits for its turn. A thread can relinquish the control to another
before its turn comes by yield().
c) Running State: It means that the processor has given its time to the thread for
execution. The thread runs until it relinquishes control on its own or it is pre-empted
by a higher priority thread.
d) Blocked state: A thread can be temporarily suspended or blocked from entering
into the runnable and running state by using either of the following thread method
suspend() : Thread can be suspended by this method. It can be rescheduled by
resume().
wait(): If a thread requires to wait until some event occurs, it can be done using wait
method and can be scheduled to run again by notify().
sleep(): We can put a thread to sleep for a specified time period using sleep(time)
where time is in ms. It reenters the runnable state as soon as period has
elapsed /over
e) Dead State: Whenever we want to stop a thread form running further we can call
its stop(). The statement causes the thread to move to a dead state. A thread will also
move to dead state automatically when it reaches to end of the method. The stop
method may be used when the premature death is required.
14.Write a program to create two thread one to print odd number only and other to
print even numbers.
➢ class EvenThread extends Thread
{
EvenThread()
{
start();
}
public void run()
{
try
{
for(inti = 0;i <= 10;i+=2)
{
System.out.println("Even Thread : "+i);
Thread.sleep(500);
}
}
catch (InterruptedExceptione){}
}
}
class OddThread implements Runnable
{
OddThread()
{
Thread t = new Thread(this);
t.start();
}
public void run()
{
try
{
for(inti = 1;i <= 10;i+=2)
{
System.out.println("Odd Thread : "+i);
Thread.sleep(1500);
}
}
catch (InterruptedExceptione){}
}
}
class Print
{
public static void main(String args[])
{
new EvenThread();
new OddThread();
}
}
15.With syntax and example explain try & catch statement.
➢ try- Program statements that you want to monitor for exceptions are contained
within a try block. If an exception occurs within the try block, it is thrown.
Syntax: try
{
// block of code to monitor for errors
}
catch- Your code can catch this exception (using catch) and handle it in some rational
manner. System-generated exceptions are automatically thrown by the Java runtime
system. A catch block immediately follows the try block. The catch block can have
one or more statements that are necessary to process the exception.
Syntax: catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
E.g.
class DemoException {
public static void main(String args[])
{
try
{
int b=8;
int c=b/0;
System.out.println("Answer="+c);
}
catch(ArithmeticException e)
{
System.out.println("Division by Zero");
}}}