History OOP languages Intro 1 Year Language reported dates vary for some languages... design Vs delievered 1957 Fortran High level programming language 1958 Lisp 1959 Cobol 1960 Algol Structured Programming 1965 Basic 1967 Simula-67 OOP - simulation: class, inheritance, override 1971 Pascal 1973 C, Prolog 1979 ADA object based 1980 Modula 2 1983 Smalltalk OOP - GUI: untyped, class variables 1986 C++ Mulitiple Inheritance, Popularized OOP 1986 Effiel Generic classes, design-by-contract / assertion 1991 Python Multiple Inheritance, scripting, dynamic types 1995 Java interfaces, VM portable, nested classes 2002 C# properties, delegates
OO Definitions && UML Objects are "things" in an OO program Intro 2 Classes are the definitions of objects. Members represent attributes and behaviors instance variables (data members) class variables (shared by all instances) methods (behavior, member functions) constructors -- no type, same name as class, instantiate objects Encapsulation (visibility, accessibility) private accessible within object protected accessible within object and its subclasses public accessible by all Composition has-a relationship (aggregation, UML "lite") Class attributes can be objects. An object "A" has-a object "B". Shape has a Size. Shape Size
Object instantiate new Class Intro 3 Square # side1 : int + Square + draw : void + public # protected - private Name # variable : type + constructor + method : type Square asquare = new Square(); // java instantiation asquare references an object of type Square. asquare.side1 = 10; Note: UML diagrams are not exactly correct due to limitation of powerpoint drawing capabilities
Messages Intro 4 OOP works by objects sending messages and receiving the results of the messages. sendingobject requests a method in the receiving object to act and maybe reply (return something) Messages sends are method (function) calls. In typed languages methods have a return type, name, and argument list The sending object and the receiving object must have the same type. In untyped languages (smalltalk) methods can return objects.
Abstraction Inheritance classes can be dervived, subclassed from super classes classes inherit ("reuse of type") definition Inheritance graph is rooted in the parent Class Intro 5 General (shared) attributes and behaviors are defined near root common factors are placed in ancestral classes abstract classes are never leafs Specific (unique) attributes and behaviors are defined near leafs Is-a relationship objects of a subclass have an "is-a" relationship to the superclass Square is a shape. Multiple inheritance languages have possible name collisions Shape Square
Overloading Intro 6 Method Overloading (ad hoc polymorphism) occurs when a several methods share the same name. Method executed resolved at compile time. Non OOP langs (ie C) also overload functions. Overloading based on Scope. Methods w/ same name defined in different classes. Overloading based on Type Signature. Class has several definitions of a method with same type and name but different arguments. Polyadicity method can take variable number of arguments. C# params Optional arguments args w/ default values provided. Function Count (A, B : integer; C : Integer = 0) { }
Overriding Intro 7 Method Overriding occurs when a subclass redefines the behavior (and scope) of an inherited method (inclusion polymorphism). Method executed resolved at run time. Notation C++ uses keyword virtual to note method in ancestor class C# uses keyword virtual like C++ but also requires keyword override in descendant class. class Parent { public virtual int meth(int a) { } } class Child : Parent { public override int meth(int a) { } } Often used in conjunction with Polymorphism. A method (often abstract) defined in an ancestor class is overriden in some of the descendant classes. (example later)
Polymorphism Intro 8 Abstract class, method (pure polymorphism). An ancestral class that can't be instantiated but can be subclassed. Abstract method is declared and must be defined in the subclass. Polymorphism enables subclasses of a class to all respond to the same method (inherit the method name) but redefine (override) the method's behavior. Java Swing Example if Shape is an abstract class with an abstract draw method Square can define draw to render square shapes (in Shape's scope) Rectangle can define draw to render rectangle shapes Instances of Squares and Rectangles can be keep in a container of Shapes (since they are all shapes)
Interfaces, Properties Intro 9 Single inheritance languages often provide "contractual multiple inheritance" with interfaces ( mixins ). Interfaces define the capability of behaviorial inheritance method signatures. The implementor of an interface "is-a" instance of the interface class OO Properties (C#) are "classes lite" that provide more transparent encapsulation (replace set*(...) and get*() methods useful as indexers (enumerators) over collections. OOPLs have large collection, graphics/gui, I/O, math class libraries. also database, multi-media, network, security...
UML Intro 10 Inheritance is shown using a solid line with an arrow head directed from the subclass to the superclass. Interfaces are shown using a dashed line with an arrow head directed from the implementor to the interface Aggregations (Composition) is shown using a solid line with a diamond head directed from the owner class to the contained class. Aggregation part can exist separately from whole Composition part can't exist separately from whole Associations represents services provided (dependencies) between 2 classes (eg. client - server) are shown using a solid line drawn between the classes. Cardinality quantifies the aggregation and association relationship beween classes. Strict UML uses open arrow heads for inheritance and interfaces and open diamond heads to indicate coposition where the contained object could exist without the owner (powerpoint doesn't have these shape to my knowledge).
Java Swing Example Intro 11 ViewPoly 1 n Shape 1 ViewPoly can have n (many) Shapes Shape is an abstract class Square inherits Shape Rectangle inherits Square Square Rectangle 1 large blue square 1 large red square 4 small green squares 2 white rectangles
Shape # x : int # y : int # color : Color + Shape(int, int, Color) + draw(graphic) : void Square # side1 : int + Square(int, int, Color, int) + draw(graphic) : void Intro 12 UML diagrams + public # protected - private abstract Rectangle - side2 : int + Rectange(int, int, Color, int, int) + draw(graphic) : void
Intro 13 JFrame ViewPoly - scene : Shape[] - count : int - MAX : int - adderror : boolean + ViewPoly(String, int) + paint(graphic) : void - add(shape) : void + main(string[]) : static void composition
Intro 14 // In ViewPoly.java public void paint(graphics g) { for(int i = 0; i < count; i++) scene[i].draw(g); Shape Shape Shape Shape Shape Shape } Square draw(g) : void Square draw(g) : void Rectangle draw(g) : void Square Rectangle draw(g) : void draw(g) : void