Reputation: 227
Hi I am a newbie in objective c.I am going through the concepts of inheritance in objective c.On reading a tutorial on tutorials point.I found that objective C supports multilevel inheritance.But whatever I have implemented now it appears that objective c supports Hierarchical inheritance also.As we can see in the following code:
@interface Shape : NSObject
{
CGFloat area;
}
@end
@interface Square : Shape
{
CGFloat length;
}
@end
@interface Rectangle : Shape
{
CGFloat length;
CGFloat breadth;
}
@end
In such situation,Please help me to understand now the type of inheritances supported by objective c
Upvotes: 2
Views: 697
Reputation: 131418
Multi-level inheritance is where you have several generations of classes:
@interface Shape : NSObject
{
CGFloat area;
}
@end
@interface Rectangle : Shape
{
CGFloat length;
CGFloat breadth;
}
@end
@interface Square : Rectangle
{
}
@end
In the example above, the base class is shape. A Rectangle
is a type of Shape
, and a Square
is a type of Rectangle
. The Rectangle
's parent class is Shape
. The Square
's parent class is Rectangle
, and it's "grandparent" class is Shape
.
(Squares are a special type of rectangle). A square doesn't need any extra properties. In fact it needs less properties than a rectangle.)
Hierarchical inheritance is where a parent class has more than one child class. For example, a shape class might have child classes of Rectangle, Circle, and Triangle.
Objective-C supports both types of inheritance.
Multiple
inheritance is a different thing. That's where a class can inherit traits from multiple parents. Objective-C does not support multiple inheritance. Neither does Swift or Java for that matter.
(BTW the terms multi-level inheritance and hierarchical inheritance are not widely used in the industry. I had to look them up on Wikipedia in order to understand the distinction. I can't think of an object-oriented language that does not support both multi-level inheritance and hierarchical inheritance.)
Upvotes: 0
Reputation: 172
Objective c doesn't support multiple inheritance. You can refer this link Objective C programming guide
Though you can mimic the functionality of multiple inheritance using protocols but there is no official support for multiple inheritance in objective c.
Upvotes: 1