Objects and Object Oriented Programming A General Introduction

Objects and Object Oriented Programming A General Introduction Copyright © 1998 -2012 - Curt Hill

Purpose • The what and why of object oriented programming • Nothing on how – Appropriate for either C++, ABAP, C# or Java • This is more like theoretical introduction Copyright © 1998 -2012 - Curt Hill

Background: Advances in Programming Languages • 1950 s: Machine and assembly language – 1957 -9 FORTRAN • 1960 s: Compilation and HLL – ALGOL, COBOL, BASIC, LISP, PL/I • 1970 s: Structured programming – C, Pascal, Ada • 1980 s: Objects – C++ • 1990 s: Generics – Java, revisions of C++, Ada Copyright © 1998 -2012 - Curt Hill

The Concept of Type • Determines allowable values – Whole numbers, characters • Determines allowable operations – Assignment, addition • We do not care about representation Copyright © 1998 -2012 - Curt Hill

Structured types • High level languages have a means of composing a new type from other types: – Arrays - homogenous – Records(Pascal), structs(C), groups(COBOL) - heterogeneous • Allows us to group things together into one piece Copyright © 1998 -2012 - Curt Hill

Records or structs • Allow us to put several things together as one • Person description would include – Name – Age (or birth date) – Occupation • Defines the values or state Copyright © 1998 -2012 - Curt Hill

Problems with records • Does not define the operations or behavior • The internal representation is exposed • The next step is an object Copyright © 1998 -2012 - Curt Hill

Example: Fractions • Want a type with following characteristics: – Two values • Numerator and denominator – Some assertions: • Denominator is not zero or negative • Numerator and denominator are reduced – Some operations • Arithmetic, comparisons, I/O, assignment • Any reasonable operations Copyright © 1998 -2012 - Curt Hill

Why this example needs an object • Visibility – Conceal the values, so users will not violate assertions • Encapsulation – The functions are part of the object and cannot be applied to just any sets of variables Copyright © 1998 -2012 - Curt Hill

Booch Diagrams • One way to visualize an object • Grady Booch, a famous OO researcher, invented them • Next we will consider the Booch diagram for a fraction Copyright © 1998 -2012 - Curt Hill

The Fraction Class add mult numerator get. Numerator denominator get. Denominator to. String adjust Legend Non-const methods Const methods property Copyright © 1998 -2012 - Curt Hill

Booch Diagrams • The heavy line shows that the inner part is concealed • The only access is to the public methods – These pierce the outer shell – Green only accesses, does not change – Red changes • Restricting the access through methods guarantees assertions are maintained Copyright © 1998 -2012 - Curt Hill

An object • Has a state – The represented values • Has behaviors – The operations that it can perform – Usually defined in terms of functions or procedures • Encapsulated – The functions apply only to the values in the object – Visibility is set by programmer Copyright © 1998 -2012 - Curt Hill

Another Example • Cruise control is an object • The state encapsulates: – The gas level – Speedometer – Brake / clutch • It offers following methods: – Set – Coast – Accelerate – On and off Copyright © 1998 -2012 - Curt Hill

Terminology • Class: keyword used in several languages to declare an object • Type: any item that defines values and operations • Object: user or system defined class • Some use class and type interchangably (Java) • Some use type for predefined types like int and class for user defined objects Copyright © 1998 -2012 - Curt Hill

More terminology • Instance: a variable is an instance of a class or type • Instantiation: to flesh out a type or class into a variable • Property: a variable in an object, aka member data • Method: a function that defines the interface, aka member function • Client: code that uses an object without knowledge of interior Copyright © 1998 -2012 - Curt Hill

Objects: Abstraction Tools • Object oriented programming is devising black boxes • Each black box is an object • We determine the behaviors of these • The outermost black box is the program • Inside that are numerous smaller objects • Each layer is ignorant of concealed complexity Copyright © 1998 -2012 - Curt Hill

