amit


{ City } noida
< Country > india
* Profession *
User No # 39861
Total Questions Posted # 1
Total Answers Posted # 33

Total Answers Posted for My Questions # 2
Total Views for My Questions # 26001

Users Marked my Answers as Correct # 308
Users Marked my Answers as Wrong # 54
Answers / { amit }

Question { 13350 }

Hi Friends, can u give me Real Time example for interface
and abstract class.(With Banking Example)


Answer

hi pushpa don't go with the book think that
i'm going to tell you what is the imagine
abstract class Human
{
public abstact void eat();
}


thereis osme class is Amit ,Apoorva,and Imarana
they both in hierarchy ofthe Human class theu all extend
this class
class Amit extends Human
{
public void eat()
{
Systm.out.println("eat biryani")
}
}
classApoorva extends Human
{
pulic void eat())
{
Sysetm.out.println("eat food");
}
}
class Imrana extends Human
{
public void eat()
{
System.out.println("eat teh Dosa");
}
}
}
someans to say that theare is same hierarchy means Human
so you want toextends it just for inner world Human
2)
second pointis srise that what you think about the outer
world imagine in outre world there is another class Lizard
it aldso want ot use the functionality eat
that this particular class can extends this i don't think
so and you too
so this is the outer world class this is not in the
hierrachy of the Human so think
i am goin to make the interface for that touse this for
alll world class object whther they are in the hierrrachy
of any superr class are not

interface Nature
{
void eat();
}

class Lizard implements Mature
{

public void eat()
{
System.out.println("eaa mosquito");
}
}

and i'm too going to use this functionality so tell me
isthsii wherer the class in the same hoerrarchy are not

class Amit implements Nature
{

public void eat()
{
System/oput.println("eat any thing");
}
}
}
so this the possible to us this wiytout going any hoerarchy
so if the inner world you should to use abstrac class
but in to use functionality intyhe outer world or inner
world u should to use interface.
i think you got this
amitsing2008@@gmail.com
amit09mca(scjp1.5)

Is This Answer Correct ?    13 Yes 2 No

Question { TCS, 12178 }

What will happen inside init() in servlet. my interviewer
asked servlet lifecycle. i said "once servlet is loaded in
to memory init() will be called which performs servlet
initialization " . Again interview asked what values will be
initialized . what is difference between init() and
init(ServletConfig config).


Answer

when the first request goes to the container then first
container create the object and the constructor execute
then init() method called
note: thatGenericServlet has an
init(ServletConfig conf ) method require by the Servlet
interface. If you don't override init(ServletConfig conf),
then the GenericServlet method will be called - it in turn
calls the init() method.
so if you use the override the init(ServletConfig con)
so you should to call the super.init(ServletConfig con)
which in turn call the lnlt()
2)what will happen inside the init() its use to intialize
teh parametr which you give in a web.xml
so it intialize the custom parmeter and other paranetr too
so its just for intialization
because wedon't do intialization through constructor
bracuse the object of servlet will create by webcontainer
and execution of constructor too so we use thr init method
for initializtion because we are not able to use new for
creating a object in sevlet
thanks amit singh

amitsing2008@gmail.com

Is This Answer Correct ?    8 Yes 1 No


Question { 24879 }

What is difference between abstract class & final class


Answer

1)abstrat class is just a incomplete class mean to say you
only declare a method when some class extends this abstart
class then it adds some facility in this class when you
define the method body those inherited from the abstract
class.
2)but the fianl class you cn't able to add the new
functionality to this class because extnd the class its not
just mean to make child clss but to means to facilitate
this class with some aditive coding .so means to say that
its complete class you don't able to add new functionality
in this class and you never extends this class .
thanks amit singh

amitsing2008@gmail.com

Is This Answer Correct ?    29 Yes 3 No

Question { Infosys, 19017 }

Given:
1. package test;
2.
3. class Target {
4. public String name = “hello”;
5. }
What can directly access and change the value of the
variable name?
1 any class
2 only the Target class
3 any class in the test package
4 any class that extends Target


Answer

