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=18616

Inheritance:-


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.


Comments

Author: Rakesh Kumar14 Jun 2008 Member Level: Bronze   Points : 1

This is very beneficial for freshers as well as Experienced persons who want to brushup their skills over OOPS,

Author: Nanak Deep14 Jun 2008 Member Level: Bronze   Points : 0

great sir, thanx.....

Author: Phani16 Jun 2008 Member Level: Silver   Points : 1

This is very useful one who wants to know about oops Concepts

Author: Kapil Dhawan16 Jun 2008 Member Level: Gold   Points : 1

nice piece of code,
keep posting such articles
really appreciated work
Thanks a ton
Regards,
Kapil

Author: Gaurav Agrawal19 Jun 2008 Member Level: Silver   Points : 2

Hi sakti,
Very good and detailed article.
It is very helpful for me.
It is very important interview questions and frequently asked in interview.
Again thanks to share your knowledge with us.
Keep Posting and help your fellow developers.

Regards
Gaurav Agrawal

Author: raj kumar19 Jun 2008 Member Level: Silver   Points : 1

hi,

i need detailed answer on

When to implement Interface, when Class. example will be appriciated.

Thanks

Author: deepak kumar19 Jun 2008 Member Level: Bronze   Points : 1

enough information about the oops concept and i like these articles

Author: ab koi apne naam se nahin jaana jayega!!!!!!19 Jun 2008 Member Level: Gold   Points : 0

Waiting for your next article.....

Author: Ashish verma19 Jun 2008 Member Level: Bronze   Points : 1

sir,
It was really a nice piece of coding.It contributes
great in building oops concept.
Please keep on posting more articles.
Thanks.

Author: kalpana19 Jun 2008 Member Level: Bronze   Points : 0

Thanks Sir.

Author: R O B I N20 Jun 2008 Member Level: Gold   Points : 0

Nice work by u....
It will help aloat to everyone

Author: malaikani rajamani30 Jan 2009 Member Level: Bronze   Points : 0

Good nice one. Now i clear with base concepts

Guest Author: Rajan kumar09 Jun 2012

I m very greatful to you for giving me such article .I cleared my concept by getting it.so sir pls keep on posting such articles..



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: