If I have a class class_Main. I am making an another class class_Object and creating an object of class_Main in class_Object. Now I am making the class_Extended by extending the class_Main . So what is the difference between class_Object and class_extended.
Both are wrapping underlying class_Main in new functionality, but in different ways:
The first is composition - class_Object contains an instance of a class_Main, and class_Object can provide its own API that may provide some access to class_Main functionality or use that functionality in a custom way. In summary, class_object HAS A class_Main.
The second is inheritance - class_Extended contains an instance of a class_Main because it IS an instance of class_Main (read about OOP and Polymorphism to see what I mean). Again, additional functionality can be provided around class_Main but the default is to behave as a class_Main with all its APIs, unless they are overridden or extended. In summary, class_Extended IS A class_Main.
The above distinction may help you decide which one to use for your situation.
Read also about Polymorphism for reasons why you might use one or the other.
In reply to gordon:
Thanks a lot Gordon
You say " class_Extended contains an instance of a class_Main because it IS an instance of class_Main."
Actually class_Extended is an extended class of class_Main .
I am still confused with the explanation .
In reply to mamta_r:
Polymorphism means that extended classes can be used in any way that the base class can be used.
When you extend a base ‘bus access sequence’ or a ‘packet transmitted’ base class, to create a more specialized ‘write cycle bus access sequence’ or ‘packet type X transmitted’ class, those new extended classes can be used anywhere that the original base classes can be used.
Read about OOP principles, and try some of your own experiments, it will become clearer.
I edited my answer to add clarification of what I meant by ‘IS an instance’.
You will often see the two types of OOP arrangement I listed - composition and inheritance - described by the phrases “has a” and “is a”. With composition, class_Object HAS A class_Main. With inheritance, class_Extended IS A class_Main.
In reply to gordon:
Thanks Gordan
I know about the extended classes . What I am asking here is the difference while using when we make an extended class and when we make an object of the class?
We are deviating here from original Question. I can’t find explanation in the first comment you have answered.
In reply to mamta_r:
In case of extended class(class_Extended), the base class (class_Main) members/properties+methods (say int a, function func) will directly become the exended class properties+methods. From an extended class object handle, you will be able to access the the properties&methods of base class (Ex: class_Extended_h.a, class_Extended_h.func). The only benefit will be that you can over-ride, or add new methods/properties in the extended class.
Where as in the other case (class_object case) you mentioned, where you have created an object of class_Main say class_Main_Obj inside the class_Object, in which case only class_Main_Obj have become new member of the class_Object; And you can’t directly access the properties/methods of class_Main from class_Object unless you say something like class_Object_h.class_Main_Obj.func or class_Object_h.class_Main_Obj.a.
To summarize, the first case is called “class extension” & the second case is called “Class instantiation”.
One of the places where Class instantiation is mainly used is during the construction of testbench, where you instantiate several agents, scoreboards, coverage models etc inside an environment.