Upload
ethan-warren
View
217
Download
0
Embed Size (px)
Citation preview
Types in programming languages 1
Types in programming languages
What are types, and why do we need them?
Types in programming languages 2
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 3
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 4
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 LinkedList
Types in programming languages 5
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 str1, str2;• String str = str1 + str2;• String s = str1 – str2; // illegal
Types in programming languages 6
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: iVar is an integer, sName is a string, etc.
Types in programming languages 7
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
InvalidCastException
• 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 8
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 9
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> MyList;
• MyList can be List, LinkedList etc.• MyList.Add(“Anders”);
• Works no matter whether it is List or LinkedList
Types in programming languages 10
Subtypes and substitution (2)
Class S { virtual B method(A a) { … }}
Class T : S { override Y method(X x) { … }}
• Requirements• Parameters
• A must be a subtype of X• Return types
• 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 11
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-sub-classes.
• Example: MethodOverriding