Loading ...

Instantiation of class.

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Instantiation of class.

Instantiation of class.

Posts under the topic: Instantiation of class.

Posted: 10/25/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

I am newbie in C#.I have one confusion.

There are two classes A and B.

using System;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            A objA = new A();

            A objB = new B();
        }
    }

    class A
    {
        public void MethodA()
        {
            Console.WriteLine("method of A class");
        }
    }

    class B : A
    {
        public void MethodB()
        {
            Console.WriteLine("method of B class");
        }
    }
}


Now the confusion which I have is that what is the meaning of:

A objB = new B();


I have seen and instantiated class like this:

A objB = new A();


Can anyone tell me why we used:

A objB = new B();

Thanks in advance.


Posted: 10/25/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

Mohit, you have some really challenging and interesting questions :) -

Here is how I think you will understand this best.

I've rewritten your code so that I've added constructors in each class. Run it and see the difference and what ideas will come to you once you see the printed result.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("----------  A objB = new B();  -------------");
            A objB = new B();            
            objB.MethodA();
            Console.WriteLine(Environment.NewLine);
            Console.WriteLine("----------  A objB = new A();  -------------");
            A objA = new A();
            objA.MethodA();
            Console.ReadLine();
        }
    }

    class A
    {
        public A()
        {
            Console.WriteLine("Constructor of class A");
        }

        public void MethodA()
        {
            Console.WriteLine("method of A class");
        }
    }

    class B : A
    {
        public B()
        {
            Console.WriteLine("Constructor of class B");
        }
        public void MethodB()
        {
            Console.WriteLine("method of B class");
        }
    }

Hope this helps and will clear up your doubts Wink


Posted: 10/25/2010

Contributor 2237  points  Contributor
  • Joined on: 9/24/2009
  • Posts: 172

hajan said:

user="Hajan Selmani"]Mohit, you have some really challenging and interesting questions :) -

Thank you HajanSmile

Check my code now -

using System;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("----------  A objB = new B();  -------------");
        A objB = new B();
        objB.MethodA();
        Console.WriteLine(Environment.NewLine);
        Console.WriteLine("----------  A objB = new A();  -------------");
        B objA = new B();
        objA.MethodA();
        Console.ReadLine();
    }
}

class A
{
    public A()
    {
        Console.WriteLine("Constructor of class A");
    }

    public void MethodA()
    {
        Console.WriteLine("method of A class");
    }
}

class B : A
{
    public B()
    {
        Console.WriteLine("Constructor of class B");
    }
    public void MethodB()
    {
        Console.WriteLine("method of B class");
    }
}

I have changed only B objA = new B(); and output is the same:). So what is the difference?


Posted: 10/25/2010

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

The difference is that now you have both methods accessible to the objA

B objA = new B()

objA.MethodA(); //accessible
objA.MethodB(); //accessible

previously you didn't have both of them, now you do since B inherits all the objects from A.

I hope it's clear now :)


Page 1 of 1 (4 items)