Biology Example • Class is a biology word – Means group of related orders, families and species • Animals (a kingdom) – Vertebrates (a phylum) – Mammals (a class) – Rodents (an order) – Squirrels (a family) – Thirteen line ground squirrel (a specie) Copyright © 1998 -2012 - Curt Hill

Relationship • • The prior list shows anis arelationship A thirteen line ground squirrelis asquirrel A squirrelis arodent A thirteen line ground has all the characteristics of any of the above Copyright © 1998 -2012 - Curt Hill

Enhancing a Type • There are three ways to take an existing type and make a new one • Traditional programming copies or composes to achieve similar purpose • Copy: copy and make slight or major changes • Compose: use a type inside another • Object oriented programming also uses derivation Copyright © 1998 -2012 - Curt Hill

Copy • • Make a file copy of the code Make the modifications and use Must have the source code The two versions are now separate – Changing the original does nothing to the new version – Corrective code needs to be propagated to the copies • This is usually not the right way to do things Copyright © 1998 -2012 - Curt Hill

Composition • Make a new type which contains an old type • The new type has available all the features of the old and may add new functionality • The source of the old type is not needed • The new type does not access the old type’s internals • This is the right way if the new type is basically different – Not anis arelationship Copyright © 1998 -2012 - Curt Hill

Derivation • Inheritance occurs when a new type is derived from an old type • The source of the old type is not needed • Despite the lack of original source, the internal processing of the old type may modified by the new type • This is the right way if the new type could pass for the old type in certain situations Copyright © 1998 -2012 - Curt Hill

Composition and Inheritance • Composition expresses ahas arelationship – Fraction has a numerator (integer) – Person has a date of birth but a person and a date are fundamentally different • Inheritance expresses anis arelationship – A studentis aperson Copyright © 1998 -2012 - Curt Hill

Biology Example Again • Mammals are warm blooded, have fur and feed young milk • Rodents have 1 pair of upper incisor teeth and are mammals • Squirrels have furry tails and are rodents Copyright © 1998 -2012 - Curt Hill

A specific instance • A thirteen line ground squirrel is a squirrel • Does a thirteen line ground squirrel have • warm blood? • 1 pair of upper incisors? • furry tail? • Yes: a thirteen line ground squirrel is a squirrel, is a rodent, is a mammal Copyright © 1998 -2012 - Curt Hill

Inheritance • Deriving new classes from old classes • New class may retain any properties or methods of old • New class may add new properties, new methods • New class may replace old properties or methods Copyright © 1998 -2012 - Curt Hill

Copying and Deriving • Whats the difference? • After the copy, the two classes are separate and no longer related • After a derivation the derived class can be changed by changing the original Copyright © 1998 -2012 - Curt Hill

Inheritance example • • Personhas aname and age Studentis aperson with GPA and major Employeeis aperson with a wage Gradis astudent with degree Copyright © 1998 -2012 - Curt Hill

person set_name employee student set_wage set_major grad set_degree Copyright © 1998 -2012 - Curt Hill

Properties and methods • All classes have properties (eg. name and age) and methods (eg. set_name) – Person explicitly declares it – All the rest inherit it • Grad has degree, as well as GPA and major Copyright © 1998 -2012 - Curt Hill

Terminology of inheritance • Person is the base class or superclass • Student and Employee are derived classes Copyright © 1998 -2012 - Curt Hill

Displaying the contents • Each class needs a display method • How would that work? – Each class would call the display of its ancestor – Then display the things that are unique to it Copyright © 1998 -2012 - Curt Hill

Overloaded functions • In Java, C++ we can overload function names – No overloaded functions in ABAP – There are redefined methods • Four display functions each with a different argument • This can be deduced at compile time – But not always Copyright © 1998 -2012 - Curt Hill

Polymorphism • Literally: many shapes • Classes that are related are interchangable • In C++ only the pointers are interchangable – In ABAP this is a ref type • In Java any container, such as an array can have related contents Copyright © 1998 -2012 - Curt Hill

