tags:

views:

63

answers:

2

If I don't mark a method as virtual, will it be available to a derived class?

If using object a, I make changes to method of base class. Then object b accesses the same method m1() of the base class (which is available to derived class).

Will it print those changed value by object a?

Will they share common method ?

class A
{
    public int m(int i)
    {
        return i * i;
    }
}

class B : A
{
}

class C
{
    static void Main()
    {
        A a = new A();
        int x = a.m(2);    // returns 4

        B b = new B();
        int y = b.m(4);    // 16
    }
}
+1  A: 

if i don't use virtual to a method, will it be available to derived class?

Yes the method will be available to any derived classes but you won't be able to override this method in a derived class and change its behavior. In your example class B posses the m1 method because it derives from class A but cannot override it as the method is not virtual.

As a matter of fact your example won't even compile because you didn't specify a return type for the m1 method. Also by not specifying an access modifier for the method, private will be assumed and you won't even be able to call the base method in derived classes (unless using reflection of course). (after edit by @dtb the code will now compile)

Darin Dimitrov
+2  A: 

Yes, because the derived class is a type of the base class.

Consider a Mammal class. All mammals breathe, so we would have Mammal.Breathe(). Now consider a Cat class. Since cats are mammals, we would have this as derived from Mammal, then there is already a Cat.Breathe() inherited from Mammal, without any extra work (the "no extra work" bit being one time-saving aspect of OO).

If Mammal.Breathe() was virtual, then we could make it behave differently in the case of Cat.Breathe(). If it's not virtual, we cannot, though a non-virtual method can call a virtual method, which would make that part of its behaviour overridable.

Jon Hanna
I Got the point sir , your explanation is always of great avail........my out of above program are correct sir? i also want to know if using object "b" i do some changes to method of super class and then i make a new object of A class then if i invoke the method on object "a" of super class which has already been changed using the base class object then a.m1() will get that changed method
NoviceToDotNet
don't look on syntax.. i want to learn concept
NoviceToDotNet
We're back to the differences between hiding and over-riding again. I recommend you try the various possibilities and step through the code in a debugger. The explanations here and on your other questions have already covered the relevant theory, you need to get a *feel* for what actually happens in practice.
Jon Hanna
ok sir i do the same....
NoviceToDotNet
i learnt all explained by you, now i do my own practice .if i stucke somewhere then i revert back to you ...thanks for all your assistance and explaining in detail with example most of my doubts are clear now
NoviceToDotNet