Classes .


47 views
Uploaded on:
Category: Sales / Marketing
Description
CS 884 (Prasad). Java Classes. 2. . Class Declaration. Fields (Type Field initializer)Class variables (static)Instance variablesMethods ( Signature Code)Class systems (static)Instance methodsStatic Initializers ( Code to introduce class vars)Constructors (Code to instate case vars)Initialization pieces.
Transcripts
Slide 1

´╗┐Classes Fields, techniques, and constructors Inheritance Java Classes

Slide 2

Class Declaration Fields (Type + Field initializer) Class factors ( static ) Instance factors Methods ( Signature + Code) Class strategies ( static ) Instance strategies Static Initializers ( Code to introduce class vars) Constructors (Code to instate example vars) Initialization squares Java Classes

Slide 3

Method Declaration Formal parameter names are unmistakable and can\'t be concealed . Real contentions are passed by esteem . Technique body is a square . Techniques can\'t be settled. (Common) Recursion is allowed. The body of a static (class) technique can allude just to static individuals. Java Classes

Slide 4

Subclass class Object is the foundation of class chain of command . Subclass Members ( open/ensured/default? ) individuals acquired from direct super-class. individuals acquired from direct super-interface. individuals unequivocally proclaimed in the subclass. Constructors and static initializers are not acquired. Class with private constructors not instantiated . Default constructor for class C. C( ) { super ( ); } Java Classes

Slide 5

Alternatives: when degrees cover ... Extents of basic name x of individuals E1 and E2 cover Overloading : Both E1 and E2 accessible. On the off chance that both are techniques, utilize x if resolvable by mark. In the event that both are consistent fields, utilize qualified names. On the off chance that degree x/E2 incorporated into extension x/E1 , then Hiding : x alludes to E2 , however E1 exists and is available by a qualified name or super. x. Abrogating : x alludes to E2 , and E1 does not exist. ( E1 in parent reusable utilizing super. x. ) Java Classes

Slide 6

Field name clashes Fields pronounced in a class must have particular names. Fields pronounced in a class shroud fields acquired (from the super-class or super-interfaces) with a similar straightforward name. Occurrence (static) field can conceal static (occasion) field. Fields acquired with same straightforward name must be alluded to utilizing unambiguous names . Introduction: last fields, static fields, occurrence fields (in each catergory: printed arrange ) Java Classes

Slide 7

