Inheritance:-
Inheritance basically provides two models. Reusability model and extensibility model. Reusability model was there in C also through header file and in VB since Visual Basic is an object based language. An object based language is the one where in we can use the objects or components or modules as is but can’t modify or extend their behavior.
Interview Definition: – Extending or enhancing the functionality of base class.
In inheritance few terms or concepts that needs mentioning are hiding and overriding.
Method or Data Hiding: – Having data member or function in child class having same signature as that in base class. This is not an error but compiler shows you a warning messages when you have same data member in both child and base stating that you are hiding the base class implementation if hiding is intended use new keyword. So by specifying new keyword with data member and functions you can hide base class’s implementation of data or function. This will be clearer with an example later on.
Method Overriding:
There is only a practical difference between hiding and overriding except from the fact that hiding use new keyword whereas Overriding uses override keyword and the overridden function must be declared as either virtual or abstract on the base class. One more thing is that you can also hide data members but can override only member functions.
So I think the ingredients are ready to start creating our recipe. Let’s start cooking.
Scenario 1:-
What data is available through inheritance:-
public class Class1
{
int i; //No Access specifier means private
public int j; // Public
protected int k; //Protected data
internal int m; // Internal means visible inside assembly
protected internal int n; //inside assembly as well as to derived
classes outside assembly
static int x; // This is also private
public static int y; //Static means shared across objects
[DllImport("MyDll.dll")]
public static extern int MyFoo(); //extern means declared in this
assembly defined in some other assembly
public void myFoo2()
{
//Within a class if you create an object of same class then
//you can access all data members through object reference
even private data too
Class1 obj = new Class1();
obj.i = 10; //Error can’t access private data through object.
obj.j = 10;
obj.k = 10;
obj.m = 10;
obj.n = 10;
// obj.s =10; //Errror Static data can be accessed by class names only
Class1.x = 10;
// obj.y = 10; //Errror Static data can be accessed by class names only
Class1.y = 10;
}
}
public class Class2 : Class1
{
int publicDataFromBase, protectedDataFromBase,
internalDataFromBase, protectedInternalDataFromBase;
public void foo()
{
publicDataFromBase = j; // Data was public so can be inherited.
Access through member function as well as object of child
protectedDataFromBase = k; // Data was protected so can be inherited.
Access through member function only
internalDataFromBase = m; // Data was internal so can be inherited.
Access through member function as well as object of child
protectedInternalDataFromBase = n; // Data was protected internal
so can be inherited.Access through member function as well as object of child
y = 10; //Static data can be inherited.
Data was public so can be inherited.
Access through member function as well as object of child
}
}
Now let’s create objects of base and child and see the behavior:-
style="display:block; text-align:center;"
data-ad-format="fluid"
data-ad-layout="in-article"
data-ad-client="ca-pub-5021110436373536"
data-ad-slot="9215486331">
Scenario 1:-
Class2 is within same assembly
Class2 obj = new Class2();
obj.j = 10; // Data was public so can be inherited.
Access through member function as well as object of child
obj.k=10; // Compilation Error. Data was protected so can be inherited.
Access through member function only
obj.m = 10; // Data was internal so can be inherited.
Access through member function as well as object of child
obj.n = 10; // Data was protected internal so can be inherited.
Access through member function as well as object of child
obj.foo(); //foo of child
obj.myFoo2(); //myFoo2 of base
Scenario 2:-
Class2 is in some other assembly
Class2 obj = new Class2();
obj.j = 10;
// obj.m = 10; //internal means public inside assembly
but outside assembly not visible
// obj.n = 10; // Protected internal means public inside
assembly but outside assembly visible to derived classes through member functions
obj.foo();
obj.myFoo2();
Let’s Continue our discussion on OOPS in next article here