BlueBear
BlueBear

Reputation: 7639

Correct Concept for Object-Oriented Programming

I am asking this question to see if my understanding of how Object-Orientation works is correct. Let's say that I have an abstract super class that has several methods all with some sort of implementation.

@interface SuperClass : UIViewController
- (void)methodOne;

// Other public stuff


@end

.......

@implementation SuperClass

- (void)methodOne
{
    //some implementation
}

- (someObject *)objectMethod
{
     //more implementation
}

@end

Then If I am implementing a subclass of that:

@interface SubClass : SuperClass

@end

.......

@implementation SubClass

- (void)methodOne
{
    // override method implementation code
}

@end

So from the example above, if I create a view controller, which is a class of SubClass, will it essentially create a SubClass object and automatically add the implementation of all the SuperClass methods? The idea that I am getting at is if when the preprocessor runs, does it take any method that has not been overridden in the subclass and just place the super class code method into that class for it's use? In this case I only overrode the 'methodOne' method from the super class and left the 'objectMethod' method alone. Does that mean my SubClass will utilize the new overridden 'methodOne' implementation and use the SuperClasses' 'objectMethod' implementation?

Thanks a bunch! Let me know if I need to clarify something.

Upvotes: 0

Views: 995

Answers (2)

Kiefer Aguilar
Kiefer Aguilar

Reputation: 363

If you redefine methodOne in the SubClass implementation, instances of SubClass will use the redefined implementation. If there is no redefinition the SubClass implementation, it will look to the implementation of SuperClass for a definition. This process recursively continues through higher super classes until it finds a definition.

If you'd like to slightly modify the definition in the SubClass, you could do something like:

-(void) methodOne{
    // Some code to add before SuperClass's implementation is called
    ....

    // Call SuperClass's implementation
    [super methodOne];

    // Some code to add after SuperClass's implementation is called
    ...
}

Upvotes: 3

AdamG
AdamG

Reputation: 3718

Yes. The subclass gets all the superclasses methods. If you override one then that method no longer has the superclasses implementation of it. So in this case, you will get the objectMethod of the superclass and the methodOne of the subclass when you instantiate a subclass instance and call either method.

Upvotes: 1

Related Questions