Interview Questions

What is the difference between parent and owner ?

Delphi Interview Questions


(Continued from previous question...)

What is the difference between parent and owner ?

An1:
Parent
Parent refers to the component that another component is contained in, such as TForm, TGroupBox or a TPanel. If one control (parent) contains others, the contained controls are child controls of the parent.
Parent determines how the component is displayed. For example, the Left and Top properties are all relative to the Parent.
The Parent property can be assigned and changed during run-time.
Not all components have the Parent. Many forms do not have a Parent. For example, forms that appear directly on the Windows desktop have Parent set to nil. A component's HasParent method returns a boolean value indicating whether or not the component has been assigned a parent.
We use the Parent property to get or set the parent of a control. For example, place two panels (Panel1, Panel2) on a form and place one button (Button1) on the first panel (Panel1). This sets Button's Parent property to Panel1.
Button1.Parent := Panel2;


Ownership
First note that a Form is the overall Owner of any components that reside on it (positioned on the form at design-time). This means that when a form is destroyed, all the components on the form are also destroyed. For example, if we have an application with more that one form, when we call the Free or Release method for a form object, we do not have to worry about explicitly freeing all of the objects on that form - because the form is the owner of all it's components.
Every component we create, at design or run time, must be owned by another component. The owner of a component — the value of its Owner property — is determined by a parameter passed to the Create constructor when the component is created. The only other way to re-assign the Owner is using the InsertComponent/RemoveComponent methods during run-time. By default, a form owns all components on it and is in turn owned by the Application.


An2:
When a component is created, an owner (which is another component) is specified as a parameter to the Create method. Unless the component is destroyed earlier, the owner will automatically free the component when the owner is destroyed. This is the purpose of ownership.

Controls that appear on the screen need to know where to paint themselves, hence the parent property. It specifies the visual container for the control. This property must be specified before a control can be displayed.

When a component is dropped on a form at design time, the owner is the form and the parent is the container on which it is dropped. For example, drop a group box on a form. Its owner and parent are both the form. Drop a check box on the group box. Its owner is the form and its parent is the group box.

The specification of owner and parent are at the programmer's discretion when controls are created dynamically and so the above relationships may not hold. This is especially true with composite components. With run-time creation of controls, the programmer may specify any component to be an owner (including a nil owner) and any window control as the parent. If a nil owner is specified, the programmer is responsible for explicitly freeing the object.

(Continued on next question...)

Other Interview Questions