SlideShare a Scribd company logo
1 of 18
Download to read offline
Object-Oriented Programming Language
                                            Chapter 8 : Inheritance


                          Atit Patumvan
          Faculty of Management and Information Sciences
                        Naresuan University
2



                                                               It All Begins at the Root



        @interface Fraction: NSObject
         :
        @end



                                                                          NSObject
                                                                                       root class or superclass




                                                                            Fraction
                                                                                       subclass




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                Object-Oriented Programming Language
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);
                                                                                                  }
                                                                                                  @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                            Object-Oriented Programming Language
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);
    }
    @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                       Object-Oriented Programming Language
5



                                                                        Simple Inheritance

Program 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 = 100
35:!
36:! [b release];
                                    @interface ClassA: NSObject @interface ClassB: ClassA
37:!
                                    {                             -(void) printVar;
38:! [pool drain];
                                    !    int x;                   @end
39:! return 0;!
                                    }
40:! }
                                                                  @implementation ClassB
41: }
                                    -(void) initVar;              -(void) printVar
  :
                                    @end                          {
                                                                  !    NSLog(@"x= %i", x);
                                    @implementation ClassA        }
                                    -(void) initVar               @end
                                    {
                                    !    x = 100;
                                    }
                                    @end

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University           Object-Oriented Programming Language
6



        Extension Through Inheritance: Adding New Methods