Effects of Polymorphism • Do a display on any class derived from person • Not know until run-time, which kind it is • Determining the function to use is called binding • Dynamic binding vs static binding Copyright © 1998 -2012 - Curt Hill

Binding • Most programming languages use static binding – At compile or link time determine the function needed – Examples: Pascal, C, COBOL • Some programming languages use dynamic binding – Determine function needed at run time – Examples: LISP, Java and ABAP • C++ uses both, depending on situation Copyright © 1998 -2012 - Curt Hill

Inherited functions • Execute the set_name function against any variable of these four types – The same effect occurs – Defined in the base type – Variable is upcast, that is converted to base type Copyright © 1998 -2012 - Curt Hill

Polymorphism • Execute the display function against any variable of these four types – The different effects occur based on the actual type of the variable – It just calls display knowing that all person derived classes have a display function • A function can accept a person type and receive either a person, student, employee or grad without problem Copyright © 1998 -2012 - Curt Hill

Abstract Base Type • A type that is not instantiatable • Only used to define the interface for derived classes • Consider a generic mammal – Has the characteristics of mammal but none of those of the subordinate classes Copyright © 1998 -2012 - Curt Hill

Multiple Inheritance • Recall the grad class – It inherited behavior from both student and person • Multiple inheritance is the inheriting of behavior from two different classes – These classes are not usually in the same inheritance tree Copyright © 1998 -2012 - Curt Hill

Characteristics of Multiple Inheritance • When it is good it is wonderful – A good example is iostreams in C++ • Inherit from a string formatting object (stream) and a file I/O object (io) • Thus have characteristics of both • When it is bad it is horrendous – C++ is the main language to retain multiple inheritance – Smalltalk had it then removed it – Java never had – Eiffel seems to have done the most with it Copyright © 1998 -2012 - Curt Hill

Problems with Multiple Inheritance • Main problem is name clashes and what to do with them – Suppose two classes have a method X – A new class is a derivation of both • How do we get one X over the other • When both ancestors are from same tree problems compound Copyright © 1998 -2012 - Curt Hill

Interface • The advantages and disadvantages of multiple inheritance have spawned alternatives • Chief of these is the Interface – Java and ABAP have • We define what must be supplied, but do not supply it – Require that a method must be defined but do not provide any code Copyright © 1998 -2012 - Curt Hill

Three Types of Code Client Derived Class Copyright © 1998 -2012 - Curt Hill

Visibility • • • Control over accesses Private - class only Public - every one Protected - class and derived classes One other – C++ - friend classes or functions – Java - friendly or package Copyright © 1998 -2012 - Curt Hill

Rule of Thumb on Visibility • Properties should be private – The user cannot violate any assertions if they cannot directly access the properties – Protected is also acceptable • Methods should be public – This is how the client manipulates things • Exceptions are not illegal – However, there should be a good reason for an exception Copyright © 1998 -2012 - Curt Hill

Static • Static properties – Only one copy no matter how many instances of the class • Static methods – Method without access to instance variables – May only access static variables – Usually performs a stand-alone function Copyright © 1998 -2012 - Curt Hill

Constructors and Destructors • A Constructor is called when an object is created – C++ - implicitly or explicitly • On stack or heap – Java – almost always explicitly • Always on heap • Destructor is called when an object is destroyed – C++ - implicitly – No Java or ABAP destructors – garbage collector takes care of Copyright © 1998 -2012 - Curt Hill

Types of Object Languages • Pure – Smalltalk – Every variable is an object • Hybrid – C++, ABAP – Either paradigm may be used – Java is also a hybrid but much closer to pure than C++ Copyright © 1998 -2012 - Curt Hill

Why are these things important? • Objects become our main abstraction tool • They allow us to divide our programs into bite size pieces • They facilitate reuse • They simplify design Copyright © 1998 -2012 - Curt Hill

Conclusion • A new and better way to think • Superset of structured programming • A new form of analysis, as well Copyright © 1998 -2012 - Curt Hill
- Slides: 52