ITEC 320 Lecture 24 OO in Ada (2)
Review • Questions? • Reminder: Project 4 due on Tuesday • Exam 2 next Friday • Inheritance in Ada
Objectives • Polymorphism • Abstract records • Multiple inheritance
True OO • Tagging – Let Ada know you will be changing it type Object is tagged record X_Coord: Float; Y_Coord: Float; end record; type Point is new Object with null record; type Circle is new Object with record Radius: Float; end record;
Expanding • Passing records to functions / procedures Before: Procedure Test(testVar: in out testType) After: Procedure Test(testVar: in out testType’Class) What do you think this does?
What to execute? • Have function that takes type from ancestor and one that takes the right type • Both have same name • Which one gets executed? • Specify behavior with overrides overriding – can also be not overriding procedure Operation(X: T);
Expanding • Also applies to pointers Before: type testPtr is access all myClass After: type testPtr is access all myClass’Class type Cell is record Next: access Cell; element: testPtr; end record What would happen if we tagged this?
Lessons • Ada still loves its strict typing • Bends the rules a bit with polymorphism • Or is it rule bending? • Remember the ‘class
Using inheritance • What if you need to tell what object is what type? Checking: Procedure test(P: Person) is if P’Tag = Woman’Tag Or if P in Woman’Class then type Person is abstract tagged record Birth: Date end record; type Man is new Person with record Bearded: Boolean; end record; type Woman is new Person with record Children: Integer; end record;
Example • What do you think this means? package P is type T is abstract tagged record … end record; Procedure Op(X: T) is abstract; end p;
Interfaces • Code package Q is type Intis interface; procedure Op(X: Int) is abstract; procedure N(X: Int) is null; procedure Action(X: Int’Class); end Q; Similar, except not allowed to contain information like a record.
Why? • What do you think the purpose of abstract / interface capabilities are? • What scenarios do they allow? • What do they prevent?
Shortcut • Java syntax • varName.methodName(params); • Ada syntax • procedureName(records); record X is … end record procedure Op(var: X, other params here)… Can be called with X.Op(other params); --Ada and Java sitting in a tree Note: has to be tagged and class wide
Private variables • Keep the user away from your data! package Geometry is type Object is abstract tagged private; private type Object is tagged record x_coord : Float; y_coord : Float; end record; end Geometry; Requires a child package to inherit / use the Object class
Multiple inheritance • C++ method • Diamond problem • Java method • Ada method • Can inherit from one tagged • All others have to be interfaces • Allows for procedures / functions • type NT is new T and Int1 and Int2 with …
Summary • Variant Records