Presentation is loading. Please wait.

Presentation is loading. Please wait.

4. The procedural extension Java c of Java I pslab 김윤경.

Similar presentations


Presentation on theme: "4. The procedural extension Java c of Java I pslab 김윤경."— Presentation transcript:

1 4. The procedural extension Java c of Java I pslab 김윤경

2 4.1 Static semantics of Java c 4.1.1 Syntax of Java c Fig.4.1 Syntax of a Java class class A { constructor declarations field declarations method declarations static initializers }

3 Constraint 4.1.1  The type B must be a class and I 1, …, I n must be different interfaces.  The class B is not final.  If A is final, then it not abstract.  If A=Object, there is no extends clause. Inheritance relation : ⊂ h -If A ⊂ h B : A is a subclass of B or B is a superclass of A -If A ⊂ h I : A implements I or I is superinterface of A -If I ⊂ h J : I is a subinterface of J or J is a superinterface of I Constraint 4.1.2 The inheritance relation ⊂ h must be acyclic. A ⊂ h A is not allowed that.

4 Fig.4.2 Syntax of a java Interface interface I { constant declarations abstract method declarations } Constraint 4.1.3 1.The types J 1, …, J n are different interfaces. 2.The interface I is implicity abstract.

5 Lemma 4.1.1. Let A,B and C be classes.  A ⊂ h Object(every class is a subclass of Object).  If A ⊂ d B and A ⊂ d C, then B =C.  If A ⊂ h B and A ⊂ h C, then B ⊂ h C or C ⊂ h B. Lemma 4.1.2. If A is an interface and A ⊂ h B, then B is an interface, too. Definition 4.1.1. A package is a collection classes and interfaces. Definition 4.1.2. A Java c program is a set of packages. Package statement form package PackageName;

6 Fig.4.3. Syntax of Java c Exp := … |Field |Class.Field|Invk Asgn:= … |Field=Exp|Class.Field=Exp Exps:=Exp 1, …, Exp n Invk := Meth(Exps)|Class.Meth(Exps) Stm:= … |Invk;|return Exp;|return; Phrase := … |static Block

7 4.1.2 Class members-constructor,field, method declarations and static initializers Field declarations syntax. A field ; Constraint : if the field is final, then a variable initializer must appear in the declaration of field. Method declarations syntax. A method (B 1 loc 1, …, B n loc n ) body Constraint :The name A is a type or the keyword void. It is called declared return type of the method. If C/msig is abstract, then C is abstrct. Static initializer syntax. Static block It is executed when the class is initialized.

8 4.1.3. Interface members Constant declarations syntax. A field =exp; Constraint : The field is implicity public, static and final. Abstract method declarations syntax. A meth(B 1 loc 1,.., B n loc n ) Constraint : The method is implicity public and abstract(and not static)

9 4.1.4. Accessibility, visibility, hiding and overriding 1.Accessibility  If x if private in C, then C/x is accesible from class C only.  If x if default access in C, then C/x is accessible from all classes in the same package. Outside of the package, the element C/x is not accessible.  If x if public in C, then C/x is accessible from anywhere.  If x if protected in C, then C/x is accessible from the same package or outside of the package from subclasses of C. 2. Visibility If A ⊂ h B, C/x is visible in B, x is not declared in A and C/x is accessible from A, then C/s is visible in A. ex) class A{ public static int I=2; private static int j=3; public static int k=4;} class B{ public static int I=4;}

10 3.overriding Definition. A method A/msig is said to directly override a method C/msig, if there is a class or interface B such that  A ⊂ d B,  C/msig is visible in B and  C/msig is accessible from A Constraint  The return type of msig in A is the same as in C.  Method msig is not final in C.  Method msig is static in A if, and only if, it is static in C.  Method msig is not private in A.  If msig is public in C, then msig is public in A.  If msig is protected in C, then msig is public or protected in A.

11 Constraint. If two methods B/msig and C/msig with the same signature are bothe visible in A, 1.msig has the same return type in B and C, 2.If msig is public in B, then msig is public in C. 3.If msig is not static in B, then msig is not static in C. Ex) error! interface I{ int m(int I); } class B { int m(int I){ return I*I; }} abstract class A extends B implements I {}

