important points.doc
TRANSCRIPT
-
8/14/2019 Important Points.doc
1/13
Declarations and Access Control
Modifiers fall into two categories:
Access modifiers:public,protected,private
Nonaccess modifiers (includingstrictfp,final, andabstract)
A class can be declared with only publicor defaultaccess. (Additionally Strictfp,abstract, final)
he other two access control levels protected,privatedon!t ma"e sense for aclass
#our access controls (which means all three modifiers $ default) wor" for mostmethod and variable declarations
Class Access (Visibility)
Default Access: A class with default access can be seen only by classeswithin the same package.
Note: If class A and class B are in different packages, and class A has a defaultaccess, class B wont be able to create an instance of class A, or e!en declare a!ariable or a return type of class A"
#o these two things to $ake it work, place class A, class B in the sa$e package(or) declare class A with public access $odifier"
Exam Watch : %hen you see a &uestion with co$ple' logic, be sure to look atthe access $odifiers first" hat way, if you spot an access !iolation (for e'a$ple,a class n package A trying to access a default class in package B), youll knowthe code wont co$pile so you dont ha!e to bother working through the logic" Itsnot as if, you know, you dont ha!e anything better to do with your ti$e whiletaking the e'a$" ust choose the *Co$pilation fails+ answer and oo$ on to thene't &uestion"
Public Access: A class declaration with the public keyword gives allclasses from all packages access to the public class. visible to all classesin all packages.
-ther (Nonaccess) Class .odifiers: %an modify a class declaration using the "eyword final,abstract, or strictfp.
hese modifiers are in additionto whatever access control is on the class. &oucan!t always mi' non abstract modifiers.
never! ever! ever mark a class as both final and abstract. "#ell me why$%
strictfpis a "eyword and can be used to modify a class or a method, but never avariable.
Mar"ing a class as strictfpmeans that any method code in the class will conformto the *+ standard rules for floating points.
Final%lasses -hen used in a class declaration, the final "eyword means theclass can!t be sub classed. No other class can ever e'tend (inherit from) a finalclass.
#inalcan be used to modify a class or a method or a variable.
Abstract%lasses: An abstract class can never be instantiated. t is to bee'tended (subclassed).
Abstractcan be used to modify a class, a method but never a variable.
Exam Watch: /ook for &uestions with a $ethod declaration that ends with ase$icolon, rather than curly braces" If the $ethod is in a class0as opposed toan interface0then both the $ethod and the class $ust be $arked abstract" 1ou$ight get a &uestion that asks how you could fi' a code sa$ple that includes a
-
8/14/2019 Important Points.doc
2/13
$ethod ending in a se$icolon, but without an abstract $odifier on the class or$ethod" In that case, you could either $ark the $ethod and class abstract, orre$o!e the abstract $odifier fro$ the $ethod" -h, and if you change a $ethodfro$ abstract to non abstract, dont forget to change the se$icolon at the end ofthe $ethod declaration into a curly brace pair2
&emember:3!en a single $ethod is abstract, the whole class $ust be declared
abstract" Can put non abstract $ethods in the abstract class"
Exam Watch: 1ou cant $ark a class as both abstract and final" hey ha!enearly opposite $eanings" An abstract class $ust be subclassed, whereas afinal class $ust not be subclassed" If you see this co$bination of abstract andfinal $odifiers, used for a class or $ethod declaration, the code will not co$pile"
.ethod and Variable #eclarations and .odifiers
Methods and instance (nonlocal) variables are collectively "nown as members.%an modify a member with both access and nonaccess modifiers.
Member Access: public, protected, private, default.
Exam Watch: Its crucial that you know access control inside and out for the
e'a$" here will be &uite a few &uestions with access control playing a role"4o$e &uestions test se!eral concepts of access control at the sa$e ti$e, so notknowing one s$all part of access control could blow an entire &uestion"
'uestion: %hat does it $ean for code in one class to ha!e access to a $e$berof another class5 he $e$ber in class A should be !isible to class B
&ou need to understand two different access issues: /. -hether method code inone class can access a member of another class. . (0sing reference). 1.-hethera subclass can inherit a member of its superclass (use e'tend and access it byinheritance iff the member should be visible)
f a subclass inherits a member, the subclass hasthe member.
Exam Watch:1ou need to know the effect of different co$binations of class and$e$ber access (such as a default class with a public !ariable)" o figure this
out, first look at the access le!el of the class" If the class itself will not be !isibletoanother class, then none of the $e$bers will be either, e!en if the $e$ber isdeclared public" -nce you!e confir$ed that the class is !isible, then it $akessense to look at access le!els on indi!idual $e$bers"
Public: -hen a method or variable member is declared public, it means all otherclass regardless of the pac"age can access the member.(assuming the classitself is visible).
f a member of its superclass is declared public, the subclass inherits thatmember regardless of whether both classes are in the same pac"age.
n the subclass, you can invo"e the super class method without reference.(-ithout the dot operator). 0sing reference also wont hurt.
he reference thisalways refers to the currently e'ecuting ob2ect. this referenceis implicit.
Private: Members mar"ed private can!t be accessed by code in any class otherthan the class in which the private member is declared.
A private member is invisible to any code outside the member(s own class.
Question:-hat about a subclass that tries to inherit a private member of itssuperclass3 A subclass can!t inherit it.
-
8/14/2019 Important Points.doc
3/13
A subclass can!t see or use or even thin" about private members of its superclass.
&ou can however have a matching method in the subclass with the same methodname in the super class. t is simply a method that happens to have the samemethod name as the private method in the super class. 4verriding rules does notapply.
On the ob:n practice it!s nearly always best to "eep all variables private orprotected. f variables needs to be changed programmers should use publicaccessor methods.
Question: %an a private method be overridden by a subclass3 No. Since thesubclass cannot inherit the private method, it therefore cannot override themethod. (4verriding depends on inheritance).
Default !embers: A default member can be accessed only if the classaccessing the member belongs to the same pac"age.
Protected !embers: A protected member can be accessed (throughinheritance) by a subclass even if the subclass is in the different pac"age.
Note: &ou cannot access protected member using ob2ect reference. he
subclass cannot use the dot operator on the superclass reference to access theprotected member.
Question 5ut what does it mean for a subclassoutside the pac"age to haveaccess (visibility to a superclass (parent) member3 t means the subclass inheritsthe member.
#he subclass can only see the protected member through inheritance. #or asubclass outside the pac"age, the protected member can be accessed onlythrough inheritance.
Question: -hat does a protected member loo" li"e to other classes trying to usethe subclassoutsidethepac"age to get the subclass! inherited protectedsuperclass member3
Say for e': class A is in 6ac"age A, class 5 is in 6ac"age 5. class 5 is
accessing the protected member ' in class A. -hat happens if some class sayclass % in the same pac"age 5 has a reference to class 5 wants to access themember variable ' (protected member in class A).7ow does that protected member behave once the subclass has inherited it38oes it maintain its protected status, such that classes in the %hild!s pac"agecan see it3)o.-nce the subclass6outside6the package inherits the protected $e$ber, that$e$ber (as inherited by the subclass) becomes privateto any code outside thesubclass"#he bottom line: when a subclass6outside6the6package inherits a protected$e$ber, the $e$ber is essentially pri!ate inside the subclass, such that onlythe subclass own code can access it"
Question: -hat about default access for two classes in the same pac"age3 twor"s fine. 9emember that default members are visible only to the subclassesare in the same pac"age as the superclass.
/ocal Variables and Access .odifiers
Question: %an access modifiers be applied to local variables3 "o#
Exam Watch:here is ne!er a case where an access $odifier can be applied toa local !ariable, so watch out code like the following"
class #oo void doStuff()
-
8/14/2019 Important Points.doc
4/13
private int ' ; *ed "eyword indicates that themethod can be accessed by only one thread at a time.
Synchroni>ed"eyword can be applied only to methods.
Synchroni>edmodifier can be paired with any of the four access controllevels. (private, public, default, protected).
Synchroni>edcan be paired with final but not with abstract
"ative !ethods: he native "eyword indicates that a method is implementedin a platform dependent language such as %.
Native can never be combined with abstract. Native can be applied only to methods, not to classes , variables.
'trictfp !ethods:Strictfp forces floating point operations to adhere *+standard.
Strictfp can modify a class or nonabstract method declaration and that aariable can never be declared strictfp.
ariable 8eclarations
(nstance )ariables: nstance variables are defined inside the class butoutside the method. 4nlu initiali>ed when the class is instantiated.
nstance variables can use any of the four access levels. %an be mar"ed#inal, transient, abstract, strictfp, native, synchroni>ed.
*ocal variables:Bocal variables are declared within the method. Means
variables is not 2ust initiali>ed within the method but also declared within themethod. t starts its life inside the method, it!s also destroyed when themethod has completed.
*ocal variables are al+a%s on the stac, not the heap# -et clarified#P$:./
Bocal variable declarations can!t use most of the modifiers such as public,transient, volatile, abstract, static.
Bocal variables can be mar"ed final#
5efore using the local variable it should be initiali>ed.
Bocal variables don!t get default values.
Bocal variables can!t be referenced outside the method in which it is declared.
t is possible to declare the local variable with the same name as an instancevariable. hat!s "nown as shado+in$.he following (but wrong) code is trying to set an instance variable!s valueusing a parameter:class #oo int si>e ; 1*e(int si>e) si>e ; si>e< CC 333 which si>e e?uals which si>e333=
-
8/14/2019 Important Points.doc
6/13
-
8/14/2019 Important Points.doc
7/13
class #oo int ' ; F