locked
How to use derived class object via base class? RRS feed

  • Question

  • Hi all

    Here is my code:

    class Happybirthday
        {
            private string birthdayMessage;
            private string presentString;
    
            public Happybirthday()
            {
                presentString = "Number of presents: ";
                birthdayMessage = "Happy Birthday ";            
            }        
    
            public string getmessage(string givenname)
            {
                return birthdayMessage + givenname + '\n';
            }
    
            public string getPresents(int numPresents)
            {
                presentString = presentString + numPresents.ToString() + '\n';
                return presentString;
            }
    
        }
    
        class birthdayParty : Happybirthday
        {
            public birthdayParty(): base()
            {
            }
    
            public string getParty(bool haveparty)
            {
                if (haveparty == true)
                    return "Enjoy your party!" + '\n';
                else
                    return "Sorry - No party for you!" + '\n';
            }
        }
    
        class guest:Happybirthday
        {
            private string msg;
            public guest():  base()
            {
                msg = " gonna rock you today";
            }
            public string getguestmsg(string name)
            {
                return name + msg + '\n';
            }
    
        }

    and i am using this in my form

    Happybirthday birthdymsg = new Happybirthday();
    
    MessageBox.Show(birthdymsg.getmessage("SHREERAM"));//working
    MessageBox.Show(birthdymsg.getPresents(8));//working
    
    MessageBox.Show(birthdymsg.getParty(true));//not working

    how to access derived class members with creating a base class object?

    please reply asap

    thanks in advance


    Hemant Kaushik

    Sunday, July 6, 2014 4:28 AM

Answers

  • Hi Hermant,

    you've created an object of your base-class. So there's no getParty-method available. It's only available in a birthdayParty-object. The following would work:

      birthdayParty birthdymsg = new birthdayParty();
    
                MessageBox.Show(birthdymsg.getmessage("SHREERAM"));//working
                MessageBox.Show(birthdymsg.getPresents(8));//working
    
                MessageBox.Show(birthdymsg.getParty(true));// working

    You could also still create your birthdymsg-Variable of type HappyBirthday. That allows you to assign also objects of subclasses to it. But to access methods of a subclass, you need to do a cast then. This code shows you that variant:

       Happybirthday birthdymsg = new birthdayParty();
    
                MessageBox.Show(birthdymsg.getmessage("SHREERAM"));//working
                MessageBox.Show(birthdymsg.getPresents(8));//working
    
                MessageBox.Show(((birthdayParty)birthdymsg).getParty(true));// working
    Btw: Class-names are usually Pascal-cased in .NET => means TheStartWithAnUpperCaseCharacter.


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook



    Sunday, July 6, 2014 9:04 AM
  • >>how to access derived class members with creating a base class object?

    Short answer: You can't. The would defeat the whole purpose of object oriented inheritance.

    The Happybirthday base class doesn't know anything about any derived classes. If you want to invoke a method that is specific for the derived birthdayParty class, the type of the variable must be birthdayParty and not Happybirthday. You can only call the methods that are defined in the Happybirthday base class on an object of type Happybirthday.

    • Marked as answer by Anne Jing Monday, July 14, 2014 9:43 AM
    Sunday, July 6, 2014 6:44 PM

All replies

  • Hi Hermant,

    you've created an object of your base-class. So there's no getParty-method available. It's only available in a birthdayParty-object. The following would work:

      birthdayParty birthdymsg = new birthdayParty();
    
                MessageBox.Show(birthdymsg.getmessage("SHREERAM"));//working
                MessageBox.Show(birthdymsg.getPresents(8));//working
    
                MessageBox.Show(birthdymsg.getParty(true));// working

    You could also still create your birthdymsg-Variable of type HappyBirthday. That allows you to assign also objects of subclasses to it. But to access methods of a subclass, you need to do a cast then. This code shows you that variant:

       Happybirthday birthdymsg = new birthdayParty();
    
                MessageBox.Show(birthdymsg.getmessage("SHREERAM"));//working
                MessageBox.Show(birthdymsg.getPresents(8));//working
    
                MessageBox.Show(((birthdayParty)birthdymsg).getParty(true));// working
    Btw: Class-names are usually Pascal-cased in .NET => means TheStartWithAnUpperCaseCharacter.


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest app: The "Womanizer" :-)
    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook



    Sunday, July 6, 2014 9:04 AM
  • >>how to access derived class members with creating a base class object?

    Short answer: You can't. The would defeat the whole purpose of object oriented inheritance.

    The Happybirthday base class doesn't know anything about any derived classes. If you want to invoke a method that is specific for the derived birthdayParty class, the type of the variable must be birthdayParty and not Happybirthday. You can only call the methods that are defined in the Happybirthday base class on an object of type Happybirthday.

    • Marked as answer by Anne Jing Monday, July 14, 2014 9:43 AM
    Sunday, July 6, 2014 6:44 PM