12 Definition. A class A implements a method msig, if there exists a class B such that  A ⊂ h B and msig is declared in B,  B/msig is visible in A,  msig is not abstract in B ex) interface I { int m(int I); } class B{ public int m(int I){ =>class A implements return I*I; method m(int) } class A extends B implements I{}

13 4.1.5. Static type checking Two possibilites to access a static field in A:  B.field=>identifier field is a field of B or a field of one of B ’ s superclasses or superinterfaces which is visible in B. C.field at compile time : C/field is unique with the property that C/field is visible in B and accessible from A.  field=> identifier field is a field of A or a field of one of A ’ s superclasses or superinterfaces which is visible in A. C.field at compile time : C/field is unique with the property that C/field is visible in A and if field is static in C.

14 Table 4.1.Type constraints for Java c Table 4.2. Type constraints after introduction of primitive type casts.  C.field T(  ) is the declared type of field in C.  ( C.field =  exp) T(  ) is the declared type of field in C, field is not final in C, T(  ) ⊂ T(  )  C.msig(exps) T(  ) is the declared return type of msig in class C. return  exp; If the position  is in the body of a method with return type A, then T(  ) ⊆ A.  ( C.field =  exp)Let D be the declared type of field in C. If D is primitive,then T(  ) =D= T(  )  C.msig(  1 exp 1, …,  n exp n) If msig = meth(B 1, …, B n ) and B i is a primitive type, then T(  i ) = B i. return  exp; If the position  is in the body of a method with a primitive return type A,then T(  ) =A.

15 4.1.6.Overloaded methods Definition. A method C/meth(A 1, …, A n ) is more specific than a method D/meth (B 1, …, B n ), if C ⊆ h D and A i ⊆ B i for i=1, ….,n. Ex) The most specific method is chosen during compile time. class A { static void m (double d){} static void m(long l){} static void test(int i){ m(i); }} Ex) class A { static void m(int x, long y){} static void m(long x, int y){ m(0,0);//Reference to m is ambiguous. }}

16 4.1.7. Vocabulary of Java c super : ruturn direct superclass body : Class/Msig -> Block data ClassState= Linked | InProgress | Initialized | Unusable classState : Class -> ClassState Initialized(c)= classState(c) Initialized ∨ classState(c) =Inprogress globals : Class/Field->Val meth : Class/Msig(currently executed method) Type Frame =(Class/Msign, Phrase,Pos,Locals)//the frame of the invoking method frames:Frame * Data Abr=Break (Lab)|Continue(Lab)|Return|Return(Val)

17 4.2 Transfer rules for Java c Fig. 4.4 Execution of Java c expressions execJavaExp c= case context(pos) of c.f->if initialized(c) then yield(globals(c/f)) else initialize(c) c.f =  exp->pos:=  c.f= ► val-> if initialized(c) then globals(c/f):=val yieldUp(val) else initialize(c) c.m  (exps)->pos:=  c.m ► (vals)->if initialized(c) then invokeMethod(up(pos),c/m,vals) ()->yield([]) (  1 exp 1, …,  n exp n ) -> pos:=  1 (  1 val 1, …, ► val n ) -> yiedUp([val 1, …, val n ]) (  1 val 1, …, ► val n,,  i+1 exp i+1, …,  n exp n )->pos:=  i+1

18 Fig.4.5 Execution of Java c statements execJavaStm c = case contest(pos) of static  stm->let c=classNm(meth) if c= Object ∨ initialized(super(c)) then pos:=  else initialized(super(c)) static  Return->yieldUp(Return) return  exp; ->pos:=  return ► val;->yieldUp(Return(val)) return;->yield(Return) lab: ► Return ->yieldUp(Return) lab: ► Return(val)-> yieldUp(Return(val)) Return -> if pos=firstPos ∧ ┐null(frames) then exitMethod(Norm) Return(val)->if pos=firstPos ∧ ┐null(frames) then exitMethod(val) ► Norm;->yieldUp(Norm)


Download ppt "4. The procedural extension Java c of Java I pslab 김윤경."

Similar presentations


Ads by Google