Posts Understanding declarative class and instantiating class in .net
Post
Cancel

Understanding declarative class and instantiating class in .net

While creating an object of any class, the class which is on the left of declaration is called Declarative class and one on the right is called Instantiating class.

Taking an hypothetical example, consider the following class diagram,

Inheritance

Man inheriting from Emotions

public class Emotions
    {
        public void Laugh()
        {
            Console.WriteLine("Laughing");
        }

        public void Cry()
        {
            Console.WriteLine("Crying");
        }
    }

    public class Man : Emotions
    {
        public void Read()
        {
            Console.WriteLine("Man Reads");
        }

        public void Play()
        {
            Console.WriteLine("Man Plays Cricket");
        }
    }

For the above class when we create object like this,

Emotions e = new Man();

The above object “e” shows following intellisense ,

Void Main

Void Main

e.{will list public functions from Declarative (Emotions) class but will execute from Instantiating (Man) class}

So, in such scenarios (inheritance), when we declare object of base class and instantiate from derived class, objects show public functions, properties and variables of base (declarative) class in intellisense. However if any of those function is overridden, then derived (instantiating) class function is executed.

The above rule does not stand in shadowing. As in shadowing child class shadows the function from base class. So base class is unaware of any such function declaration.  In that scenario base (declarative) class function will be executed.

 

 

This post is licensed under CC BY 4.0 by the author.