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

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
Lecture 5: Interfaces.
Java Programming 2 Dr. Priti Srinivas Sajja Introductory concepts of java programming as specified in PGDCA 203:Object Technology, S P University.
Object Oriented Programming
INHERITANCE BASICS Reusability is achieved by INHERITANCE
OOP: Inheritance By: Lamiaa Said.
1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 The Design of Class Mechanism for MOBY. 2 Earlier Software designers have to choose between the following schemes for the development Earlier Software.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Intro to OOP with Java, C. Thomas Wu
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
The object-oriented extension Java o of Java c (Java and the Java Virtual Machine ch. 5) PSLab 문세원.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Java and the Java Virtual Machine 3. The imperative Core Java I of Java Pslab 오민경.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
Classes, Interfaces and Packages
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Inheritance.
Modern Programming Tools And Techniques-I
OOP: Encapsulation &Abstraction
Final and Abstract Classes
Inheritance and Polymorphism
COP 3331 Object Oriented Analysis and Design Chapter 5 – Classes and Inheritance Jean Muhammad.
Overloading and Constructors
Interface.
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Java Programming Language
Interfaces.
Java Inheritance.
Chapter 14 Abstract Classes and Interfaces
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

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

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

Constraint  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 The inheritance relation ⊂ h must be acyclic. A ⊂ h A is not allowed that.

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

Lemma 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 If A is an interface and A ⊂ h B, then B is an interface, too. Definition A package is a collection classes and interfaces. Definition A Java c program is a set of packages. Package statement form package PackageName;

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

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.

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)

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;}

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.

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 {}

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{}

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.

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.

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. }}

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)

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

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)