Вы находитесь на странице: 1из 2

Q.1.Howyoucandefineanobject?Howitsrelatedtorealworldanditbehaves?WithExample.

Ans:Thisquestionisoftenaskedforalljavainterviewfrom18yearexperiencecandidate.Asweknowobjectisa
realworldentity.Ithasthreebasicpropertyi.e.State,BehaviorandIdentity.Simplywecansayitsablueprintof
class.Anythingaroundyouiscalledanobject.Butyes,ithastheabovethreepropertybywhichwecandefineone
object.
State:whattheobjectshave.Doghaveaname,age,color,etc.Itisimplementedbyvariablesinjava.
Behavior:whattheobjectsdo.Dogcanbark,bite,run,etc.Itisimplementedbymethodsinjava.
Identity:whatmakesthemunique,Doghaveidunique,whichisunique.(thisisimportantwhenimplementingthe
equalsmethod,todetermineiftheobjectsaredifferentornot).Everyobjecthasdifferenthashcode.
ExampleSuppose,therearemanydogs.But,youcan'tsay,thatisanobject.Youneedtotakeonedogwhich
hasnamedick,age8,colorwhiteanditmusthaveauniqueidentification.Andnowwecansaydickisadog
object.Andthisdickcanbark,biteandrun.Thesearethebehaviorthatobjectcando,andinitsimplementedby
methods.
Q.2.Howmanycontructor&methodsareinObjectclass?
Ans:ThereareonlyoneconstructorandninemethodsinObjectclass.Itsavailableinsidejava.langpackage.
Constructor:
publicObject()

Methods:

clone()Thiscreateandreturncopyofthesameobject.
equals()Thisisusedtocomparesomeotherobjectisequaltothisornot.
toString()Convertandreturnastringrepresentation.
finalize()Thisiscalledbygarbagecollectorwhentherearenomoreactivereferencetothatobjectexist.
getClass()Thisreturnstheruntimeclassofthisobject.
hashCode()Returnsanhashcodeforthisobjectanditsunique.
notify()Wakesupasinglethreadthatiswaitingonthisobject'smonitor.
notifyAll()Wakesupallthreadthatiswaitingonthisobject'smonitor.
wait()Thiswillmakeobjecttowait,untilanyotherthreadnotifythis.

Q.3.Whywait()andnotify()methodisinsideObjectclassinsteadofThreadclass?

Ans:ThisisaFAQforanyjavainterview.Because,wait()andnotify()methodworkswithmultithreaded
environment.ThisisinsideObjectclassbecause,wearedoingthemonitoringworkoveranObject.Wheneverwe

arecallingthesemethods,wearecallingoverobject,notonthread.
Q.4.HowmanywayswecancreateobjectinJava?
Ans:Therearebasicfourwayforcreatingobjectinjavaasbelow:
Byusingnewkeyword
Objectobj=newObject()
Byusingclass.forName()
DogdogObj=(Dog)class.forName("jdeveloperguide.lab.Dog").newtInstance()

Byusingdeserialization
ObjectInputStreaminStream=newObjectInputStream(anInputStream);
DogdogObject=(Dog)inStream.readObject();

Byusingclone()
DogdogObj=newDog()
DogdogObj2=dogObj.clone()//Itmustimplementclonnableinterface.
Q.5.HowJVMallocatesobjectinmemory?Whatisheapandstackmemory?
Ans:Normallyobjectsareallocatedonheapmemory.Heapmemoryisadynamicmemorywhichincreaseand
decreasethesizeonruntime.Allprimitivetypes,methodsareallocatedonstackmemory.Allobjecttypeand
referencetypesareallocatedonheaparea.Everythreadcreatesastack(callstack)andwhenthreadcompletesthe
stackisalsoreleasethereservememory.Thestackisfastercomparetoheap.

Вам также может понравиться