Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
XML
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class Data
    {
        /// <summary>
        /// Virtual Method Called
        /// </summary>
        public void a()
        {
            Console.WriteLine("Base class Method");
        }
    }

    public class Demo1 :Data
    {
        /// <summary>
        /// New Method Called
        /// </summary>
        public void a()
        {
            Console.WriteLine("Child class Method");
        }
        public string Result = "a";
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            Data obj1 = new Demo1();
            obj1.a();
            Console.ReadLine();

        }
    }
}
Posted
Comments
Kishor Deshpande 5-Aug-14 4:21am    
You must have got a warning while compiling this program.
Warning like: Base class method a() hides derived class method, please use new keyword if hiding is intentional.
In such scenario,
1) If you want to have the method called of object Demo1() then use virtual keyword in base class and override keyword in derived class method.
2) If you want the functionality to be like this(Calling the method of reference used to attach the object i.e. Data in this case,) , then use new keyword in derived class method, then you do not get any compile time warnings. :)
Cheers!
Kishor

Because of missing keywords.
To mark a method / property such that it can be overriden in a derived class, use the virtual keyword.
To override a method in a derived class, use the override keyword.
C#
public virtual void a()
public override void a()
Also look at the compiler warnings you got with your code!
 
Share this answer
 
Because he wanted to do something?

The system calls your static Main method when it starts your application: from that point on it's up to you to tell it exactly what to do. If you don't explicitly call a method (or set an handler for an event) then the code you have written will not be executed.

In this specific case, the code constructs an instance of the Demo1 class, and calls the a method via the Data reference called obj1. The system looks at the reference, finds no overloads as calls the base class method.

You might have noticed that when you compiled this you got a warning: these aren't as serious as errors in that they won't stop you application from running, but (particularly when you are just learning) it's a very good idea to change your settings so the do (Easy enough, just double click your projects "Properties" branch in the Solution explorer pane and change the "Build" tab option "Treat warnings as errors" to "All").

Because the original base class method isn't marked as virtual, the system doesn't even try to look for a "latest version" to call, so unless the reference is to a Demo1 instance, it will always call the "lower" version. Try like this instead:

C#
public class Data
    {
    /// <summary>
    /// Virtual Method Called
    /// </summary>
    public virtual void a()
        {
        Console.WriteLine("Base class Method");
        }
    }

public class Demo1 : Data
    {
    /// <summary>
    /// New Method Called
    /// </summary>
    public override void a()
        {
        Console.WriteLine("Child class Method");
        }
    public string Result = "a";
    }
 
Share this answer
 
This is all about inheritance. Read Class Inheritance[^].


I would suggest you to go through the articles below to understand the concept of inheritance.
MSDN : Inheritance (C# Programming Guide)[^]
C# - Inheritance[^]
OOPS Principle - Inheritance in C# with an example[^]


--Amy
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900