first i want to see the visibility of class itis
default acees
the class is default acess so this class is visible in a
same package
so then the member in it public so
1)so the class in the same package will acess it
2)the class in the same package which extends this class
can acess it
not any other class in the whole world or subclass in other
package.
thanks amit singh
amitsing2008@gmail.com

Is This Answer Correct ?    17 Yes 0 No

Question { 13883 }

could you run the java program without main method?


Answer

yes you can run java peogram without main
we do it through the static block ,because its execute
before the main method,
so when the class is loaded it will execute before main
but whrn the static block excute and after exiting it jvm
search for main when it will not fou8nd it then it throw
some exception so for this we use the
System.exit();
class Moon
{
static
{
System.out.println("hello i am visible without main");
}
System.exit();
}
Thanks amit singh

amitsing2008@gmail.com

Is This Answer Correct ?    36 Yes 8 No

Question { 6806 }

"we cannot create an object of interface but we can create
a variable of it".diacuss the statement with the help of
an example.


Answer

yes we can create a variable in inerface
and i will be constant
mans to say that hey will be static and final
inerface Xyz
{
public static final i = 12;
public static final j = 20;
}
class Amit implements Xyz
{
psvm(String [] args)
{
Xyz k = new Amit();
System.out.println("value of var1 " + k.i);
Sysem.out.prinln("value ofd var 2 " + k.j);
}
}
Thanks ami singh
(amitsing2008@gmail.com)

Is This Answer Correct ?    11 Yes 0 No

Question { CTS, 22935 }

java.lang.ClassNotFoundException:oracle.jdbc.driver.OracleDr
iver? I get this error at run time.I used oracle10G. I set
CLASS PATH:C:\oraclexe\app\oracle\product\10.2.0
\server\jdbc\lib\ojdbc14.jar;
I write JDBC PROGRAM like
import java.sql.*;