Design Issues class C { class S C amplifies C { TC x ; TSC x ; ...} If field redefinition were prohibited , including a "clashing" field to C can nullify SC . Hence, while upgrading a class, a software engineer would have needed to investigate every one of the subclasses before picking a name! Java Classes

Slide 8

If superseding of fields were allowed, the techniques in C could break , regardless of the possibility that TSC were to be a subtype of TC . This is on the grounds that sort accuracy of assignments can be damaged. class C { TC x; TC y; TC p() { x = y; y = x; return x; } class SC amplifies C { TSC x; } new SC().p(); ERROR: " x = y/y = x " unless TC has same sort as element !!! Java Classes

Slide 9

In Java, sort TSC and TC are irrelevant, and subclass field x shrouds class field x . In dialects, for example, C++, expansion of a field in a parent class requires recompilation of subclasses since capacity necessity for a subclass occurrence has changed. In Ada, customers of a bundle should be recompiled notwithstanding when the private - segment is altered on the grounds that capacity necessities may have changed. (Customer code however stays unaltered.) In Modula-2, customers are not recompiled in light of the fact that exclusive reference sorts are utilized as a part of this circumstance and capacity necessities are settled from the earlier. Java Classes

Slide 10

Solving Fragile Superclass Problem The Java compiler passes typical references to individuals, while the mediator performs last name determination at connection time. The capacity format of items is not controlled by the compiler, but rather is conceded to run time and dictated by the mediator. Upgraded classes with new occasion factors or strategies can be connected in without influencing existing code. Java Classes

Slide 11

Method name clashes Method (Constructor) Signature name of the technique number, sort, and request of formal parameters Overloading : A class may not pronounce two strategies with a similar mark. A proclaimed occurrence (resp. static) strategy abrogates (resp. stows away ) an acquired example (resp. static) technique with a similar mark. Order time Error: if a case (static) technique has same signature as an acquired static (occurrence) strategy . Java Classes

Slide 12

Hidden/overidden individuals Compile-time Error: Two strategies ( pronounced or covered up or abrogated ) with same mark however extraordinary return sorts (or void ). Rearranges over-burden determination . (Cf. Ada) S func(int x) {} neither over-burdens nor abrogates T func(int x) {} if S =/= T. Shrouded fields/(static) techniques can be gotten to utilizing a name or a cast to super-class sort or utilizing super . Abrogated (occurrence) strategies and parent constructors can be gotten to just utilizing super . Java Classes

Slide 13

Inheritance A class acquires from its immediate super-class and direct super-interfaces every one of the fields and techniques (regardless of whether conceptual or not) of the guardians that are open to the code (e.g., ensured , however not private ) and are neither abrogated nor covered up by a statement in the class. Java Classes

Slide 14

A class can acquire at least two fields with a similar name either from two interfaces or from its super-class and an interface. Increase acquired fields might be disambiguated utilizing qualified names. Shrouded fields and private fields are actualized by a subclass example, that is, has capacity designated for it. Covered up/abrogated individuals might be gotten to in the subclass utilizing qualified names or super . Private individuals pronounced in a class are not available in a subclass. Java Classes

Slide 15

Overriding and Dynamic Binding class C { void p() {} } class S develops C { void p() {} } C x = new S(); x.p() Illegal Overloading and Overriding class C { void p() {} float p() {} } class S augments C { int p() {} } Java Classes

Slide 16

S x = new S(); C y = x; (x.a == 77) (y.a == 84) (( (C) x).a == 84) (x.p() == y.p()) ((( C )x).p() == y.p()) (x.q() != y.q()) (( (C) x).q() == y.q()) class C { int a = 84; static int q() {} int p() {...} } class S augments C { int a = 77; static int q() { C.q(); ...} int p() {... super .p(); ...} Hiding and Overriding Java Classes

Slide 17

Dynamic Binding When an occasion strategy is summoned through a protest reference, the real class of the question oversees which execution is utilized. (Interestingly, when a field or a static technique is gotten to, the proclaimed kind of the reference is utilized. Java Classes

Slide 18

Binding and Type System Java Classes

Slide 19

Dynamic Binding in Java class P { open void f(P p) { System.out.println("f(P) in P. "); } class C develops P { open void f(P p) { System.out.println("f(P) in C. "); } open void f(C cp) { System.out.println("f(C) in C. "); } Java Classes

Slide 20

class DynamicBinding { open static void main(String[] args) { P pp = new P(); C cc = new C(); P pc = cc; pp.f(pp); pp.f(cc); pc.f(pp); pc.f(cc); cc.f(pp); cc.f(cc); } Java Classes

Slide 21

class P { open void f(P p){} } class C amplifies P { open void f(P p){} open void f(C c){} } P pp = new P(); C cc = new C(); P pc = cc; pp.f(pp); pp.f(cc); pc.f(pp); pc.f(cc); cc.f(pp); cc.f(cc); Abbreviated Example Java Classes

Slide 22

pp.f(pp); pp.f(cc); pc.f(pp); pc.f(cc); cc.f(pp); cc.f(cc); >=P f(P) {} >=P f(P) {} (pressure) >=P f(P) {} >=P f(P) {} (compulsion) >= C f(P) {} >= C f(C) {} Compile-time versus Run-time (official) P f(P) {} P f(P) {} (intimidation) C f(P) {} C f(P) {} (compulsion) C f(P) {} C f(C) {} Java Classes

Slide 23

Static authoritative of Signatures, Dynamic official of Code class P { open void f(P p){System.out.println("f(P) in P. "); } class C amplifies P { open void f(P p){System.out.println("f(P) in C. ");} open void f(C c){System.out.println("f(C) in C. "); } class DYNAMIC2 { open static void main(String[] args) { P pp = new P(); C cc = new C(); P pc = cc; pp.f(cc); pc.f(pp); pc.f(cc); cc.f(pc); cc.f(cc); ((P) cc).f(cc); ((P) cc).f((P) cc); } Java Classes

Slide 24

Static official of Signatures, Dynamic authoritative of Code class P { open void f(P p){System.out.println("f(P) in P. "); } open void f(C c){System.out.println("f(C) in P. "); } class C augments P { open void f(P p){System.out.println("f(P) in C. ");} } class DYNAMIC { open static void main(String[] args) { P pp = new P(); C cc = new C(); P pc = cc; pp.f(cc); pc.f(pp); pc.f(cc); cc.f(pc); cc.f(cc);/Error : < Java 1.4 Fine : > Java 5 ((P) cc).f(cc); ((P) cc).f((P) cc); } Java Classes

Slide 25

Keywords : Abstract , Final last field = (steady presentation) ( requires initializer ). - last ref sort field = ( settled question , state variable) . last class strategy = ( subclass can\'t stow away ) . last occurrence strategy = ( subclass can\'t abrogate ) . code for conclusive strategies can be in-lined . (no dynamic restricting important) unique class technique = accumulate time blunder. dynamic example strategy = ( no usage) . Java Classes

Slide 26

(cont

Recommended
View more...