Types in programming languages What are types and











- Slides: 11

Types in programming languages What are types, and why do we need them? Types in programming languages 1

Definition: Type • Type: A collection of values • bool: true, false • int: -4, 3, 6, 2, etc. • Data type: Type + operations to manipulate the type • Bool + {&&, ||, !} • Int + {+, -, *, etc. } Types in programming languages 2

Definition: Data item • Data belong to a type • 34 belongs to int • False belongs to bool • Simple data item • No subparts • Examples: 34 and false • Aggregate data item • Has sub-parts • Classes aggregate other types Types in programming languages 3

Abstract Data Type (ADT) • Abstract data type • Defines a type in terms of type + operations • Focus on what you can do with data items, • not how it is done • Can be programmed using interfaces • Data type • Implementation of the ADT • Focus on how • Programmed using a class • Example: Collections • ADT: IList • Data type: List and Linked. List Types in programming languages 4

Why do we need types? • A variable is just a name / alias of a memory location (one or more bytes in memory) • 01001111 • Using types we can have rules saying which operations can legally be applied to variables (memory locations) • And which operations to disallow • Example: • String str 1, str 2; • String str = str 1 + str 2; • String s = str 1 – str 2; // illegal Types in programming languages 5

Strong vs. weak typing • Strong typing • Variables, expressions, etc. have types • Types must “match” • Languages: C#, Java, and many other programming languages • Weak typing • No types • Variables are just aliases for memory locations • Languages: Assembly, BASIC, and many other languages • When strong typing was introduced (late 1960’es) programmers used to say • “Strong typing is for programmers with weak minds” • Meaning: A programmer should be able to remember the “types” of variables himself. • Hungarian notation: i. Var is an integer, s. Name is a string, etc. Types in programming languages 6

Static vs. dynamic type checking • Static type checking • Types of variables, expressions etc. are checked at compile-time • C#, Java etc. used static type checking • Dynamic checking • Type of variables, expressions etc. are checked at runtime. • C# when you use type casts, checking is deferred to run-time: You might get a Invalid. Cast. Exception • Check as mush as possible at compile-time • An error message from the compiler to the programmer is much better than an error message form the program to the end-user Types in programming languages 7

Types in object-oriented programming • In object-oriented programming the programmer creates his own types, called classes. • From theses classes we make variables, called objects • Types are organized in an inheritance hierarchy • C#, Java: A single class hierarch rooted in the class Object • C++: More hierarchies, not single root Types in programming languages 8

Subtypes an substitution • Whenever you have an object of a super type you should be able to substitute that object with an object of a subtype • Example • IList<String> My. List; • My. List can be List, Linked. List etc. • My. List. Add(“Anders”); • Works no matter whether it is List or Linked. List Types in programming languages 9

Subtypes and substitution (2) Class S { virtual B method(A a) { … } } • Requirements • Parameters • A must be a subtype of X • Return types Class T : S { override Y method(X x) { … } } • Y must be a subtype of B • Called co-variant return types • These requirements are not handled well in many programming languages, like C# Types in programming languages 10

Method overriding vs. method overloading • Overridden methods • same signature in super-type as in subtype • Overloaded methods • Same name but different parameters • Some C# keywords • Virtual, used on base-class methods • The method can be overridden in sub-classes • Override, used on sub-class methods • The method overrides a method from the base-class • New, used on sub-class methods • The method is not overridden. • Instead the sub-class has is own (new) version of the method. • Sealed, used on sub-class methods • The method was virtual in a base-class, but cannot be overridden in sub-classes. • Example: Method. Overriding Types in programming languages 11