|
-
January 31st, 2012, 10:22 AM
#1
Get value from inherited class' function while executing the same overrided function?
Hey.
I have a class that implements an interface, that class is later inherited from by a few other classes. The first class has a "basic function" that returns a value. How can I get that value from the "parent" class' function when I override it?
Code:
interface A
{
String getData();
}
class B : A
{
public class B()
{
}
public String getData()
{
return "Hey";
}
}
class C : B
{
public class C()
{
}
public String getData()
{
return Parent->getData() + "\nHello";
}
}
So when I execute:
B b = new B();
b.getData();
OUTPUT: "Hey";
C c = new C();
c.getData();
OUTPUT = "Hey Hello";
I'm wondering this, because I'm trying to inherit classes that have specific "properties" for that class. Say we have an animal -> mammal -> dog, Animal is just the "shell", all "mammals" have some properties that are unique, and later on a Dog has different properties than a Cat but they're still bot mammals and should retrieve the Mammals properties or likewise...
Any ideas?
-
January 31st, 2012, 11:06 AM
#2
Re: Get value from inherited class' function while executing the same overrided funct
You can reference the parent class in C# by using the 'base' keyword (base class is another term for parent class). Try this:
Code:
class C : B
{
public class C()
{
}
public String getData()
{
return base.getData() + "\nHello";
}
}
Look here for more detail: http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|