0

I have a small query related to OOP concept in C#.

I have an interface

interface intf { string Hello(); } 

A base class

public class BaseClass { public string Hello() { return "Hello of base class called"; } } 

A child class that is derived from BaseClass and implements the interface intf as well

public class ChildClass : BaseClass, intf { string Hello() { return "Hello of child class called"; } } 

Now my question is that when I create an object of ChildClass then when I call the hello method it always calls the hello method of BaseClass. Firstly, why does it call the Hello of the BaseClass? Secondly, how can I call the Hello of the ChildClass?

private void Form1_Load(object sender, EventArgs e) { ChildClass obj = new ChildClass(); MessageBox.Show(obj.Hello()); } 
2
  • Why declare the method on the interface too? What are you trying to accomplish? Also, are you aware of the fact that you can explicitly implement an interface? Commented Sep 16, 2010 at 3:51
  • Well i am just trying to learn the conepts and yes i am aware that if i do intf.hello this will call my interface method. thanks for the reply Commented Sep 16, 2010 at 3:57

5 Answers 5

6

First of all, you did not provide an access modifier for Hello in ChildClass. This makes it private by default. To access that method from outside of the class, mark it public (or internal if using it from the same namespace). As it stands now, the only publicly visible Hello method is the base class method.

Secondly, once you have resolved the access issue, Hello will hide the method in the base. If this is intentional, it is advisable to use the new keyword with the method

public new string Hello() 

If your intention was not to hide the method but instead to override it, mark the method as virtual in the base class and use the override keyword in the child.

public class ChildClass : BaseClass, intf { public override string Hello() { return "Hello of child class called"; } } public class BaseClass { public virtual string Hello() { return "Hello of base class called"; } } 

This will allow you to always call the child method unless you explicitly call the base method from within the child.

Calls like this from the outside world

((BaseClass)child).Hello(); (child as BaseClass).Hello(); BaseClass baseClass = new ChildClass(); baseClass.Hello(); 

Will result in the child method being used.

Calls like this from within the child class

base.Hello(); 

Will result in the base class method being used.

Sign up to request clarification or add additional context in comments.

Comments

2

Why call the Hello of base

Your child class implements the interface, so it is considered to contain a public Hello method. But in your child class, Hello is not public. The Hello method in base class is public and is considered to be the implementation of the interface.

How to call Hello of child class

Make the Hello in child class public. Then it will be considered to an implementation of the interface.

Comments

2

Declare the method in the base class virtual and then override it in the child class. That way, the virtual call will resolve to the child class instead of the parent class.

Update: To answer the question about why it still the base method even after you put new in the method declaration:

By default, class members are private. Therefore, since you did not mark the method as public, the method is private. Therefore the programs is force to called the base method as it's the only one that's accessible.

3 Comments

Also, the method on the child class should be public
Esteban, Thanks for the reply and i agree that it will call the hello of child when i use the virtual and override keywords. Now let me modify the code little bit and add new keyword to my child class hello key if i check now it still calls the hello of base. WHY?
check for errors and warnings when you build your project. If you don't have your virtual and override keywords right, you'll get a warning like: "'App.ChildClass.Hello()' hides inherited member 'App.BaseClass.Hello()'. Use the new keyword if hiding was intended."
0

The way you've currently declared your methods, the method Hello() in the ChildClass in inaccessible, and hence the public method in the BaseClass is called. What you really want is what Esteban said though, declare the base method public virtual, the derived method public override, otherwise your method is just shadowing the base class method.

Comments

0

As other said, make the Hello method in the Child class public override.

Also, IMHO, you should implement the interface in the base class, not the child class. But that has nothing to do with the fact your method doesn't get called.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.