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

Slides:



Advertisements
Similar presentations
More on Classes Inheritance and Polymorphism
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Inheritance Lakshmish Ramaswamy. Example A Rectangle class with area method A Circle class with area method Array containing references to circles & rectangles.
Generic programming in Java
Inheritance Inheritance Reserved word protected Reserved word super
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
1 Types Object Oriented Programming Spring 2007.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Java Implementation: Part 3 Software Construction Lecture 8.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
Inheritance in the Java programming language J. W. Rider.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Overview of C++ Polymorphism
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Sections Inheritance and Abstract Classes
Inheritance and Polymorphism
Object Oriented Programming in Java
Types of Programming Languages
ATS Application Programming: Java Programming
Object Oriented Programming
Generic programming in Java
Java Inheritance.
Subtype Substitution Principle
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

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

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 languages2

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 languages3

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 languages4

Why do we need types? A variable is just a name / alias of a memory location (one or more bytes in memory) 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 languages5

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 languages6

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 languages7

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 languages8

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 MyList; MyList can be List, LinkedList etc. MyList.Add(“Anders”); Works no matter whether it is List or LinkedList Types in programming languages9

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 languages10

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 Types in programming languages11