1

So basically i have been looking of a way to write my code which has alot of uiview instances(makes everything look so messy ! erk!) more readable and elegant.

So far i have seen the use of subclassing but don't really understand the importance/reason of using the practice !

From what i understand:

"class2.m":
  1. You make a class which inherits from the UIView.

    "class1.m":

  2. From the superclass "class1" you make an instance of class one which inherits from class2 ?

So what i am trying to understand is that why would you have a class which inherits from a class2.m that inherits from another (UIView), instead of inheriting UIView directly from class1 ?

Sorry i sound like such a stubborn person but i simply just don't get it ? I really want to know why...

Regards

Edit

Can someone please explain using an example of something like a class called userProfile and guestProfile that subclasses Profile ?

ipalibowhyte
  • 1,553
  • 2
  • 22
  • 34

5 Answers5

3

The reason I subclass UIView is so I can override -drawRect: to do my own custom drawing. Because Core Graphics drawing code can only be executed from this method, it is only exposed when you subclass, giving me an opportunity to customize my views.

Brian Tracy
  • 6,801
  • 2
  • 33
  • 48
1

It's useful if you have several classes (class2, class3, class4...) which inherit from UIView but have something in common. You put the common stuff in class1, and you make these classes inherit from class1.

jcaron
  • 17,302
  • 6
  • 32
  • 46
1

An analogy is that say a UIView is like an Animal, now class1.m is a bird and class2.m is a duck. a duck is a bird which is an animal (inheritance)

now if we were to make a new bird, we dont want to just inherit from animal otherwise we will need to recode everything bird defines, which just adds code bloat and wastes time.

but say you want to make a dog, we cant obviously inherit from bird, because a dog doesnt have any of the features of a bird, so we would have to inherit from animal instead.

hope that clarifies things a bit

Fonix
  • 11,447
  • 3
  • 45
  • 74
1

Short answer:

You subclass UIView in order to inherit all the UIView functionality and add custom functionality.

Long answer:

Your question pertains more to the idea of inheritance than the use of subclassing UIView specifically. Inheritance is a simple concept, but can cause confusion if you are new to object oriented programming.

If you notice, all classes that you will commonly use when developing an iOS application inherit from NSObject. NSObject is the root class of most Objective-C classes, the main class method which NSObject provides (and subsequently any class which inherits NSObject) is alloc (or new depending on usage). This is why you can call [UIView alloc], as alloc is inherited through the class hierarchy. UIView itself doesn't provide an implementation for alloc, rather it inherits the method through NSObject.

A simple and common example to illustrate inheritance uses animals. Let's say we have a super-class at the root of the hierarchy which all animals inherit from called Creature. Now let's subclass Creature to create a Mammal class. The Mammal class will inherit all the functionality (the public interface) of the Creature class.

@interface Creature{

}
- (void)whatAmI;

@end

@implementation Creature

-(void)whatAmI{

     NSlog(@"I am a Creature");
}

@end

@interface Mammal:Creature{

}

@end

Implement our Mammal class:

Mammal *mammal1 = [[Mammal alloc]init];
[mammal1 whatAmI];

Output:

I am a Creature

Note that Mammal inherits the -whoAmI method from the parent class Creature.You can now inherit from Mammal and that subclass will inherit both the functionality of Mammal and Creature. The hierarchy of inheritance can continue.

If you want a firm understanding of Objective-C I recommend Programming in Objective-C by Steven Kochan.

Resources which discuss inheritance in Objective-C:

http://www.techotopia.com/index.php/Objective-C_Inheritance

http://www.tutorialspoint.com/objective_c/objective_c_inheritance.htm

Objective-C multiple inheritance

Community
  • 1
  • 1
Michael
  • 6,561
  • 5
  • 38
  • 55
0

Basically the use of subclass is to customize the new functionality which can be reuse in other class as well.

Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56