class Example
{
public static void main(String args[])
{
try
{
Class.forName
("oracle.jdbc.driver.OracleDriver");
System.out.println("Driver Loaded");
Connection con=DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:xe","system","salmas");
System.out.println("Driver Connected");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from
emp");
while(rs.next())
{
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
st.close();
con.close();
}

catch(Exception e)
{
System.out.println(e);
}

finally
{
System.out.println("it's
finally block executed");
}

}
}


Answer

have you set the enoviornment variable if not then beacuse
of this the driver can not be load
because which you path
Class.forName
("oracle.jdbc.driver.OracleDriver");
in Class.forName("String" )
compiler search it out in bin/lib of jre or jre bin/lib/ext
so please check it out because of it this excption commonly
come
because if some one used Mysql and i did in past i got thsi
exception because it didn't set the Classpath for connection.jar
so do it getridof this thing
thanks
amitsing2008@gmail.com
amit09mca

Is This Answer Correct ?    2 Yes 0 No

Question { 4625 }

please send code example of inner classes?


Answer

here is the code of inner classes in which you will get all
inner classes in a one code


//interface for anonymous class

interface Aishwarya
{
public static final int lm = 10;
void intmeth();
}



//class for cretaing anonymous class
class Amitabh
{

void isit()
{
System.out.println("in superclass Amitabh");
}

}



//outer class for the regular inner class
class Outer
{
private static int i;
static int j;
Outer()
{

}


Outer(int i,int j)
{

this.i = i;
this.j = j;

}



public int add()
{

return i+j;

}


private int substract()
{
return (j-i);
}



public static void show2()
{
System.out.println("inner show in static method");
Outer.Inner j = new Outer().new Inner();
j.show();
}


public void show1()
{
Inner f = new Inner();
f.show();
}


//just to define how argument define anonymous class work
public void argument(Aishwarya a1)
{
Aishwarya a2 = a1;
System.out.println("value of interface constant " + a2.lm);
a2.intmeth();
}



//simple regular inner class
class Inner
{

public void show()
{
System.out.println("show add in inner" + add());
System.out.println("show substarct in inner" + substract
());
System.out.println("value of i of outer" + i);
System.out.println("value of j of outer" + j);
System.out.println("value of i of outer through this" +
Outer.this.i);
System.out.println("value of j of oter through this" +
Outer.this.j);

}
}

//end of the inner class




//static nested class is not real inner class
static class Bye
{
void statmeth()
{
System.out.println("hi im static only acess the statis var
method of outer class");
System.out.println("don't need for me to create the outer
instance");
System.out.println("because i'm Outerclass type");
System.out.println("value of i from outer which is
static :" + i);

//don't do it

//System.out.println("value " + add());
}
}//end of static class





//anonymous class
Amitabh a = new Amitabh(){
public void isit()
{
System.out.println("isit
abs bay borned in this");

}
public void isntpoosible()
{
System.out.println("isnot
in superclass");
}


};




public void did()
{
a.isit();
//a.isntpoosible();
}


//main method of outer
public static void main(String []args)
{

Outer o1 = new Outer(4,5);
o1.did();

//here is how to define anonymous class in argument

o1.argument(new Aishwarya(){
public void intmeth()
{
System.out.println("hi
this is interface method implement");
}


});

System.out.println("in main " + o1.add());
System.out.println("substract in main" + o1.substract());
Outer.Inner i1 = new Outer().new Inner();
System.out.println("show in main");
i1.show();



// create the object of amit
Amit a112 = new Amit();
a112.hi();
a112.dosome();

//create the object of static class
Bye bbb = new Bye();
bbb.statmeth();
}
}
// end of outer






//just for manupulating the code
class Amit
{

public int j = 45;
public static int l = 10;
private String s1 = "amit";
public String s2 = "sumit";



public static void hi()
{

System.out.println("hi this is amit");
Outer.Inner j1 = new Outer().new Inner();

j1.show();

Outer o22 = new Outer();

System.out.println("outer add " + o22.add());
System.out.println("is there any requirement of method
local class so see it");
System.out.println("in static context you only able to
acess the static var from the enclose class");
System.out.println("don't get then do some differ");




//local method class in static method

class LocalInner1
{


public void seeall1()
{
System.out.println("print :" + l);
//System.out.println("string concat : " + s1+s2);
System.out.println("string concat :" + new Amit().s1 +
new Amit().s2);
//System.out.println("strig concat" + this.s1 + this.s2);


}


}
LocalInner1 i1 = new LocalInner1();
i1.seeall1();

}


public void dosome()
{

//String z = "i'm in mood to be access if you can";


final String z = "yes finallly access";
System.out.println("you only apply on mlc just abstract and
final if not get so do diifer ");
System.out.println("in Voidsome");


//local method inner class in instance method
class LocalInner2
{
public void seeall2()
{
System.out.println(j);
System.out.println("access the static :" + l);
System.out.println("acess private and public :" +
s1+s2+z);
}

}
LocalInner2 i2 = new LocalInner2();
i2.seeall2();

}
}




run this code you will get all aspect in this code in which
class what restricted what no .wvery inner classes you will
get in it
don't arg without run the code
thanks amit singh
amitsing2008@gmail.com
amit09mca

Is This Answer Correct ?    6 Yes 1 No

Question { 8042 }

what is object deep copy and shallow copy and why it is
required?


Answer

hi i'm amit
now i've one hour right now to tell you what is the cloning
imagine you create a object Like (Amit a = new Amit();)
and the you ggive asingn the paticular ref variable in the
other refernce variable
like( Amit b = a;) is this is cloning not
Why: because the refernce variable only to get the bitwise
patter the way to get the onject where it is
so its not the cloning.
so what you must to do if you want to create a clone of
thsis object then what to do you should to implements the
cloneable interface for that class you want to clone.
so implemnts the cloneable interface.
and override the clone method of the Object class

/*imagine a girl is so birliant i want to this girl
i want to this girl but how so i'm going to create the
clone of
this girl,beacuse i'm not able to approach directly
beacuse i don't want any reference to get the way to meet
the
original apoorv beacuse i'm so shy
*/

SHALLOW COPY:

class Apoorva implements Cloneable
{
private String s;
private int age;
public Apoorva(String s1,int age)
{
s = s1;
this.age = age;
}

public String getthis()
{
return s + " " + age;
}

public Object clone() throws CloneNotSupportedException
{

return super.clone();

}
}

public class Amitsingh
{
public static void main(String []args)
{
Apoorva a = new Apoorva("she is birliant",21);
Apoorva b=null;

try{
b = (Apoorva)a.clone();
}
catch(Exception e)
{
}
System.out.println("hi apporv " + b.getthis() + " i mean
it " + a.getthis());
}
}

/*so what you get Object.clone() omnly do the simple
cloning
its only create a new object and then copy the value of
field
means it copy all field from one object to another.
but what when you have a meber of any class type then
if you write code which above mention will only copy the
refence of the
the class type which will be member in this particular
class
so that why itis called a shallow copy io field to field
copy.
note bydefault it does fiel by field copy only primitive
then the new object will create and the referance to the
object will return
but if there is any Class type (forget that i will use
above a
String class type because it is immutable so don't matter)

which is immutable then object refrensce to those member
will copy
both refrence refer to the same object.

so "MEANS TO SAY THAT IN SHALOOW COPY STRING AND
PRIMITIVE TYPES ITS NOT PROBLEM BUT
WHEN MUTABLE THEN WHAT TO DO IS THE SAHLLOWCOPY WILL WORK

ANSWER IS NO, IMAGINE YOU HAVE A CLASS A IN WHICH
A MEMBER OF ANOTHER CLASS B TYPE AND YOU WANT TO
CLONE THE CLASS AND WANT TO CHANGE THE VALUE OF
ANY MEMBERFIELD WHICH IS IN THE CLASS B ITS POSSIBLE
THROUGH TO CLONE THE OBJECT OF CLASS A USING SHALLOW COPY"

not its not possible but why answer is only the reference
variable will copy of class type not the whole class so the
after cloning using sahllowopy
you have the refernce of same object bso
it will not work for you because you want to change in
member field value in the object of class B
which will create during the cloning.but the class B object
not cloned during the shallow copy.
"so if you changed that you are in dream you think you
cloned the Class B
and you changed its state so the original object state will
also changed
so you don't want to that then the deepcloning comes in a
light.
*/

so now the turns to understand the deep copy


DEEP COPY:

class Itsection implements Cloneable
{
private int passoutyear;
private String Floor;
public Itsection()
{
}
public Object clone() throws CloneNotSupportedException
{
return super.clone();
}

public void setall(int x,String s)
{
passoutyear=x;
Floor = s;
}

public String gets()
{
return " " + Floor + " ";
}
public String getint()
{
return " " + passoutyear + " ";
}
}



// class in which other class type member to be cloned

class Apoorva implements Cloneable
{
private String s;
private int age;
private Itsection it; //class type must implement the
cloneable interface for deep copy
//otherwise we didn’t able
to call the protected method Object clone()
Apoorva(String s1,int age)
{
this.s = s1;
this.age = age;
it = new Itsection();
}
public String getthis()
{
return s + " " + age;
}

public Object clone()
{
Apoorva a1=null;
try
{
a1 = (Apoorva)super.clone();
a1.it = (Itsection)a1.it.clone();

}

catch(CloneNotSupportedException e)
{
}
return a1;
}

public static void main(String []args)
{
Apoorva a2 = new Apoorva("she is birliant",21);
a2.it.setall(2009,"second floor 2nd buiding");
System.out.println("original object" + a2.it.gets() +
a2.it.getint());


Apoorva a3 =(Apoorva)a2.clone();
a3.it.setall(2010,"trd floor 2nd building");
System.out.println("clone object " + a3.it.gets() +
a3.it.getint());
System.out.println("hi apporva " + a2.getthis() + " i
mean it " + a3.getthis());

}
}

thanks AMIT
SINGH
amitsing2008@gmail.com

don't arg wthout run the code
amit09mca

Question:- to clone the thread object how itis poosible or
not??????????
ans: ---
to clone innerclasses it is possible or
not?????????????/////
bye

Is This Answer Correct ?    7 Yes 0 No

Question { 22338 }

what is the use of Clonable,and serializable interface?


Answer

serialzable and cloneable interface both are marker
interface
Cloneable interface:
if any class implements this interface .so this interface
its just a indicator or marker that a particular class
going touse clone() of the Object class to field tio field
copy of the instance ofthe particular class.


Serializable:
its also the interface .if any class implements it so means
that this particular class object convert in byte and going
to save in a locakl disk
and also its useful to send object in a Byteform to the
network.
and at the another network it will convert from byte to
Object of particular type.

Is This Answer Correct ?    48 Yes 5 No

Question { 21575 }

servlet has not any main method in it how does it work by
JVM?


Answer

no man first the constructor will run then the init()
method
which are used to intialization step likee servletconfig
then after service() method for all thread which will
create for the only on instance of servlet
what the mean is it people think that always the instance
of servlet always create when the requst for the particular
servlet but here only the one instance of servlet and the
the service method will excecute for distinct request
ao don't ever think that

note: that the constructor is default constructor you are
no able to call this constructor
so first the constructor will run
this servlet object it just a noraml object
but after the servletcofig it will be the servlet object
so the constructor will run
thanks
amitsing2008@gmail.com
amit09mca(scjp1.5 and many more)

Is This Answer Correct ?    3 Yes 5 No

Question { 6222 }

all are saying java doesn't support multiple inheritance but
by default Object class is super class for all the user
defined classes and we can extend atmost one class so each
class can extend more than one class so java supports
multiple inheritance?i am confused with this,pls any one
explain me.


Answer

logic is simple differnet
imagine you create a class
Class A
{
A()
{
super()
}
}
ya this class extends Object class implicitly
and it will definitely call the Object class constructor

but if you extends the Class A explicitly
then what will happen

class A0
{
AO()
{
super();
}
}
class A extends AO
{
A()
{
super();
}
}
}

so what happen because the Object class Replace by this
immediate super class which is in your Apis which you build
in your project

now the class AO is responsible to implicit extends Object
class
so what is the mean that Object inheritence is passeddown
through your Immediate super class
so don't say that implicit inheritence creete the mulitiple
inherritence
"because in java is not allowed beacaus so ambiguity
when one class extends Two classes and both have same method
signature
then which method will call
Amit singh
amitsing2008@gmail.com
amit09mca

Is This Answer Correct ?    13 Yes 0 No

Question { 4677 }

what is class.forname() and how it will be useful ?


Answer

it is useful when you don't know about the class as compile
time you must to use it you pass the class name as a string
and
its a part of reflection api
but you used it when you connect through db
and many more you should do when you don't know the class
at compile time
and also above which mention by pradip right
amitsingh200@gmail.com
amit09mca(scjp 1.5 and manymore)

Is This Answer Correct ?    5 Yes 0 No

Question { 12382 }

what is the difference between String s="hello"; and String
s=new String("hello");?


Answer

not the difference both are the string
String s = "hello"
its aliteral string it will be in a pool
imagine that when you cretae the anothre string
String s1 = "hello"
thn ther is not new Styrin object wil cretae its will pick
from the pool where the literal strin g hello will presnet
so s>
----------"hello"

s1>
both pointing the same string
2)String s = new String("hello");
there are two string will create frist the litreal "hello"
then the other through the new String("hello"); which is
point through referenece s is pointed so there is always
create a new String which is pointed by the reference s
its not going to pick through the pool
amitsing2008@gmail.com
amit09mca(scjp 1.5 and manymore)

Is This Answer Correct ?    3 Yes 2 No

Question { 6508 }

why the sleep method does not leave the lock and wait method
leave the lock


Answer

every class has an object lock but when you used
synchronized block or synchronized method
means to say the when you use synchronized keyword then the
object lock come in an existence without you don't get the
lock on particular object you
must know that wait()
1) what is the purpose of when you call wait on the object
which lock you get when you use synchronized block or method
then particular thread immediate leave the
lock so means to say that wait must to use when you get the
lock on the particular object without

2)its your turns to think that there is any mendotary
to call Thread.sleep() method in synchronized block
so whe there is no relation b/t sleep and Monitor(i'm saying
lock on the object)
why youthink it like that why the sleep() doesn't leave the
lock and wait() leave the lock

3) ther is no realtion between object lock and sleep
amit singh
amitsing2008@gmail.com

Is This Answer Correct ?    0 Yes 2 No

Prev    1    [2]   3    Next