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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Generic programming in Java
ITEC200 – Week03 Inheritance and Class Hierarchies.
Java Generics.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
CS102 Data Types in Java CS 102 Java’s Central Casting.
Exam Objective : Legal return types PRESENTED BY : SRINIVAS VG.
Creating Classes from Other Classes Chapter 2. 2 Chapter Contents Composition Adapters Inheritance Invoking constructors from within constructors Private.
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.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Topic 3 The Stack ADT.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Java Tutorial. Object-Oriented Programming Concepts Object –a representation of some item state  fields/members and should be encapsulated behavior 
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.
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Programming With Java ICS Chapter 8 Polymorphism.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Copyright Curt Hill Variables What are they? Why do we need them?
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
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.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
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.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
ISBN Chapter 12 Support for Object-Oriented Programming.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Polymorphism in Methods
Sixth Lecture ArrayList Abstract Class and Interface
Inheritance and Polymorphism
Computer Science II Exam 1 Review.
Java Programming Language
Generic programming in Java
Java Inheritance.
Java’s Central Casting
Subtype Substitution Principle
Presentation transcript:

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

Types in programming languages2 Type definitions Type: A collection of values –boolean: true, false –int: -4, 3, 14593, and much more Data type: Type + operations to manipulate the type –boolean + {&&, ||, !} –int + {+, -, *, etc.}

Types in programming languages3 Definition: Date item Data items –Data, belongs to a type –34, belongs to int –false belongs boolean Simple data item –No subparts Aggregate data item –Has subparts –Classes aggregate other types

Types in programming languages4 Abstract data type (ADT) ADT: Defines a data type in terms of type + operations –Focus on what, not how. –Java: Interface, or public parts of a class Data structure –Physical implementation of an ADT –Focus on how. Example: Java collections –ADT: List –Data structures: ArrayList, LinkedList, etc.

Types in programming languages5 Why do we need types? A data item is just a name/alias/mnemonic of a memory location, i.e. one or more bytes in memory. Using types we can have rules saying which operations can legally be applied to variables (i.e. memory locations). –And which operations to disallow –Example: If 2 variables (memory locations) is declared String it may not make sense to perform the operation ‘-’ (minus).

Types in programming languages6 Strong vs. weak typing Strong typing –Variables, expressions, etc. have types. –Types must “match”. –Languages: Java, C#, and many other languages Weak typing –No types. –Variables are just aliases for memory locations (i.e. bytes) –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 the variables / memory locations himself.

Types in programming languages7 Static vs. dynamic type checking Static type checking –Types of variables, expressions, etc. checked at compile-time. –Java: Uses static type checking Dynamic checking –Types of variables, expressions, etc. checked at run-time. –Java: When you use type casts, checking is deferred to run-time. If something is wrong you get a ClassCastException. Goal: Check as much as possible at compile-time –An error message from the compiler to the programmer is much better than an error message from the program to the end-user. –Java: The main reason for introducing generics in Java 5.0

Types in programming languages8 Types in object-oriented programming In object-oriented programming the programmer creates his own types, called classes. From these classes we make variables, called objects. Types are organized in inheritance hierarchies. –Java (and C#): One single class hierarchy rooted in class Object. –Other language (like C++): More hierarchies, no single root.

Types in programming languages9 Subtypes and substitution Whenever you have an object of a super type you should be able to substitute that object with an object of a subtype. Java example –List mylist; // mylist can be an object of ArrayList, LinkedList –mylist.add(“Anders”) Works no matter is myList is an ArrayList or LinkedList

Types in programming languages10 Subtypes and substitution (2) class S { –B method(A a) { … } } class T extends S { –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 –Java Requires that A is the same type as X –Otherwise Java assumes we want overloading, not overriding. Allows Y to be a subtype of B. –New in Java 5.0

Types in programming languages11 Method overriding in Java Overridden methods must have same signature in subtype as in super type. –Same parameters in same order –Return same type or more specific (subtype). –Throw same or more specific (subtype) exceptions. Exceptions can be considered a kind of “return types” Example: MethodOverriding.java Idea for future Java version: –Parameters of same of more general type. Not considered an overriding in present day Java. Reason: Method overloading (methods with same name, but different parameter types (or number of parameters).

Types in programming languages12 Primitive types in object-oriented programming languages Most programming languages have primitive types like int, double, char, boolean, etc. These types are considered primitive because they are not classes. Java: Primitive types and wrapper classes, like int ~ Integer, boolean ~ Boolean, etc. Java 5.0: Automatic conversion from primitive to wrapper and vice versa (called “auto boxing”)