Object Oriented Problem

MrBlack 0 Tallied Votes 278 Views Share
namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
          
            Person p1 = new Student();
            
            p1.Sing();
            p1.Run();
            Console.WriteLine(p1.GetType().Name);
            Console.Read();
        }
    }
    class Person
    {
       
        public  void Run() {
            Console.WriteLine("Person Run");
        }
        public virtual void Sing() {

            Console.WriteLine("Person Sing");
        }
    }
    class Student : Person
    {

        public  void Run()
        {
            Console.WriteLine("Student Run");
        }
        public override void Sing()
        {
            Console.WriteLine("Student Sing");
        }
       
    }
    
}

Object type is Student but why it doesn't run the Student Run method ,if student run method overides we can get the Result of "Student Run"...What is the concept of this

ddanbe 2,724 Professional Procrastinator Featured Poster

Why did you post a normal question as a code snippet?
Hope the monitors can set it right.

james6754 39 Junior Poster

Because when you do new Student(), all it is saying is run a new instance of class Student(run the constructor of the class).

When you assign a new instance of Student to a variable of type Person, when you run sing and run methods of the object p1, the methods of class Person are called.

Help at all?

Bridgekeepers 0 Newbie Poster
namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
          
            Person p1 = new Student();
            
            p1.Sing();
            p1.Run();
            Console.WriteLine(p1.GetType().Name);
            Console.Read();
        }
    }
    class Person
    {
       
        public  void Run() {
            Console.WriteLine("Person Run");
        }
        public virtual void Sing() {

            Console.WriteLine("Person Sing");
        }
    }
    class Student : Person
    {

        public  void Run()
        {
            Console.WriteLine("Student Run");
        }
        public override void Sing()
        {
            Console.WriteLine("Student Sing");
        }
       
    }
    
}

Object type is Student but why it doesn't run the Student Run method ,if student run method overides we can get the Result of "Student Run"...What is the concept of this

You need to tell the compiler in some way that the virtual method has actually been supplied by the inherited class, otherwise it doesn't know about it.
In order to tell the compiler this you use the keyword "override".
If you don't specify this, the compiler will think that even though there is a function with the same name, it is local to the the inherited class, and as such not visible in the inheriance hierachy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.