Masoud Mohammadi
Masoud Mohammadi

Reputation: 1749

What is the purpose of abstract properties, when we still can use them in our abstract class?

Let's see the example at first:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Person p = new Manager();
            Manager p2=new Manager();

            p.Name = "Ahmad";
            p.Introduce();
            p.SayHello();
            p2.SayHello();
            p2 = (Manager)p;
            p2.SayHello();
        }
    }

    public abstract class Person
    {
        protected Person()
        {
            Name = "Reza";
            Introduce();
        }

        public abstract string Name { get; set; }

        public void SayHello()
        {
            Name = "Ali";
            MessageBox.Show(Name);
        }
        public abstract void Introduce();
    }

    public class Manager : Person
    {
        public new void SayHello()
        {
            MessageBox.Show("Test2");
        }

        public override string Name { get; set; }

        public override void Introduce()
        {
            MessageBox.Show("Hello " + Name);
        }
    }
}

at first i hadn't written constructor for base class.

as far as i know the purpose of abstract method is to force the derived class to implement from it, and because of that we can't implement abstract methods in base class.

then i added an abstract property. and i saw that we can initialize that property in base class and using it.

1st: Wasn't the purpose of abstract to just declare them and let derived class to implement it?
Why can we use the property in base class?

we could just implement a non-abstract property at first, and it would make no difference.

then i added the constructor and things get more complicated. we can use Introduce() method in constructor to call Introduce() from Child class (i understand that from debugger).

so Child inherits from Father here, but we call a method in Child from Father, which is strange and is somehow against the rules of inheritance.

2nd: What have i missed?

Edit:

enter image description here

enter image description here

Upvotes: 2

Views: 1791

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149628

Wasn't the purpose of abstract to just declare them and let derived class to implement it? Why can we use the property in base class?

You can't use the abstract property in the base class, as you cannot instantiate it directly. You'd create an abstract property for the same reasons you'd want to create an abstract method, which is make sure your derived types implement it.

Your Name property isn't actually using the base-class instance. When called from SayHello, it will go to your derived-type implementation of Name and use that.

So Child inherits from Father here, but we call a method in Child from Father, which is strange and is somehow against the rules of inheritance. What have i missed?

It's not only strange, its an error which might cause a run-time exception, for the fact that the child object hasn't been initialized yet. If you were to access any member of Manager that are instantiated via managers constructor (not via field initialization) inside Introduce, you would get an exception:

public class Manager : Person
{
    public Manager()
    {
        s = "Hello";
    }

    private string s;

    public override string Name { get; set; }

    public override void Introduce()
    {
        Console.WriteLine(s.ToLower());
    }
}

When you now call Introduce, you'll see a NullReferenceException, as s hasn't been initialized yet.

If you want to call an abstract method on derived type, make sure you do it after object instantiation. Perhaps via an Initialize method on the base.

You can read Virtual member call in a constructor for more information:

if you make a virtual method call in a constructor, and it is not the most derived type in its inheritance hierarchy, that it will be called on a class whose constructor has not been run, and therefore may not be in a suitable state to have that method called.

Upvotes: 2

Related Questions