You must Sign In to post a response.
  • Category: General

    What is diff bw Class1 c=new Class1() and Class1 obj=new Class2() in c#

    Hi, i ve class1 and class2. class2 inherits class1.

    What is diff bw the following.
    1. Class1 o=new class1();
    2. Class2 o=new class2();
    3. Class1 o=new class2();
    4. Class2 o=new class();
  • #753060
    Hai Arul Jesuraj,
    Please find below the differences between the statements:
    1. Class1 o=new Class1();
    In this statement, the Class1 object is created and placed at a memory location.
    2. Class2 o=new class2();
    In this statement, the Class2 object is created and placed at a memory location.
    3. Class1 o=new class2();
    In this statement, the Class2 object is created and assigned to the Class1 memory location.
    4. Class2 o=new Class1();
    In this statement, the Class1 object is created and assigned at a Class2 memory location via reference.
    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #753063
    1. Class1 o=new class1();

    In this case, first memory is allocated to all the data members of class1 and the reference to this memory location is assigned to o. where o is a reference variable of type class1. now using o you can access all the members of class1.
    2. Class2 o=new class2();
    Here , first memory is allocated to all the data members of class2 and the reference to this memory location is assigned to o. where o is a reference variable of type class2. now using o you can access all the members of class2.

    Here the twist comes when same method is defined in both parent class and child class. if child class method is defined using new. then child class method is called.
    Here base class members can be called if an instance of the derived class is cast to an instance of the base class.

    For example
    Class1 o = (Class1) new Class2();
    3. Class1 o=new class2();
    Here, you cannot use a reference to a base class to access the members and methods of a derived class even if the base class reference may contain a valid reference to an object of the derived type. here using o we can only access base class members but not class2 members.

    4. Class2 o=new class();

    the above statement is not correct it will give you an error. do you intend to do as below:
    Class2 o=new class1();

    this will give error:Cannot implicitly convert type 'Class1' to 'Class2'. An explicit conversion exists (are you missing a cast?)

    so you need to cast as follows

    Class2 o = (Class2) new Class1();
    now using o you can access both the public members of class1 and class2

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #753082
    Hi,

    I think Pawan give you the detailed explanation out of your query if you still having any doubt then please revert back.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


  • Sign In to post your comments