You are on page 1of 6

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

Home

Java Interview Questions

Java Programs

Programming Challenges

classandinterfaceBasedInterview
Questions
1.What'sthedifferencebetweenaninterfaceandan
abstractclass?
Anabstractclassmaycontaincodeinmethodbodies,which
isnotallowedinaninterface.Withabstractclasses,you
havetoinherityourclassfromitandJavadoesnotallow
multipleinheritance.Ontheotherhand,youcanimplement
multipleinterfacesinyourclass.

Download Notes

Like

4.8k

What's new..?

1.4k

12OnlineNow

Labels
Android(2)Arrays(1) Class(2)CodingStandard(1)
Collection (3) Daemon Thread (1) Datatypes (1)
Durga Sir (1) Durgasoft (1) Encapsulation (1)

Exception Handling (3) Garbage Collection (1)


Hibernet (1) Identifiers (1) interface (2) Interview
(2)

Interview Question (18)

(28)

Java

JavaEE (10) JavaSE (13)

JDBC (1) JSP (2) Literals (1) Multi threading (3)

2.Cananinnerclassdeclaredinsideofamethod
accesslocalvariablesofthismethod?
Yes,itispossibleifthevariablesaredeclaredasfinal.

NatrajTechnology(1) Notes(8) Object(2)OOPS

Programming
Challenges (11) Programs (1) Pyramid (3)
(2) PDF (1) Plymorphism (1)

RealLifeExample (1) Reserved Words (1) SCJP (1)


Serialization (1) Servlet (4) Session (1) Spring (1)
String (1) Struts2 (2) varargs Method (1) Video

3.Youcancreateanabstractclassthatcontainsonly
abstractmethods.Ontheotherhand,youcancreate
aninterfacethatdeclaresthesamemethods.Socan
youuseabstractclassesinsteadofinterfaces?
Sometimes.Butyourclassmaybeadescendentofanother
classandinthiscasetheinterfaceisyouronlyoption
becauseJavadoesnotsupportmultipleinheritance.

Tutorial(1)

4.Whataccessleveldoyouneedtospecifyinthe
classdeclarationtoensurethatonlyclassesfromthe
samedirectorycanaccessit?
Youdonotneedtospecifyanyaccesslevel,andJavawill
useadefaultpackageaccesslevel.Aclasswithdefault
accesswillbeaccessibleonlytootherclassesthatare
declaredinthesamedirectory/package.
5.Whenyoudeclareamethodasabstractmethod?
Wedeclareamethodasabstract,Whenwewantchildclass
toimplementthebehaviorofthemethod.
6.CanIcallaabstractmethodfromanonabstract
method?
Yes,WecancallaabstractmethodfromaNonabstract
methodinaJavaabstractclass
7.WhatisthedifferencebetweenanAbstractclass
andInterfaceinJava?orcanyouexplainwhenyou
useAbstractclasses?
Abstractclassesletyoudefinesomebehaviorwhileforcing
http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

1/7

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

yoursubclassestoprovidetherest.Theseabstractclasses
willprovidethebasicfuncationalityofyourapplication,child
classwhichinheritthisclasswillprovidethefuntionalityof
theabstractmethodsinabstractclass.
Whereas,AnInterfacecanonlydeclareconstantsand
instancemethods,butcannotimplementanydefault
behavior.
Ifyouwantyourclasstoextendsomeotherclassbutatthe
sametimereusesomefeaturesoutlinedinaparent
class/interfaceInterfacesareyouronlyoptionbecause
Javadoesnotallowmultipleinheritanceandonceyou
extendanabstractclass,youcannotextendanyother
class.But,ifyouimplementaninterface,youarefreeto
extendanyotherconcreteclassasperyourwish.
Also,Interfacesareslowasitrequiresextraindirectionto
findcorrespondingmethodintheactualclass.Abstract
classesarefast.
8.Whataredifferenttypesofinnerclasses?
Innerclassesnestwithinotherclasses.Anormalclassisa
directmemberofapackage.Innerclassesareoffourtypes
1.Staticmemberclasses
2.Memberclasses
3.Localclasses
4.Anonymousclasses
9.Whatarethefield/methodaccesslevels
(specifiers)andclassaccesslevels?
Eachfieldandmethodhasanaccesslevelcorrespondingto
it:
private:accessibleonlyinthisclass
package:accessibleonlyinthispackage
protected:accessibleonlyinthispackageandinall
subclassesofthisclass
public:accessibleeverywherethisclassisavailable
Similarly,eachclasshasoneoftwopossibleaccesslevels:
package:classobjectscanonlybedeclaredand
manipulatedbycodeinthispackage
public:classobjectscanbedeclaredandmanipulatedby
codeinanypackage
10.Whatmodifiersmaybeusedwithaninnerclass
thatisamemberofanouterclass?
Anonlocalinnerclassmaybedeclaredaspublic,
protected,private,static,final,orabstract.
http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

