locked
Instantiating a derived class - need help understanding it RRS feed

  • Question

  • User661550187 posted
    Hi,

      I am reading an article online and I have come across the below code
    snippet.

    Public Sub ReturnBook(ByVal dueDate As DateTime)

            Dim myBook As LibraryAsset = New Book

            myBook.DueDate = dueDate

            Dim amountDue As Double = myBook.CalculateFineTotal()

            Console.WriteLine("Book: {0}", amountDue.ToString())

        End Sub

     I am kind of not sure if I understand the line of code Dim myBook As
    LibraryAsset = New Book right. In which LibraryAsset is the base class
    and Book is the derived class. LibraryAsset class is a MustInherit
    class with a overridable function in it.

    Instead of the line Dim myBook As LibraryAsset = New Book can't we
    code like

    Dim myBook As Book = New Book ? Why do we have to assign a New Book
    instance to LibraryAsset?

    In other words if I have a base class X and a derived class Y inorder
    for me to instantiate Y in another class do I have to use

    Dim y1 as X = New Y but not Dim y1 as Y = New Y?

    Please explain this. I am bit new to OOP techniques.

    Thanks,

    -L

    Wednesday, May 6, 2009 10:09 PM

Answers

  • User-1473675079 posted

    Ok what you are seeing here is a technique called polymorphism.

    To answer your last question first yes you can instantiate a the class as

    Dim y1 As Y = New Y();

    But consider these classes.

    public class Person

    {

    public virtual string Occupation()

    {

    return "No Occupation";

    }

    }

    public class Fireman : Person

    {

    public override string Occupation()

    {

    return "Fireman";

    }

    }

    public class Policeman : Person

    {

    public override string Occupation()

    {

    return "Policeman";

    }

    }

     You can then do the following when using the classes.

    Person theperson;

    theperson = new Fireman();

    Response.Write(theperson.Occupation());

    theperson =
    new Policeman();

    Response.Write(theperson.Occupation());

     

    This will write "FiremanPoliceman" 

     

    Note we declare theperson variable as Person.

     

    But by instantiating an instance of the derived classes Fireman or Policeman into it we can get the different implementations of the Occupation method.

     

    Consider this function

    int Salary(Person thePerson)

    {

    if (thePerson.Occupation() == "Fireman")

    return 50000;

    else if (thePerson.Occupation() == "Policeman") return 60000;

    else

    return 0;

    }

     

    We can call this function and pass into it different derived class instances but we need to know nothing about the derived class in the Salary functions implementation.

     

    Hope this helps. 

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 11:10 PM
  • User-1473675079 posted

    Both your above examples produce exactly the same result.  The second example is the typically way of instantiating a class.  You use the first method when you want to use polymorphic behavior in your classes.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2009 7:05 PM

All replies

  • User-1473675079 posted

    Ok what you are seeing here is a technique called polymorphism.

    To answer your last question first yes you can instantiate a the class as

    Dim y1 As Y = New Y();

    But consider these classes.

    public class Person

    {

    public virtual string Occupation()

    {

    return "No Occupation";

    }

    }

    public class Fireman : Person

    {

    public override string Occupation()

    {

    return "Fireman";

    }

    }

    public class Policeman : Person

    {

    public override string Occupation()

    {

    return "Policeman";

    }

    }

     You can then do the following when using the classes.

    Person theperson;

    theperson = new Fireman();

    Response.Write(theperson.Occupation());

    theperson =
    new Policeman();

    Response.Write(theperson.Occupation());

     

    This will write "FiremanPoliceman" 

     

    Note we declare theperson variable as Person.

     

    But by instantiating an instance of the derived classes Fireman or Policeman into it we can get the different implementations of the Occupation method.

     

    Consider this function

    int Salary(Person thePerson)

    {

    if (thePerson.Occupation() == "Fireman")

    return 50000;

    else if (thePerson.Occupation() == "Policeman") return 60000;

    else

    return 0;

    }

     

    We can call this function and pass into it different derived class instances but we need to know nothing about the derived class in the Salary functions implementation.

     

    Hope this helps. 

     

     

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 6, 2009 11:10 PM
  • User661550187 posted

    Thanks for the example. But the point I would like to understand is in your code instead of doing as

     Person theperson;

    theperson = new Fireman();

    Response.Write(theperson.Occupation());

     

    what result do we get when we do as follows

    Fireman theperson;

    theperson = new Fireman();

    Response.Write(theperson.Occupation());

     

    Thanks,

     

    -L

    Thursday, May 7, 2009 8:47 AM
  • User-1473675079 posted

    Both your above examples produce exactly the same result.  The second example is the typically way of instantiating a class.  You use the first method when you want to use polymorphic behavior in your classes.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 7, 2009 7:05 PM