• Keine Ergebnisse gefunden

Exercise3Structuralvs.NominalTyping Exercise2RequiredandProvidedInterfaces Exercise1IntrospectionandReflection PracticeSheet3(HintsandComments) AdvancedAspectsofObject-OrientedProgramming(SS2012)

N/A
N/A
Protected

Academic year: 2022

Aktie "Exercise3Structuralvs.NominalTyping Exercise2RequiredandProvidedInterfaces Exercise1IntrospectionandReflection PracticeSheet3(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 3 (Hints and Comments)

Exercise 1 Introspection and Reflection

a) See enclosed source. Note, that the source does not consider enums and does not try to get access to private fields (which is sometimes possible, depending on the security manager). Furthermore, it does not handle arrays, neither as fields nor as toplevel object.

b) It’s not, due to value of private fields, static fields, etc.

c) Serialization API: Pro: can store complete objects, including private fields, can be tuned for each class by im- plementing special methods. Cons: needs the marker interface serializable implemented by all objects it should serialize, writes binary representations, that are dependent on the source, thus code changes can make the stored files unreadable.

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 JavaBeans, but can be used for any objects, that fulfill certain properties, like having a default constructor, having getter and setter methods for fields.

Java Persistence API : Stores objects into a database. The interfaces are included in the Java API, the imple- mentation has to be provided by third party code. Popular implementations are hibernate and eclipselink. The programmer defines a mapping between an object and a relational datastore and then the implementation provides him with classes and objects that allow to retrieve objects in a very flexible way. It is even possible to query the store like a database.

Our solution: pro: can be used for any object, the only constraint is, that it has to provide a default constructor, human readable file format. Cons: does not store everything.

Exercise 2 Required and Provided Interfaces

a) Provided interface for classes

• outside java.io: all public methods, constructors, fields, inner classes, etc ofObjectOutputStreamand its superclasses.

• inside java.io: like for classes outside java.io+protected fields, methods and constructors+fields, methods, constructors with the default modifier.

• subclasses of ObjectOutputStream inside java.iolike all other classes inside java.io

• subclasses of ObjectOutputStream outside java.io like all other classes outside java.io+protected field, methods, constructors, etc.

b) To find the required interface of a class, you have to look at the code (documentation, specification) to find out, which objects you can/ have to pass to the class in order to work with it. In this case, the OOS requires an OutputStream (parameter to the constructor).

Exercise 3 Structural vs. Nominal Typing

a) Advantages:

• Very flexible, if an object should be used somewhere, it just has to have the required set of features.

• Very easy to extend with new types, just factor out commonalities.

The advantages are at the same time the disadvantages, because the programmer has no direct control over the subtype relation, such that unrelated classes can be subtypes of each other, if by chance they have same methods and instance variables.

(2)

b) Employeeis a subtype ofStudent, so they can register for exams, which is certainly not intended. This problem is an example for subtyping by accident, which is the price to pay for the flexibility of the type system.

c) IfBis a subtype ofA, objects of typeBcan be assigned to variables of typeA. Thus in the second line we can writenewB(). Due to the dynamic binding of methods, the system will call the implementation ofminB, thus, the parameter has to be of the dynamic typeT4, and of the static typeT2. Additionally, allT2 objects have to beT4 objects as well, otherwise, we could create an object ofT2 in line 1, which is statically correct, but it would crash at runtime. In summary,T2 has to be a subtype ofT4. We assign the result to a variable of typeT1, thus the result type of the method has to be a subtype ofT1, thusT3 is a subtype ofT1.

In summary, in order to defineB <: A, we need to ensureT1 :>T3 andT2 <: T4. This means, that the result types have to be covariant and the parameter types contravariant.

2

Referenzen

ÄHNLICHE DOKUMENTE

Does the print method in class PhDAssistant override the print method in class Person ? Hint: look at the Java Language Specification in Section 8.4.8... Exercise

Declarative &amp; Model-based Specifications Declarative specifications just specify which properties hold, they do not say anything about how the holding is achieved. In

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

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