2/7

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

11.Canananonymousclassbedeclaredas
implementinganinterfaceandextendingaclass?
Ananonymousclassmayimplementaninterfaceorextend
asuperclass,butmaynotbedeclaredtodoboth.
12.Whatmustaclassdotoimplementaninterface?
Itmustprovideimplementationtoallofthemethodsinthe
interfaceandidentifytheinterfaceinitsimplementsclause
intheclassdeclarationlineofcode.
13.Whatisthedifferencebetweenastaticanda
nonstaticinnerclass?
Anonstaticinnerclassmayhaveobjectinstancesthatare
associatedwithinstancesoftheclass'souterclass.Astatic
innerclassdoesnothaveanyobjectinstances.
14.Whencananobjectreferencebecasttoan
interfacereference?
Anobjectreferencebecasttoaninterfacereferencewhen
theobjectimplementsthereferencedinterface.
15.Ifaclassisdeclaredwithoutanyaccess
modifiers,wheremaytheclassbeaccessed?
Aclassthatisdeclaredwithoutanyaccessmodifiersissaid
tohavedefaultorpackagelevelaccess.Thismeansthat
theclasscanonlybeaccessedbyotherclassesand
interfacesthataredefinedwithinthesamepackage.
16.Whichclassshouldyouusetoobtaindesign
informationaboutanobject?
TheClassclassisusedtoobtaininformationaboutan
object'sdesign.
17.Whatmodifiersmaybeusedwithaninterface
declaration?
Aninterfacemaybedeclaredaspublicorabstract.
18.Isaclassasubclassofitself?
Yes,aclassisasubclassofitself.
19.Whatmodifierscanbeusedwithalocalinner
class?
Alocalinnerclassmaybefinalorabstract.
20.Cananabstractclassbefinal?
Anabstractclassmaynotbedeclaredasfinal.Abstractand
Finalaretwokeywordsthatcarrytotallyoppositemeanings
andtheycannotbeusedtogether.
21.Whatisthedifferencebetweenapublicanda
nonpublicclass?
http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

3/7

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

Apublicclassmaybeaccessedoutsideofitspackage.A
nonpublicclassmaynotbeaccessedoutsideofits
package.
22.Whatmodifiersmaybeusedwithatoplevel
class?
Atoplevelclassmaybepublic,abstract,orfinal.
23.WhataretheObjectandClassclassesusedfor?
TheObjectclassisthehighestlevelclassintheJavaclass
hierarchy.TheClassclassisusedtorepresenttheclasses
andinterfacesthatareloadedbyaJavaprogram.
24.Canyoumakeaninstanceofabstractclass
Noyoucannotcreateaninstanceofabstractclass.Ifyou
usenewkeywordtoinstantiateanabstractclass,youwill
getacompilationerror.
25.Describewhathappenswhenanobjectiscreated
inJava
Severalthingshappeninaparticularordertoensurethe
objectiscreatedproperly:
1.Memoryisallocatedfromheaptoholdallinstance
variablesandimplementationspecificdataofthe
objectanditssuperclasses.Implemenationspecificdata
includespointerstoclassandmethoddata.
2.Theinstancevariablesoftheobjectsareinitializedto
theirdefaultvalues.
3.Theconstructorforthemostderivedclassisinvoked.
Thefirstthingaconstructordoesiscallthe
consctructorforitssuperclasses.Thisprocesscontinues
untiltheconstrcutorforjava.lang.Objectiscalled,
asjava.lang.Objectisthebaseclassforallobjectsinjava.
4.Beforethebodyoftheconstructorisexecuted,all
instancevariableinitializersandinitializationblocksare
executed.Thenthebodyoftheconstructorisexecuted.
Thus,theconstructorforthebaseclasscompletesfirstand
constructorforthemostderivedclasscompleteslast.
26.WhatisthepurposeofSystemClass
Thepurposeofthesystemclassistoprovidetheaccessto
theSystemreources
27.WhatisinstanceOfoperatorusedfor
Itisusedtocheckifanobjectcanbecastintoaspecific
typewithoutthrowingClasscastexception
28.Whyweshouldnothaveinstancevariableinan
http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

