learning Inheritance, question

Discussion in 'Mac Programming' started by larswik, Aug 26, 2010.

  1. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    Tonight I was learning about subclass and superclass. I have a question about accessing methods from MAIN. In ClassA I created an INT called X and created a method called -(void) setNum. In ClassB I created an INT named Y but gave the method the identical name -(void) setNum as I did in ClassA. In MAIN I say [myB print]; and it will display the value of y which is 10 from ClassB. Is there a way to have it print the X value of 7 from ClassA if they both share the same method name? Or is this bad coding to have identical method names from different classes?

    Code:
    #import <Foundation/Foundation.h>
    @interface ClassA : NSObject 
    {
    	int x;
    }
    -(void) setNum;
    -(void) print;
    
    @end
    Code:
    #import "ClassA.h"
    @implementation ClassA
    
    -(void) setNum
    {
    	x = 7;
    }
    -(void) print
    {
    	NSLog(@"It is %i", x);
    }
    
    @end
    Code:
    #import "ClassA.h"
    @interface ClassB : ClassA 
    {
    	int y;
    }
    -(void) setNum;
    -(void) print;
    @end
    Code:
    #import "ClassB.h"
    @implementation ClassB
    
    -(void) setNum
    {
    	y = 10;
    }
    -(void) print
    {
    	NSLog(@"It is %i", y);
    }
    
    @end
    Code:
    #import "ClassB.h"
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	ClassB * myB = [[ClassB alloc] init];
        
    	[myB setNum];
    	[myB print];
    		
    	[myB release];
        [pool drain];
        return 0;
    }
    
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    Having the same name is usually Ok and exactly what you wanted, you just used an example where it doesn't make sense. When you create a derived class, like ClassB, it should be a ClassA object, just slightly different, or better. Your's isn't. Imagine I add a method to ClassA:

    - (void) printSquare { NSLog (@"The square is %d", x*x); }

    When you call [myB printSquare] in main (), it will print rubbish unless you override printSquare in ClassB as well - a good sign that something is wrong with your design.

    What would make more sense is having a ClassB that only overrides the "print" method, to print the same value x, but lets say in roman numbers (MCM instead of 1900), or with separators between millions, thousands and hundreds (1,900 instead of 1900).
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    The famous question you need to ask yourself when deciding to subclass a class A with subclass B is this:

    Is every B an A?

    So say you have an Animal class and you want to create a Frog a class. Every Frog is an animal so it makes sense to make Frog a subclass of Animal.

    Say you have a Tree class. Every Tree is NOT an animal therefore you should not make Tree a subclass of Animal.
     
  4. yoavcs macrumors regular

    Joined:
    Apr 7, 2004
    Location:
    Israel
    #4
    To answer the OP's question:

    Yes, look up the keyword super.

    It will allow you to invoke a method from a superclass inside a subclass.
     
  5. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #5
    Code:
    #import "ClassB.h"
    @implementation ClassB
    
    -(void) setNum
    {
            [super setNum];
    	y = 10;
    }
    -(void) print
    {
            [super print];
    	NSLog(@"It is %i", y);
    }
    
    @end
    
     
  6. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #6
    That was the Key. I need to add SUPER in front of setNum. I read over the SELF part in the book but did not understand it. Sometimes it is hard to explain what I am trying to do since I am learning, even though these are just examples. So if I have two boxes, BoxA and BoxB, and each box had identical hammers in them, I would say 'Super BoxA Hammer' (the syntax is wrong, I know, it is the concept that I am trying to get) to get the hammer out of BoxA.

    Thanks again... Without this forum self learning would be much harder for me.

    -Lars
     
  7. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #7
    Conceptually that sounds wrong, although it could just be the explanation. In particular, it sounds like you're having trouble with the distinction between a class and an object. Do you really mean "I have two boxes, BoxA and BoxB" or do you mean "I have two *classes* of boxes, BoxA and BoxB, and BoxA is a subclass of BoxB"? To put that in concrete terms it's the different between "I have two dogs, Fido and Rover" and "I have a German Shepherd, which is a kind of Dog".

    The keyword 'super' only applies in the latter case. Rover is not the "superdog" of Fido, but Dog is the superclass of German Shepherd.

    <edit>
    One thing I've found helpful is to consider where the programming word "class" actually comes from. It's this definition of the English word class: "a collection of things sharing one or more common attributes". Same root as things like "the middle class" and "class D felony". So a class in objective-c (the bits between @interface and @end, and @implementation and @end) can be thought of as a description of the things that objects of that class have in common.
    </edit>
     
  8. larswik thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #8
    Thanks Catfish Man.... I do understand it but it is hard to translate it into these postings. I do mean I have 2 CLASSES of Box and BoxB would be a subclass of BoxA Which I think would be written
    Code:
     @inheritance BoxB : BoxA 
    So the example above would mean that BoxB is the subClass and BoxA is the superClass. I would inherit all the methods from superClass BoxA and the new ones I create for BoxB when I work with the instance objects in MAIN.

    Thanks again Catfish Man. Parts of the book are easy and others I have a tough time wraping my head around. Thanks for your patients.....

    -Lars
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    @interface LockedBox : Box ...

    Would be an appropriate subclass of "Box" and would probably HEAVILY make use of [super someMethod] syntax in methods to look inside the box (to see if it is currently locked)
     

Share This Page