OOP Chapter 8 : Inheritance

2,224 views

Published on

Chapter 8 : Inheritance

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,224
On SlideShare
0
From Embeds
0
Number of Embeds
91
Actions
Shares
0
Downloads
65
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

OOP Chapter 8 : Inheritance

  1. 1. Object-Oriented Programming Language Chapter 8 : Inheritance Atit Patumvan Faculty of Management and Information Sciences Naresuan University
  2. 2. 2 It All Begins at the Root @interface Fraction: NSObject : @end NSObject root class or superclass Fraction subclassAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  3. 3. 3 Subclasses and superclasses @interface ClassA: NSObject { ! int x; NSObject } -(void) initVar; @end subclass superclass @implementation ClassA -(void) initVar { ! x = 100; ClassA } @end @interface ClassB: ClassA subclass superclass -(void) printVar; @end @implementation ClassB -(void) printVar ClassB { ! NSLog(@"x= %i", x); } @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  4. 4. 4 Subclasses and superclasses @interface ClassA: NSObject { ! int x; Class Instance Variables Methods } -(void) initVar; NSObject @end @implementation ClassA -(void) initVar { ! x = 100; } @end ClassA x initVar @interface ClassB: ClassA -(void) printVar; @end @implementation ClassB -(void) printVar { ClassB x intVar printVar ! NSLog(@"x= %i", x); } @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  5. 5. 5 Simple InheritanceProgram 8.1 main.m :28: int main(int argc, char *argv[]) {29:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];30:!31:! ClassB * b = [[ClassB alloc] init];32:!33:! [b initVar];34:! [b printVar]; // x = 10035:!36:! [b release]; @interface ClassA: NSObject @interface ClassB: ClassA37:! { -(void) printVar;38:! [pool drain]; ! int x; @end39:! return 0;! }40:! } @implementation ClassB41: } -(void) initVar; -(void) printVar : @end { ! NSLog(@"x= %i", x); @implementation ClassA } -(void) initVar @end { ! x = 100; } @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  6. 6. 6 Extension Through Inheritance: Adding New MethodsProgram 8.2 Rectangle.h01: #import <Foundation/Foundation.h>02:03: @interface Rectangle: NSObject04: {05:! int width;06:! int height;07: }08:09: @property int width, height;10:11: -(void) setWidth: (int) w andHeight: (int) h; Program 8.2 Rectangle.m12: -(int) area;13: -(int) perimeter; 01: #import "Rectangle.h"14: 02:15: @end 03: @implementation Rectangle 04: 05: @synthesize width, height; 06: 07: -(void) setWidth: (int) w andHeight: (int) h 08: { 09:! width = w; 10:! height = h; 11: } : 21: @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  7. 7. 7 Extension Through Inheritance: Adding New MethodsProgram 8.2 Rectangle.h01: #import <Foundation/Foundation.h>02:#import "Rectangle.h"03:04: int main(int argc, char *argv[]) {05:! NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];06:! Rectangle * myRect = [[Rectangle alloc] init ];07:!08:! [myRect setWidth: 5 andHeight: 8];09:!10:! NSLog(@"Rectangle: w = %i, h = %i", myRect.width, myRect.height);11:! NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);12:!13:! [myRect release];14:! [pool drain];15:! return 0;16: }Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  8. 8. 8 Extension Through Inheritance: SubclassingProgram 8.3 Square.h Program 8.3 main.m01: #import <Foundation/Foundation.h> 01: #import <Foundation/Foundation.h>02: #import "Rectangle.h" 02: #import "Square.h"03: 03:04: @interface Square : Rectangle 04: int main (int argc, char * argv[])05: 05: {06: -(void) setSide: (int) s; 06: NSAutoreleasePool * pool =07: -(int) side; [[NSAutoreleasePool alloc] init];08: @end 07: 08: Square * mySquare = [[Square alloc] init];Program 8.3 Square.m 09: 10: [mySquare setSide: 5];01: #import "Square.h" 11:02: 12: NSLog(@"Square s = %i", [mySquare side]);03: @implementation Square: Rectangle 13: NSLog(@"Area = %i, Perimeter = %i",04: [mySquare area], [mySquare perimeter]);05: -(void) setSide: (int) s 14:06: { 15: [pool drain];07:! [self setWidth: s andHeight: s]; 16: return 0;08: } 17:09: 18: }10: -(int) side11: {12:! return width;!13: }14: @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  9. 9. 9 A Point Class and Memory Allocation (x,y) y myRect (x1,y1) (0,0) xProgram 8.4 XYPoint.h Program 8.4 Rectangle.h01: #import <Foundation/Foundation.h> :02: 05: @interface Rectangle: NSObject03: @interface XYPoint: NSObject 06: {04: { 07:! int width;05: int x; 08:! int height;06: int y; 09: XYPoint * origin;07: } 10: }08: @property int x, y; 11:09: :10: -(void) setX: (int) xVal andY: (int) yVal; 13: -(XYPoint *) origin;11: @end :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  10. 10. 10 The @class directiveProgram 8.4 Rectangle.h01: #import <Foundation/Foundation.h>02:03: @class XYPoint;04: @class directive05: @interface Rectangle: NSObject06: {07:! int width;08:! int height;09: XYPoint * origin;10: }11:12: @property int width, height;13: -(XYPoint *) origin;14: -(void) setOrigin: (XYPoint *) pt;15: -(void) setWidth: (int) w andHeight: (int) h;16: -(int) area;17: -(int) perimeter;18:19: @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  11. 11. 11 Handling Object in MethodProgram 8.4 Rectangle.h Program 8.4 Rectangle.m01: #import <Foundation/Foundation.h> 01: #import "Rectangle.h"02: 02:03: @class XYPoint; 03: @implementation Rectangle04: 04:05: @interface Rectangle: NSObject 05: @synthesize width, height;06: { 06:07:! int width; 07: -(XYPoint *) origin08:! int height; 08: {09: XYPoint * origin; 09: return origin;10: } 10: }11: 11: -(void) setOrigin: (XYPoint *) pt12: @property int width, height; 12: {13: -(XYPoint *) origin; 13: origin = pt;14: -(void) setOrigin: (XYPoint *) pt; 14: }15: -(void) setWidth: (int) w andHeight: (int) h; :16: -(int) area;17: -(int) perimeter;18:19: @endAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  12. 12. 12 Handling Object in MethodProgram 8.4 main.m01: #import <Foundation/Foundation.h>02: #import "Rectangle.h"03: #import "XYPoint.h" :09: Rectangle * myRect = [[Rectangle alloc] init];10: XYPoint * myPoint = [[XYPoint alloc] init];11:12: [myPoint setX: 100 andY: 200];13:14: [myRect setWidth: 5 andHeight: 8];15: myRect.origin = myPoint;16:17: NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height);18:19: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);20:21: NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]);22:23: [myRect release];24: [myPoint release]; :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  13. 13. 13 Can you explain the output from Program 7.5?Program 8.5 main.m :09: Rectangle * myRect = [[Rectangle alloc] init];10: XYPoint * myPoint = [[XYPoint alloc] init];11:12: [myPoint setX: 100 andY: 200];13:14: [myRect setWidth: 5 andHeight: 8];15: myRect.origin = myPoint;16:17: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);18:19: [myPoint setX: 50 andY: 50];20:21: NSLog(@"Origin at (%i, %i)", myRect.origin.x, myRect.origin.y);22:23: [myRect release];24: [myPoint release]; myPoint25:26: [pool drain];27: return 0; myRect width = 5 x = 100 : height = 8 y = 200 origin XYPoint@yyyy Rectangle@xxxxAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  14. 14. 14 Fixing Reference ProblemProgram 8.5B Rectangle.m Program 8.5B main.m : :12: -(void) setOrigin: (XYPoint *) pt 23: [[myRect origin] release];13: { 24: [myRect release];14: if (! origin) 25: [myPoint release];15: origin = [[XYPoint alloc] init]; :16:17: origin.x = pt.x;18: origin.y = pt.y;19: } : myPoint myRect width = 5 x = 100 height = 8 y = 200 origin XYPoint@yyyy pt Rectangle@xxxx x = 100 y = 200 copy XYPoint@zzzzAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  15. 15. 15 Overriding MethodsProgram 8.6 main.m : :07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA08: int x; 23: -(void) initVar;09: } 24: -(void) printVar;10: 25: @end11: -(void) initVar; 26:12: @end 27: @implementation ClassB13: 28: -(void) initVar14: @implementation ClassA 29: {15: -(void) initVar 30: x = 200;16: { 31: }17: x = 100; 32: -(void) printVar18: } 33: {19: 34: NSLog(@"x = %i", x);20: @end 35: } : 36: @end : :41: ClassB * b = [[ClassB alloc] init];42:43: [b initVar]; // uses overrinding method in B44:45: [b printVar]; // reveal value of x;46: [b release]; :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  16. 16. 16 Overriding Methods: Which method is Selected?Program 8.7 main.m (old) Program 8.7 main.m (new) :07: @interface ClassA : NSObject { :08: int x; 12: -(void) printVar;09: } 13: @end10: 14:11: -(void) initVar; 15: @implementation ClassA12: @end 16: -(void) initVar13: 17: {14: @implementation ClassA 18: x = 100;15: -(void) initVar 19: }16: { 20: -(void) printVar17: x = 100; 21:18: } 22: NSLog(@"x = %i", x);19: 23: }20: @end : : :42: ClassA * a = [[ClassA alloc] init];43: ClassB * b = [[ClassB alloc] init];44: [a initVar]; // uses ClassA method45: [a printVar]; // reveal value of x46:47: [b initVar]; // uses overrinding ClassB method48: [b printVar]; // reveal value of x; :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  17. 17. 17 Overriding the dealloc Method and the Keyword superProgram 8.5B Rectangle.m Program 8.5B main.m : :12: -(void) setOrigin: (XYPoint *) pt 23: [[myRect origin] release];13: { 24: [myRect release];14: if (! origin) 25: [myPoint release];15: origin = [[XYPoint alloc] init]; :16:17: origin.x = pt.x;18: origin.y = pt.y;19: } : TextProgram 8.7B Rectangle.m Program 8.7B main.m :33: -(void) dealloc :34: { 23: [myRect release];35: if(origin) 24: [myPoint release];36: [origin release]; :37: [super dealloc];38: } : overriding dealloc method keyword superAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  18. 18. 18 Extension Through Inheritance: Adding New Instance VariablesProgram 8.8 main.m Program 8.8 main.m : :07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA08: int x; 23: {09:} 24: int y;10: 25: }11:-(void) initVar; 26:12: 27: -(void) initVar;13:@end 28: -(void) printVar;14:15:@implementation ClassA Text 29: 30: @end16:-(void) initVar 31: @implementation ClassB17:{ 32: -(void) initVar18: x = 100; 33: {19:} 34: x = 200;20:@end 35: y = 300; : 36: } 37: -(void) printVar : 38: {48: ClassB * b = [[ClassB alloc] init]; 39: NSLog (@"x = %i", x);49: 40: NSLog (@"y = %i", y);50: [b initVar]; 41: }51: [b printVar]; 42: @end : :Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language

×