13
3C-1 Purity Typing Language semantics Inheritance model Single vs. Multiple inheritance Common root Modular mechanisms Generics Object Oriented Languages Object Oriented Languages Comparison Comparison

3C-1 Purity Typing Language semantics Inheritance model Single vs. Multiple inheritance Common root Modular mechanisms Generics Object Oriented Languages

Embed Size (px)

Citation preview

Page 1: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-1

• Purity

• Typing

• Language semantics

• Inheritance model Single vs. Multiple inheritance Common root

• Modular mechanisms

• Generics

Object Oriented Languages ComparisonObject Oriented Languages Comparison

Page 2: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-2

PurityPurity• Little smalltalk: Pure object oriented

• All predefined types are classes• Exception: Integer

• x <- 4• y<- 2+2• a<- List new; add: 1• b<- List new; add: 1• What is the result of the following : ( hint: == for reference equality, = for value equality )

• x == y • x = y• a == b• a = b

• All user defined types are classes• All operations are messages to objects

• No global functions

Page 3: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-3

Purity – Cont.Purity – Cont.

• Java: Almost pure object oriented• Not all predefined types are classes

• Primitive types: int, long, float, double, boolean …• Primitives have wrapper classes they can be boxed in:m_employees.put(

new Integer(employee.getNumber()), employee);

• All user defined types are classes

• Not all operations are messages to objects

• basic arithmetic are built-in operators

• flow control blocks ( if, … )

• No global functions

Page 4: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-4

Purity – Cont.Purity – Cont.• C++: Not pure object oriented

• Not all predefined types are classes

• Primitive types: int, long, float, double, bool…

• No built-in wrapper classes

• Not all user defined types are classes• typedef• union

• Not all operations are messages to objects

• Global functions are allowed

The reason: C compatibility.

Page 5: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-5

Type SystemType System• Little smalltalk:

• Dynamic typing – type rules are enforced at runtime• Variables have no associated types• Values of different types may be assigned to a single

variable• Strong typing

• The relation between a value and its type can’t be changed

• Java:• Static typing – compile-time type checking• Strong typing

• C++:• Static typing• Weak typing

• Implicit type conversion by the compiler• Relation between a value and its type can be broken

Page 6: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-6

Type System – Cont.Type System – Cont.Examples: What is the value of y in the following code:

double x = 2.3; int y = x;

C++ : 2 Implicit conversion by the compiler

Java: compilation error !! No conversion from double to int

What about LST ? ( hint: think how to write this code in LST )

Page 7: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-7

Language SemanticsLanguage Semantics• Little smalltalk: reference semantics

• Dynamic typing prevents from determining the size• Java: reference semantics

• Exception: Primitives (value semantics)class myClass{ private int x; private Integer y; private Double z;

myClass(){

y = new Integer(3);x = 3;

int yValue = y.intValue(); // yValue = 3double zValue = z.doubleValue(); //

runtime error!! }

Note the difference between objects and primitives

Page 8: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-8

Language Semantics – Cont.Language Semantics – Cont.• C++: Value semantics by default (C compatibility)

• Reference semantics on reference types

int a = 5;

int b = 6;

int& iRef = a;

iRef = b;

What is the value of iRef?What is the value of a?

Page 9: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-9

Class HierarchyClass HierarchyLittle smalltalk:

• Single inheritance of both interface and implementation• class hierarchy with one common root class

What does the following class do?

Class mystery Object lMethods mystery new l <- List new.| DoIt : aClass l add: aClass. (aClass superClass notNil) ifTrue: [self DoIt :(aClass superClass)]]

Page 10: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-10

Class Hierarchy – Cont.Class Hierarchy – Cont.

• Java inheritance mechanisms:• Single inheritance of combination of implementation and

subtype (using extends keyword)

• Multiple subtype (interface) inheritance (using implements keyword).

• class hierarchy with one common root class

class Employee2 extends Employee, implements Serializable, Comparable{

…} What methods are

inherited?What methods must be implemented?

Page 11: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-11

Class Hierarchy – Cont.Class Hierarchy – Cont.

C++: • Multiple inheritance is allowed

• No common root class for all classes

PersonPerson

StudentStudent TeacherTeacher

TATA

Page 12: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-12

Modularity MechanismsModularity Mechanisms• Little smalltalk: no modularity is required

• Source code can reside in one file or multiple files

• Java: modularity mechanisms

• One top-level class per source file It is possible to define inner classes within the top-level class

• Name of class must match the file name

• Packages – a mechanism for organizing classes into namespaces. The package is determined by the directory where the source file is located => Subdirectories create nested packages

• C++

• Classes can be organized in name spaces Nesting of namespaces is obtained by defining a namespace within a

namespace

• A class can spawn multiple files

• A file can contain multiple classes

Page 13: 3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages

3C-13

GenericsGenerics

• Generics - ability to have type parameters on a type • AKA parametric polymorphism

• Little smalltalk: no special genericity support• Dynamically typed languages support generic

programming inherently.

• Java: Generics were introduce in JDK 1.5• Allows creating generic data structures and algorithms

• C++ Templates:• A very powerful, Turing complete mechanism