DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

Accessing parent class data members from derived class using base keyword in .NET

By Ashok Nalam on 26 Dec 2012 | Category: C# | Tagged: oops inheritance 
we will see how to access parent class data member from derived class using base keyword
  Discuss Add Comment   |  
Add rating Rate this resource   

In this code snippet we will discuss about how to access parent class data member from derived class. Using base keyword in the derived class, we can access the parent class data members.

public class AccessBaseDataMember
{
public static void Main()
{
DerivedClass obj = new DerivedClass();
obj.Get_Base_DataMember();
obj.Get_Derived_DataMember();
Console.Read();
}
}
public class BaseClass
{
protected int i = 10;
}
public class DerivedClass:BaseClass
{
int i = 20;
public void Get_Derived_DataMember()
{
Console.WriteLine("Derived class data member value is {0}",i);
}
public void Get_Base_DataMember()
{
Console.WriteLine("Base class data member value is {0}",base.i);
}
}

Output:

Base class data member value is 10
Derived class data member value is 20

Data Points

  • From the derived class we can access the data members  with access modifiers as Public, Protected, internal, protected internal  of parent class.
  • Private data members can not be accessible.

  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 8586  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.