SlideShare a Scribd company logo
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 (20)

PDF
Concepts of OOPs
Essay Corp
 
DOC
Cv Thomas Faradta
thomas faradita
 
PDF
C h 04 oop_inheritance
shatha00
 
PPT
Inheritance
Anurag Daware
 
PPTX
Oop inheritance
Zubair CH
 
PPTX
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
PPT
Inheritance OOP Concept in C++.
MASQ Technologies
 
PPTX
Basics of oops concept
DINESH KUMAR ARIVARASAN
 
PPTX
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
PPTX
Interesting Concept of Object Oriented Programming
Prognoz Technologies Pvt. Ltd.
 
PDF
Object Oriented Programming Concepts
246paa
 
PDF
OOP Inheritance
Anastasia Jakubow
 
PPTX
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
PPTX
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
PDF
Machine Learning
Anastasia Jakubow
 
PPTX
Constructors & destructors
ForwardBlog Enewzletter
 
PPT
Oops ppt
abhayjuneja
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PDF
Constructor and Destructor
Kamal Acharya
 
DOCX
04 inheritance
Çhäñðrä Såpûtrå
 
Concepts of OOPs
Essay Corp
 
Cv Thomas Faradta
thomas faradita
 
C h 04 oop_inheritance
shatha00
 
Inheritance
Anurag Daware
 
Oop inheritance
Zubair CH
 
#OOP_D_ITS - 5th - C++ Oop Operator Overloading
Hadziq Fabroyir
 
Inheritance OOP Concept in C++.
MASQ Technologies
 
Basics of oops concept
DINESH KUMAR ARIVARASAN
 
#OOP_D_ITS - 6th - C++ Oop Inheritance
Hadziq Fabroyir
 
Interesting Concept of Object Oriented Programming
Prognoz Technologies Pvt. Ltd.
 
Object Oriented Programming Concepts
246paa
 
OOP Inheritance
Anastasia Jakubow
 
OPERATOR OVERLOADING IN C++
Aabha Tiwari
 
operator overloading & type conversion in cpp over view || c++
gourav kottawar
 
Machine Learning
Anastasia Jakubow
 
Constructors & destructors
ForwardBlog Enewzletter
 
Oops ppt
abhayjuneja
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
Constructor and Destructor
Kamal Acharya
 
04 inheritance
Çhäñðrä Såpûtrå
 

Similar to OOP Chapter 8 : Inheritance (20)

PDF
OOP Chapter 3: Classes, Objects and Methods
Atit Patumvan
 
PDF
오브젝트C(pdf)
sunwooindia
 
PDF
iOS Programming Intro
Lou Loizides
 
PDF
Louis Loizides iOS Programming Introduction
Lou Loizides
 
PDF
201005 accelerometer and core Location
Javier Gonzalez-Sanchez
 
PPTX
Java class 4
Edureka!
 
PDF
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Atit Patumvan
 
PDF
Iphone course 1
Janet Huang
 
PPTX
Presentation 3rd
Connex
 
ZIP
Day 1
Pat Zearfoss
 
PDF
iOS overview
gupta25
 
KEY
連邦の白いヤツ 「Objective-C」
matuura_core
 
PDF
OOP: Class Hierarchies
Atit Patumvan
 
ZIP
Day 2
Pat Zearfoss
 
PDF
Oop08 6
schwaa
 
PDF
GC in Smalltalk - Now What?
ESUG
 
PDF
Lecture 03
Nguyen Thanh Xuan
 
KEY
iPhone Development Intro
Luis Azevedo
 
KEY
Objective C 基本介紹
Giga Cheng
 
PDF
The messy lecture
Marian Ignev
 
OOP Chapter 3: Classes, Objects and Methods
Atit Patumvan
 
오브젝트C(pdf)
sunwooindia
 
iOS Programming Intro
Lou Loizides
 
Louis Loizides iOS Programming Introduction
Lou Loizides
 
201005 accelerometer and core Location
Javier Gonzalez-Sanchez
 
Java class 4
Edureka!
 
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Atit Patumvan
 
Iphone course 1
Janet Huang
 
Presentation 3rd
Connex
 
iOS overview
gupta25
 
連邦の白いヤツ 「Objective-C」
matuura_core
 
OOP: Class Hierarchies
Atit Patumvan
 
Oop08 6
schwaa
 
GC in Smalltalk - Now What?
ESUG
 
Lecture 03
Nguyen Thanh Xuan
 
iPhone Development Intro
Luis Azevedo
 
Objective C 基本介紹
Giga Cheng
 
The messy lecture
Marian Ignev
 
Ad

More from Atit Patumvan (20)

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

Recently uploaded (20)

PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Transforming Utility Networks: Large-scale Data Migrations with FME
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 

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