OOPS Concepts in .Net to Shake your fundamentals– Part 2
Introduction
In my previous article on .Net spider we discussed about Encapsulation and Abstraction and got an idea about how to create a simple class and what all access specifiers are there in .Net. Let's continue our discussion about OOPS by moving on to next best thing in Object Oreinted programming world.Yup I am talking about Inheritance. For my previous article please refer
http://www.dotnetspider.com/resources/ViewResource.aspx?resourceId=18616Inheritance:-
Inheritance basically provides two models. Reusability model and extensibility model.Reusability model was there in C also thorugh 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 which 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 implentation 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 more clear 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 keywird and the overriden 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.Lets start cooking.
Scenerio 1 :-
What dats ia avilable 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;
}
}
Scenerio 1 :-
Class2 is within same assembly
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 lets create objects of base and child and see the behavior:-
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
Scenerio 2 :-
Class2 is in some other assembly
Class2 obj = new Class2();
obj.j = 10;
// obj.m = 10; //internal means public inside assembly but outside assmebly not visible
// obj.n = 10; // Protected internal means public inside assembly but outside assmebly visible to derived classes through member functions
obj.foo();
obj.myFoo2();
Scenerio 3:-
Function present in base class not present in child class.
public class MyClass
{
public void foo()
{
Console.WriteLine(“Base version of foo");
}
}
public class MyChild : MyClass
{
}
We will create objects of base and child normally and through casting in all following samples:-
MyClass obj = new MyClass();
MyChild obj2 = new MyChild();
MyClass objCastedFromChild = (MyClass)obj2;
obj.foo();
obj2.foo();
objCastedFromChild.foo();
output:-
Base version of foo
Base version of foo
Base version of foo
Members are searched from child to base when there is no implementation in child it is first searched in immediate base and so on till it's found or a compilation error comes.Since there is no function in child with name foo all the three objects will call base version of foo.
We will continue our discussion in the next article so that you can grasp the concepts slowly and steadily.Please try to imlement everything on your own to have a good command over OOPS.
This is very beneficial for freshers as well as Experienced persons who want to brushup their skills over OOPS,