Exact meaning of 'Super'

This is basic question about iOS.


I will be happy if this question is helpful for other beginners.


In the classB(UIView object), classA(also UIView object) is added by addSubview method.

In this case, my questions are as below.




Questions.

What is ‘super class’ of class B? ‘Class A’ Or ‘UIView’ ??

2.What is ‘super view’ of class B? ‘Class A.view’ right?

in the state of [super viewDidload: ] in classB, super means what ? ‘Class A’ Or ‘UIView’ ?


Thanks

Answered by QuinceyMorris in 111103022

They are three different things that happen to use the same word "super".


1. The superclass of B is UIView (assuming you didn't actually declare B to be a subclass of A). This is a relationship between the classes.


2. If you did '[anA addSubview: someB]', the superview of the instance of B is an instance of A. This is a relationship between instances of the classes.


3. In '[super viewDidLoad]', 'super' means the same object as 'self', so the class of this 'super' is B. However, by using 'super', you're telling the compiler that you do not want to invoke class B's 'viewDidLoad' method, but its superclass's 'viewDidLoad'. That means UIView's viewDidLoad, not A's.

Accepted Answer

They are three different things that happen to use the same word "super".


1. The superclass of B is UIView (assuming you didn't actually declare B to be a subclass of A). This is a relationship between the classes.


2. If you did '[anA addSubview: someB]', the superview of the instance of B is an instance of A. This is a relationship between instances of the classes.


3. In '[super viewDidLoad]', 'super' means the same object as 'self', so the class of this 'super' is B. However, by using 'super', you're telling the compiler that you do not want to invoke class B's 'viewDidLoad' method, but its superclass's 'viewDidLoad'. That means UIView's viewDidLoad, not A's.

Superclass and superview are completely different. You could think of the class as *what* an object is. If it's a Car, with a superclass of Vehicle, it can only ever be a car. You can't change it into something else like a Dog (superclass Animal) just by moving it.


The superview, in contrast, is *where* a UIView object currently is in the view hierarchy on the screen at runtime. (The whole concept of subview / superview is only meaningful for UIView and its subclasses, not *all* classes like subclass / superclass.) You can remove a UIView from one parent (superview) and put it in a different one. A UIView may not even have a superview if it hasn't yet been added to the hierarchy. It's a dynamic thing, as opposed to subclass / superclass which is fixed once the object is created.

You are conflating two different things.


super is used to call up to your superclass when you override a method. For instance, viewDidLoad is declared in UIViewController. You have a subclass of UIViewController, say MenuViewController, or whatever you've called it.


When you override a method on your superclass (in this case that method would be viewDidLoad), you often do:


-(void)viewDidLoad
{
//Your superclasses view did load implementation will be called.
  [super viewDidLoad];

  //We've called super, now we add our own set up code after.
}


superview is completely different. That is a property on UIView. It's how you add a view inside of another view:


[someView addSubview:anotherView];
//After calling this method, anotherView's superview property will be someView.
//If you did this.

if (anotherView.superview == someView)
{
   NSLog(@"I log out.");
}


superview is a property name.... you use super to call up to your superclass.

Thank you so much ^ ^

Thanks you for showing me kind example code ^ ^

Exact meaning of 'Super'
 
 
Q