C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









OOPS Concepts in .Net to Shake your fundamentals– Part 3


Posted Date: 13 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: shakti singh tanwar       Member Level: Diamond
Rating:     Points: 10



Continuing our discussion on .Net.This is the third article in the series and in this we will be discussing all the possible scenerio's of inheritance.
For previous articles please refer
http://www.dotnetspider.com/resources/18616-OOPS-Concepts-Net-Shake-your.aspx
and
http://www.dotnetspider.com/resources/18617-OOPS-Concepts-Net-Shake-your.aspx

Scenario 4:-
Function present in both base class and child class.This concept is called Method hiding.
public class MyClass
{
public void foo()
{
Console.WriteLine("Base version of foo");
}
}

public class MyChild : MyClass
{
public new void foo()
{
Console.WriteLine("Child version of foo");
}
}
Without new keyword compiler will give a warning that you are hiding the base class implementation.
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
Child version of foo
Base version of foo
So in normal scenerio’s follow the simple rule. Thy object thy function.
Scenerio 5:-
Function present in both base class and child class but this time virtual in base class.
public class MyClass
{
public virtual void foo()
{
Console.WriteLine("Base version of foo");
}
}

public class MyChild : MyClass
{
public new void foo()
{
Console.WriteLine("Child version of foo");
}
}
Without new keyword compiler will give a warning that you are hiding the base class implementation.
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
Child version of foo
Base version of foo
So in this scenerio also follow the simple rule. Thy object thy function.

Scenerio 6:-
Function present in base class as virtual but not present in child class.
public class MyClass
{
public virtual 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
So no difference from scenerio 3. 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.
Scenerio 7:-
Function declared as virtual in base class and overridden in child class.
public class MyClass
{
public virtual void foo()
{
Console.WriteLine(“Base version of foo”);
}
}

public class MyChild : MyClass
{
public override void foo()
{
Console.WriteLine(“Child version of foo”);
}

}
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
Child version of foo
Child version of foo
So you can see the difference. In Method Hiding base class object was calling its own implementation but in this case base is calling the child class’s implementation. So in case of overriding and casting behavior changes.

Scenerio 8:-
Function declared as abstract in base class and overridden in child class.
public abstract class MyClass
{
public abstract void foo();

}

public class MyChild : MyClass
{
public override void foo()
{
Console.WriteLine(“Child version of foo”);
}

}
Now we can’t create base class object directly as base class is now an abstract class nad thus objects can now be creates via casting only. We will create objects of child normally and base object through casting in following sample:-
//MyClass obj = new MyClass(); //Not possible now
MyChild obj2 = new MyChild();
MyClass objCastedFromChild = (MyClass)obj2;
obj2.foo();
objCastedFromChild.foo();
output:-
Child version of foo
Child version of foo
So you can see the difference. In Method Hiding base class object was calling its own implementation but in this case base is calling the child class’s implementation. So in case of overriding and casting behavior changes.


Contd...




Responses

Author: Nanak Deep    14 Jun 2008Member Level: Bronze   Points : 2
sir,
everything is fine, but space should have been given between lines , to make it easy to read and understand..

but it covers almost everything related with inheritence

thanx sir....


Author: Ashish verma    19 Jun 2008Member Level: Bronze   Points : 2
Dear sir,
I would like to thanks to you for providing us such a clear concept about inheritence.This article contribute much in removing doubts about inheritence.
Thanks.


Author: kalpana    19 Jun 2008Member Level: Bronze   Points : 0
Really Great.


Author: Gaurav Agrawal    20 Jun 2008Member Level: Silver   Points : 2
Dear Sakti,

Your article is really great and covers very detailed description of OOPs concepts with example.
Really good.
keep Posting and help your fellow developers
happy programming.

regards
Gaurav


Author: Ramkumar    20 Jun 2008Member Level: Bronze   Points : 0
2nd attndnce and gained a lot


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: OOPS Concepts in .Net to Shake your fundamentals– Part 2
Previous Resource: How to send a Mail using vb.net
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

budget conference call

Contact Us    Privacy Policy    Terms Of Use