• Keine Ergebnisse gefunden

Exercise4The equals Method Exercise3IdentityvsEqualityofObjects Exercise2JavaProgramming Exercise1Lectureconcepts PracticeSheet1(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2012)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise4The equals Method Exercise3IdentityvsEqualityofObjects Exercise2JavaProgramming Exercise1Lectureconcepts PracticeSheet1(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2012)"

Copied!
2
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Prof. Dr. A. Poetzsch-Heffter Dipl.-Inform. Kathrin Geilmann

University of Kaiserslautern Department of Computer Science Software Technology Group

Advanced Aspects of Object-Oriented Programming (SS 2012) Practice Sheet 1 (Hints and Comments)

Exercise 1 Lecture concepts

Encapsulation&Information Hidingis used to restrict the access to an object’s components. Objects hide their internal structure by using get- and set-methods, making methods and attributes private etc. The reason for the usage of it is, that the implementation can change but it should have no effects to the user of a component or object.

Polymorphism&SubtypingPolymorphism is the ability of a type A, to appear as a type B in another context. Subtyping is a form of polymorphism, where the polymorph types are related to each other by the subtype relation and a notion of substitutability, meaning that a subtype can be used everywhere, where the the supertype can be used.

Declarative&Model-based SpecificationsDeclarative specifications just specify which properties hold, they do not say anything about how the holding is achieved. In model-based specifications a model of the implementation is used and operations, properties etc are specified in that model. The implementation then has to be a concretization of the model.

Functional&Non-functional PropertiesFunctional properties describe the action of a program, where as non-functional properties describe anything else. e.g. ressources which can be used, ...

Exercise 2 Java Programming

Straightforward implementation.

Exercise 3 Identity vs Equality of Objects

a) String-Objects in Java are interned. That is, string literals create a string object, but they create the same string object for the same literal. (JLS 3.10.5). This result is, thata1anda2reference the same object, whereasb1and b2both create new string objects at runtime. The two strings have the same value (namely ”B”) i.e. they are equal, but they have not the same identity.

b) Explicitly internalize the stringsb1andb2.

c) The strings have to be immutable, otherwise a change on a shared (interned) string via a variable x would change the value of other variables as well.

Exercise 4 The equals Method

a) In the followingd1andd2reference instances of the classDateandd3andd4reference instances of the class NamedDate. We have to check calls to equals with all combinations of types.

• Reflexivityd1.equals(d1)ok;d3.equals(d3)ok; the given implementation is reflexiv.

• Symmetryd1.equals(d2)⇒d2.equals(d1)ok d3.equals(d4)⇒d4.equals(d3)ok

d1.equals(d3)⇒d3.equals(d1)ok d3.equals(d1)⇒d1.equals(d3)ok The given implementation is symmetric.

• TransitivityCounterexample:

d3 = new NamedDate(2010, 5, 7, ’’A’’);

d4 = new NamedDate(2010, 5, 7, ’’B’’);

d1 = new Date(2010, 5, 7);

(2)

d3.equals(d1)andd1.equals(d4)are true butd3.equals(d4); is false The given implementation is not transitive.

• Consistencyok

• Non-Nullok

b) The contract is fulfilled except for the call with null (fix it, by adding a check) but this solution breaks Liskov’s substitution principle. The principle states that whenever a property is true for an object of type T it should be true for objects of type S as well, where S is a subtype of T.

In other words, whenever a type T is used it should be safe, i.e. leading to the same results, to use a subtype of T.

This is not the case with the given implementation. Consider the following code:

HashMap<Date, String> m = new HashMap<Date, String>();

m.put(new NamedDate(2010,4,5, ’’A’’), ’’rainy’’);

// the following calls may give different results (depending on // which object equals is called and which is used as parameter), // where the substitution principle expects the same

m.containsKey(new Date(2010,4,5));

m.containsKey(new NamedDate(2010,4,5,’’A’’));

Breaking the substitution principle often results in unexpected behavior and should be avoided if possible.

2

Referenzen

ÄHNLICHE DOKUMENTE

writeObject writes the state of the object o, the states of the objects in the object graph reachable by o and the reference structure to a newly created file with the given

XMLEncoder / XMLDecoder: Stores only the public parts of objects into an xml representation. It has similar restrictions and features as our solution. It was implemented for

The main purpose of inheritance is the reuse of code in di ff erent contexts. It makes implementing subtypes easier, because the subtype implementation only needs to describe the di

b) Checked exceptions guarantee at compile time, that they are handled by the program. Thus they do not lead to the abortion of the program. See JLS 11.2 for more about compile

This code snippet would break the guarantee of the type system, that the casts that are inserted by the compiler during the type erasure will never fail. Furthermore, one could not

Undesired dynamic alias: Store an alias to an object in the local variable tmp; hideAll components and make the aliased one visible again. Capturing occurs if a reference, which

a) Model fields form a model state, which can be di ff erent from the implemented state. The model state may be better suited for specifying the behavior of an object, because it can

b) With synchronous communication methods are executed completely before the control returns to the caller. That means that the order of execution of methods corresponds to the order