1.
What is thread? What
is multi-threading? How to create a thread in a program? (WBUT 2012)
Thread: A thread, in the context
of Java, is the path followed when executing a program. All Java programs have
at least one thread, known as the main thread, which is created by the JVM at
the program’s start, when the main() method is invoked with the main thread. In
Java, implementing an interface and extending a class accomplish creating a
thread. Every Java thread is created and controlled by the java.lang.Thread
class.
Multi-threading: Java provides built in support for
multithreaded programming. A multithreaded program contains two or more parts
that can run concurrently. Each part of such a program is called a thread, and
each thread defines a separate path of execution. Thus, multithreading is a
specialized form of multitasking.
Creating a thread in two ways:
User can create a thread by
instantiating of type Thread. Java defines two ways in which this can be
accomplished:
i.
Implement the Runnable interface
ii.
Extend the Thread class
Implement the Runnable interface |
Extend the Thread class |
Runnable abstract a
unit of executable code. We can construct a thread on any object that
implements Runnable. To implement it, a class need only a single method
run(). This function defines the code to create the thread. After a new thread
is created it will not start running, until it will call the start(), which
is declared within thread.
Ex:
class newthread implements Runnable
{ Thread t;
newthread()
{
t=new
Thread(this,"New Thread");
System.out.println(this);
t.start();
}
public
void run()
{
try{
for(int
i=5;i>0;i--)
{
System.out.println("Child
Thread"+i);
Thread.sleep(500);
}
}catch(Exception
e)
{}
}
}
class extendthread1
{
public
static void main(String args[])
{
new
newthread();
try{
for(int
i=5;i>0;i--)
{
System.out.println("main
Thread"+i);
Thread.sleep(1000);
}
}catch(Exception
e)
{}
}
}
|
Another way to create
thread is to create a new class that extends Thread and then to create an
instance of that class. The extending class must override the run() method,
which is the entry point of the thread. After a new thread is created it will
not start running, until it will call the start(), which is declared within
thread.
Ex:
class newthread extends Thread
{
newthread()
{
super("New
Thread");
System.out.println(this);
start();
}
public
void run()
{
try{
for(int
i=5;i>0;i--)
{
System.out.println("Child
Thread"+i);
Thread.sleep(500);
}
}catch(Exception
e)
{}
}
}
class extendthread
{
public
static void main(String args[])
{
new
newthread();
try{
for(int
i=5;i>0;i--)
{
System.out.println("main
Thread"+i);
Thread.sleep(1000);
}
}catch(Exception
e)
{}
}
}
|
2.
What is the difference
between Swing and AWT components?
AWT
|
Swing
|
AWT stands for Abstract windows
toolkit.
|
Swing is also called as JFC’s
(Java Foundation classes).
|
AWT components are called
Heavyweight component.
|
Swings are called light weight
component because swing components sits on the top of AWT components and do
the work.
|
AWT components require java.awt
package.
|
Swing components require
javax.swing package.
|
AWT components are platform
dependent.
|
Swing components are made in
purely java and they are platform independent.
|
This feature is not supported in
AWT.
|
We can have different look and
feel in Swing.
|
These feature is not available
in AWT.
|
Swing has many advanced
features like JTabel, Jtabbed pane which is not available in AWT. Also. Swing
components are called "lightweight" because they do not require a
native OS object to implement their functionality. JDialog and JFrame are
heavyweight, because they do have a peer. So components like JButton,
JTextArea, etc., are lightweight because they do not have an OS peer.
|
With AWT, you have 21
"peers" (one for each control and one for the dialog itself). A
"peer" is a widget provided by the operating system, such as a
button object or an entry field object.
|
With Swing, you would have only
one peer, the operating system's window object. All of the buttons, entry
fields, etc. are drawn by the Swing package on the drawing surface provided
by the window object. This is the reason that Swing has more code. It has to
draw the button or other control and implement its behavior instead of
relying on the host operating system to perform those functions.
|
AWT is a thin layer of code on
top of the OS.
|
Swing is much larger. Swing
also has very much richer functionality.
|
Using AWT, you have to
implement a lot of things yourself.
|
Swing has them built in.
|
3.
Write a program to convert given no.
of days into months and days.
import java.util.Scanner;
class DayMonth{
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
int num =
scan.nextInt();
int days = num%30;
int month = num/30;
System.out.println(num+" days = "+month+"
Month and "+days+" days");
}
}
4.
Write the difference between method
overriding and method overloading.
Sol:
Difference
between method overloading and method overriding
Overloading
|
Overriding
|
Having same method name with different Signatures.
|
Methods name and signatures must be same.
|
Overloading is the concept of compile time polymorphism
|
Overriding is the concept of runtime polymorphism
|
Two functions having same name and return type, but with
different type and/or number of arguments is called as Overloading
|
When a function of base class is re-defined in the derived
class called as Overriding
|
It doesn't need inheritance.
|
It needs inheritance.
|
Method can have different data types
|
Method should have same data type.
|
Method can be different access specifies
|
Method should be public.
|
Overloading
|
overriding
|
Class overload
{
int max(int
a,int b)
{
return(a>b?a:b);
}
float max(float a,float b)
{
return(a>b?a:b);
}
}
class ex
{
public
static void main(String args[])
{
int
a=5,b=6,c;.
c=max(a,b);
float
a1=5.5,b1=6.6,c1;.
c1=max(a1,b1);
}
}
|
Class A
{
void show()
{
System.out.println("From
A class");
}
}
class B extends A
{void show()
{
System.out.println("From
B class");
}
}
class ex
{
public
static void main(String args[])
{
B
b1=new B();
b1.show();//
override class A's version
}
}
|
5.
What is meant by "Abstract
Interface"?
Firstly, an
interface is abstract. That means you cannot have any implementation in an
interface.
All the methods declared in an interface are abstract methods or signatures of the methods.
All the methods declared in an interface are abstract methods or signatures of the methods.
Abstract class's can have a constructor, but you
cannot access it through the object, since you cannot instantiate abstract class.
To access the constructor create a sub class and extend the abstract class
which is having the constructor.
Example
public abstract class AbstractExample {
public AbstractExample(){
System.out.println("In AbstractExample()");
}
}
public abstract class AbstractExample {
public AbstractExample(){
System.out.println("In AbstractExample()");
}
}
public class Test extends AbstractExample{
public static void main(String args[]){
Test obj=new Test();
}
}
public static void main(String args[]){
Test obj=new Test();
}
}
If interface & abstract class have
same methods and those methods contain no implementation, which one would you
prefer?
Obviously one should ideally go for an interface,
as we can only extend one class. Implementing an interface for a class is very
much effective rather than extending an abstract class because we can extend
some other useful class for this subclass
An abstract class cannot be instantiated. Only its
subclasses can be instantiated. A class that has one or more abstract methods
must be declared abstract. A subclass that does not provide an implementation
for its inherited abstract methods must also be declared abstract. You indicate
that a class is abstract with the abstract keyword like this:
public abstract class AbstractClass
Abstract classes may contain abstract methods. A
method declared abstract is not actually implemented in the class. It exists
only to be overridden in subclasses. Abstract methods may only be included in
abstract classes. However, an abstract class is not required to have any
abstract methods, though most of them do. Each subclass of an abstract class
must override the abstract methods of its superclasses
or itself be declared abstract. Only the method’s prototype is provided in the class definition. Also, a final method can not be abstract and vice versa. Methods specified in an interface are implicitly abstract.
. It has no body. For example,
or itself be declared abstract. Only the method’s prototype is provided in the class definition. Also, a final method can not be abstract and vice versa. Methods specified in an interface are implicitly abstract.
. It has no body. For example,
public abstract float getInfo()
No comments:
Post a Comment