Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Markus Reitz
Technische Universität Kaiserslautern Fachbereich Informatik AG Softwaretechnik
Practice Sheet 10: Fortgeschrittene Aspekte objektorientierter Programmierung (SS 2008)
Date of Issue: 17. Juni 2008 Deadline: 23. Juni 2008 (until 10 a.m. as PDF via E-Mail)
Exercise 1 Behavioural Subtyping I
Check the following source code fragments with respect to violation or fulfillment of theBehavioural Subtypeproperty as introduced in the lecture. Explain the reason for violation or fulfillment.
p u b l i c c l a s s A {
/ /@ i n v a r i a n t v a l u e>=0;
p r o t e c t e d i n t v a l u e ;
/∗@
@ r e q u i r e s a>=0;
@∗/
p u b l i c v o i d s e t (i n t a ) { v a l u e=a ; }
/∗@
@ e n s u r e s \r e s u l t>=0;
@∗/
p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
p u b l i c c l a s s B e x t e n d s A {
/∗@
@ r e q u i r e s a>=10;
@∗/
p u b l i c v o i d s e t L a r g e (i n t a ) { v a l u e=a ; } }
p u b l i c c l a s s C { p r o t e c t e d i n t v a l u e ;
/∗@
@ r e q u i r e s a>0;
@∗/
p u b l i c v o i d s e t (i n t a ) { v a l u e=a ; }
/∗@
@ e n s u r e s \r e s u l t>0;
@∗/
p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
p u b l i c c l a s s D e x t e n d s C {
/∗@
@ a l s o
@ r e q u i r e s a>10;
@∗/
@ O v e r r i d e p u b l i c v o i d s e t (i n t a ) { v a l u e=a ; }
/∗@
@ a l s o
@ e n s u r e s \r e s u l t>10;
@∗/
@ O v e r r i d e p u b l i c i n t g e t ( ) { r e t u r n v a l u e ; } }
p u b l i c a b s t r a c t c l a s s E { / /@ p u b l i c m o d e l i n t c o u n t ;
/∗@
@ e n s u r e s c o u n t==0;
@∗/
p u b l i c a b s t r a c t v o i d r e s e t ( ) ;
/∗@
@ e n s u r e s c o u n t>\o l d ( c o u n t ) ;
@∗/
p u b l i c a b s t r a c t v o i d i n c r e m e n t ( ) ; }
p u b l i c c l a s s F e x t e n d s E {
/ /@ p r i v a t e r e p r e s e n t s c o u n t <− v a l u e ; p r i v a t e i n t v a l u e ;
p u b l i c v o i d r e s e t ( ) { v a l u e=0 ; }
/∗@
@ a l s o
@ e n s u r e s c o u n t==\o l d ( c o u n t )+1 ;
@∗/
p u b l i c v o i d i n c r e m e n t ( ) { v a l u e+=1; } }
Exercise 2 Behavioural Subtyping II
a) LetPsuperbe the complete precondition of the supertype andPsubbe the subtype’s additional preconditions for a methodm.
1. What is the complete precondition for methodm?
2. What is the complete postcondition form, ifQsuperdenotes the complete postcondition of the supertype and Qsubdenotes the subtype’s additional postconditions?
b) Show that the previous results satisfy the conditions 1. pre[Supertype]⇒pre[Subtype]
2. post[Subtype]⇒post[Supertype]
discussed in the lecture.
c) Does JML support strengthening of a method’s complete precondition in a subtype?