4/7

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

interface?
SincealldatafieldsandmethodsinanInterfacearepublic
bydefault,whenweimplementthatinterfaceinourclass,
wehavepublicmembersinourclassandthisclasswill
exposethesedatamembersandthisisviolationof
encapsulationasnowthedataisnotsecure
29.Whatisasingletonclass
Asingletonisanobjectthatcannotbeinstantiatedmore
thanonce.Therestrictiononthesingletonisthattherecan
beonlyoneinstanceofasingletoncreatedbytheJava
VirtualMachine(JVM)bypreventdirectinstantiationwe
canensurethatdevelopersdon'tcreateasecondcopy.We
accomplishthisbydeclaringtheconstructorprivateand
havingapublicstaticinstancevariableoftheclass'stype
thatcanbeaccessedusingagetInstance()methodinthe
class.
30.Cananabstractclasshavefinalmethod
Yes,youcanhaveafinalmethodinanAbstractclass.
31.Canafinalclasshaveanabstractmethod
No,aFinalclasscannothaveanAbstractmethod.
32.Whendoesthecompilerinsistthattheclassmust
beabstract
Thecompilerinsiststhatyourclassbemadeabstractunder
thefollowingcircumstances:
1.Ifoneormoremethodsoftheclassareabstract.
2.Ifclassinheritsoneormoreabstractmethodsfromthe
parentabstractclassandnoimplementationisprovidedfor
thatmethod
3.Ifclassimplementsaninterfaceandprovidesno
implementationforsomemethods
33.Howisabstractclassdifferentfromfinalclass
Abstractclassmustbesubclassedandanimplementation
hastobeprovidedbythechildclasswhereasfinalclass
cannotbesubclassed
34.Whatisaninnerclass
Aninnerclassissameasanyotherclass,justthat,is
declaredinsidesomeotherclass
35.Howwillyoureferencetheinnerclass
Toreferenceaninnerclassyouwillhavetousethe
followingsyntax:OuterClass$InnerClass
36.Canobjectsthatareinstancesofinnerclass
accessthemembersoftheouterclass
Yestheycanaccessthemembersoftheouterclass
http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

5/7

17/12/2015

JavaInterviewGuide:classandinterfaceBasedInterviewQuestions

37.Caninnerclassesbestatic
Yesinnerclassescanbestatic,buttheycannotaccessthe
nonstaticdataoftheouterclasses,thoughtheycanaccess
thestaticdata.
38.Cananinnerclassbedefinedinsideamethod
Yesitcanbedefinedinsideamethodanditcanaccessdata
oftheenclosingmethodsoraformalparameterifitisfinal
39.Whatisananonymousclass
Someclassesdefinedinsideamethoddonotneedaname,
suchclassesarecalledanonymousclasses.
40.Whatareaccessmodifiers
Thesepublic,protectedandprivate,thesecanbeappliedto
class,variables,constructorsandmethods.Butifyoudon't
specifyanaccessmodifierthenitisconsideredasFriendly.
Theydeterminetheaccessibilityorvisibilityoftheentities
towhichtheyareapplied.
41.Canprotectedorfriendlyfeaturesbeaccessed
fromdifferentpackages
Nowhenfeaturesarefriendlyorprotectedtheycanbe
accessedfromalltheclassesinthatpackagebutnotfrom
classesinanotherpackage.
42.Howcanyouaccessprotectedfeaturesfrom
anotherpackage
Youcanaccessprotectedfeaturesfromotherclassesby
subclassingthethatclassinanotherpackage,butthis
cannotbedoneforfriendlyfeatures.
Like

Share

PostedbyUmeshKushwaha

Recommend this on Google


Labels:Class,interface,InterviewQuestion,Java

http://java4732.blogspot.in/2015/09/classandinterfacebasedinterview.html

6/7

You might also like