Program 8.2 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @interface Rectangle: NSObject
04: {
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.m
12: -(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: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
7



        Extension Through Inheritance: Adding New Methods

Program 8.2 Rectangle.h
01: #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



                         Extension Through Inheritance: Subclassing

Program 8.3 Square.h                                                                 Program 8.3 main.m
01:    #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) side
11: {
12:! return width;!
13: }
14: @end
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                             Object-Oriented Programming Language
9



                                      A Point Class and Memory Allocation

                                                                                     (x,y)



      y                                                     myRect
                                              (x1,y1)


      (0,0)                                                     x
Program 8.4 XYPoint.h                                                                        Program 8.4 Rectangle.h
01:    #import <Foundation/Foundation.h>                                                       :
02:                                                                                          05: @interface Rectangle: NSObject
03:    @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



                                                                    The @class directive

Program 8.4 Rectangle.h
01: #import <Foundation/Foundation.h>
02:
03: @class XYPoint;
04:
                                                                                     @class directive
05: @interface Rectangle: NSObject
06: {
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: @end




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                      Object-Oriented Programming Language
11



                                                         Handling Object in Method

Program 8.4 Rectangle.h                                                              Program 8.4 Rectangle.m
01: #import <Foundation/Foundation.h>                                                01:   #import "Rectangle.h"
02:                                                                                  02:
03: @class XYPoint;                                                                  03:   @implementation Rectangle
04:                                                                                  04:
05: @interface Rectangle: NSObject                                                   05:   @synthesize width, height;
06: {                                                                                06:
07:! int width;                                                                      07:   -(XYPoint *) origin
08:! int height;                                                                     08:   {
09: XYPoint * origin;                                                                09:      return origin;
10: }                                                                                10:   }
11:                                                                                  11:   -(void) setOrigin: (XYPoint *) pt
12: @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: @end




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
12



                                                         Handling Object in Method

Program 8.4 main.m
01: #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



                      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];                                                                   myPoint
25:
26:           [pool drain];
27:           return 0;                                                myRect         width = 5                          x = 100
  :                                                                                   height = 8                         y = 200
                                                                                     origin                              XYPoint@yyyy
                                                                                                        Rectangle@xxxx

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                 Object-Oriented Programming Language
14



                                                           Fixing Reference Problem

Program 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@zzzz

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                              Object-Oriented Programming Language
15



                                                                      Overriding Methods

Program 8.6 main.m
  :                                                                                    :
07:    @interface ClassA : NSObject {                                                22:   @interface ClassB : ClassA
08:       int x;                                                                     23:   -(void) initVar;
09:    }                                                                             24:   -(void) printVar;
10:                                                                                  25:   @end
11:    -(void) initVar;                                                              26:
12:    @end                                                                          27:   @implementation ClassB
13:                                                                                  28:   -(void) initVar
14:    @implementation ClassA                                                        29:   {
15:    -(void) initVar                                                               30:      x = 200;
16:    {                                                                             31:   }
17:       x = 100;                                                                   32:   -(void) printVar
18:    }                                                                             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 B
44:
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



                 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:   @end
10:                                                                                  14:
11:    -(void) initVar;                                                              15:   @implementation ClassA
12:    @end                                                                          16:   -(void) initVar
13:                                                                                  17:   {
14:    @implementation ClassA                                                        18:      x = 100;
15:    -(void) initVar                                                               19:   }
16:    {                                                                             20:   -(void) printVar
17:       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 method
45:         [a printVar];   // reveal value of x
46:
47:         [b initVar];                         // uses overrinding ClassB method
48:         [b printVar];                        // reveal value of x;
  :
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                    Object-Oriented Programming Language
17



       Overriding the dealloc Method and the Keyword super

Program 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: }
  :
                                                                                     Text
Program 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 super
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                      Object-Oriented Programming Language
18



          Extension Through Inheritance: Adding New Instance Variables

Program 8.8 main.m                                                                          Program 8.8 main.m
  :                                                                                           :
07: @interface ClassA : NSObject {                                                          22:   @interface ClassB : ClassA
08:     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:
                                                                                                  @end

16:-(void) initVar                                                                          31:   @implementation ClassB
17:{                                                                                        32:   -(void) initVar
18:     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

More Related Content

Viewers also liked

Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPsEssay Corp
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritanceshatha00
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritanceZubair CH
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator OverloadingHadziq Fabroyir
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.MASQ Technologies
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop InheritanceHadziq Fabroyir
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts246paa
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++Aabha Tiwari
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++gourav kottawar
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programmingAshita Agrawal
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 

Viewers also liked (20)

Concepts of OOPs
Concepts of OOPsConcepts of OOPs
Concepts of OOPs
 
Cv Thomas Faradta
Cv Thomas FaradtaCv Thomas Faradta
Cv Thomas Faradta
 
C h 04 oop_inheritance
C h 04 oop_inheritanceC h 04 oop_inheritance
C h 04 oop_inheritance
 
Inheritance
InheritanceInheritance
Inheritance
 
Oop inheritance
Oop inheritanceOop inheritance
Oop inheritance
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading#OOP_D_ITS - 5th - C++ Oop Operator Overloading
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
 
Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.Inheritance OOP Concept in C++.
Inheritance OOP Concept in C++.
 
Basics of oops concept
Basics of oops conceptBasics of oops concept
Basics of oops concept
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance#OOP_D_ITS - 6th - C++ Oop Inheritance
#OOP_D_ITS - 6th - C++ Oop Inheritance
 
Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming Interesting Concept of Object Oriented Programming
Interesting Concept of Object Oriented Programming
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++OPERATOR OVERLOADING IN C++
OPERATOR OVERLOADING IN C++
 
operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++operator overloading & type conversion in cpp over view || c++
operator overloading & type conversion in cpp over view || c++
 
Machine Learning
Machine LearningMachine Learning
Machine Learning
 
Constructors & destructors
Constructors & destructorsConstructors & destructors
Constructors & destructors
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
constructor and destructor-object oriented programming
constructor and destructor-object oriented programmingconstructor and destructor-object oriented programming
constructor and destructor-object oriented programming
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 

More from Atit Patumvan

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agricultureAtit Patumvan
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)Atit Patumvan
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics toolsAtit Patumvan
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556Atit Patumvan
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationAtit Patumvan
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6Atit Patumvan
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsAtit Patumvan
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Atit Patumvan
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3Atit Patumvan
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2Atit Patumvan
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1Atit Patumvan
 

More from Atit Patumvan (20)

Iot for smart agriculture
Iot for smart agricultureIot for smart agriculture
Iot for smart agriculture
 
An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)An Overview of eZee Burrp! (Philus Limited)
An Overview of eZee Burrp! (Philus Limited)
 
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ตแบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
Chapter 1 mathmatics tools
Chapter 1 mathmatics toolsChapter 1 mathmatics tools
Chapter 1 mathmatics tools
 
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
 
Chapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computationChapter 0 introduction to theory of computation
Chapter 0 introduction to theory of computation
 
Media literacy
Media literacyMedia literacy
Media literacy
 
Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)Chapter 01 mathmatics tools (slide)
Chapter 01 mathmatics tools (slide)
 
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 8
 
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 7
 
การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6การบริหารเชิงคุณภาพ ชุดที่ 6
การบริหารเชิงคุณภาพ ชุดที่ 6
 
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : MethodsComputer Programming Chapter 5 : Methods
Computer Programming Chapter 5 : Methods
 
Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops Computer Programming Chapter 4 : Loops
Computer Programming Chapter 4 : Loops
 
Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)Introduction to Java EE (J2EE)
Introduction to Java EE (J2EE)
 
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 5
 
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 4
 
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 3
 
การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2การบริหารเชิงคุณภาพ ชุดที่ 2
การบริหารเชิงคุณภาพ ชุดที่ 2
 
Computer Programming: Chapter 1
Computer Programming: Chapter 1Computer Programming: Chapter 1
Computer Programming: Chapter 1
 

Recently uploaded

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 

Recently uploaded (20)

Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 

OOP Chapter 8 : Inheritance

  • 1. Object-Oriented Programming Language Chapter 8 : Inheritance Atit Patumvan Faculty of Management and Information Sciences Naresuan University
  • 2. 2 It All Begins at the Root @interface Fraction: NSObject : @end NSObject root class or superclass Fraction subclass Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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); } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 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); } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 5. 5 Simple Inheritance Program 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 = 100 35:! 36:! [b release]; @interface ClassA: NSObject @interface ClassB: ClassA 37:! { -(void) printVar; 38:! [pool drain]; ! int x; @end 39:! return 0;! } 40:! } @implementation ClassB 41: } -(void) initVar; -(void) printVar : @end { ! NSLog(@"x= %i", x); @implementation ClassA } -(void) initVar @end { ! x = 100; } @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 6. 6 Extension Through Inheritance: Adding New Methods Program 8.2 Rectangle.h 01: #import <Foundation/Foundation.h> 02: 03: @interface Rectangle: NSObject 04: { 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.m 12: -(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: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 7. 7 Extension Through Inheritance: Adding New Methods Program 8.2 Rectangle.h 01: #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 Extension Through Inheritance: Subclassing Program 8.3 Square.h Program 8.3 main.m 01: #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) side 11: { 12:! return width;! 13: } 14: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 9. 9 A Point Class and Memory Allocation (x,y) y myRect (x1,y1) (0,0) x Program 8.4 XYPoint.h Program 8.4 Rectangle.h 01: #import <Foundation/Foundation.h> : 02: 05: @interface Rectangle: NSObject 03: @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 The @class directive Program 8.4 Rectangle.h 01: #import <Foundation/Foundation.h> 02: 03: @class XYPoint; 04: @class directive 05: @interface Rectangle: NSObject 06: { 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: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 11. 11 Handling Object in Method Program 8.4 Rectangle.h Program 8.4 Rectangle.m 01: #import <Foundation/Foundation.h> 01: #import "Rectangle.h" 02: 02: 03: @class XYPoint; 03: @implementation Rectangle 04: 04: 05: @interface Rectangle: NSObject 05: @synthesize width, height; 06: { 06: 07:! int width; 07: -(XYPoint *) origin 08:! int height; 08: { 09: XYPoint * origin; 09: return origin; 10: } 10: } 11: 11: -(void) setOrigin: (XYPoint *) pt 12: @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: @end Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 12. 12 Handling Object in Method Program 8.4 main.m 01: #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 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]; myPoint 25: 26: [pool drain]; 27: return 0; myRect width = 5 x = 100 : height = 8 y = 200 origin XYPoint@yyyy Rectangle@xxxx Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 14. 14 Fixing Reference Problem Program 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@zzzz Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 15. 15 Overriding Methods Program 8.6 main.m : : 07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA 08: int x; 23: -(void) initVar; 09: } 24: -(void) printVar; 10: 25: @end 11: -(void) initVar; 26: 12: @end 27: @implementation ClassB 13: 28: -(void) initVar 14: @implementation ClassA 29: { 15: -(void) initVar 30: x = 200; 16: { 31: } 17: x = 100; 32: -(void) printVar 18: } 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 B 44: 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 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: @end 10: 14: 11: -(void) initVar; 15: @implementation ClassA 12: @end 16: -(void) initVar 13: 17: { 14: @implementation ClassA 18: x = 100; 15: -(void) initVar 19: } 16: { 20: -(void) printVar 17: 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 method 45: [a printVar]; // reveal value of x 46: 47: [b initVar]; // uses overrinding ClassB method 48: [b printVar]; // reveal value of x; : Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 17. 17 Overriding the dealloc Method and the Keyword super Program 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: } : Text Program 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 super Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language
  • 18. 18 Extension Through Inheritance: Adding New Instance Variables Program 8.8 main.m Program 8.8 main.m : : 07: @interface ClassA : NSObject { 22: @interface ClassB : ClassA 08: 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: @end 16:-(void) initVar 31: @implementation ClassB 17:{ 32: -(void) initVar 18: 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