Showing posts with label Polymorphism. Show all posts
Showing posts with label Polymorphism. Show all posts

Knowing When to Use Override and New Keywords (C# Programming Guide)


Knowing When to Use Override and New Keywords (C# Programming Guide)

First we declare three classes: a base class called Car, and two classes that derive from it, ConvertibleCar and Minivan. The base class contains a single method, DescribeCar, which sends a description of the car to the console. The derived class methods also include a method called DescribeCar, which displays their unique properties. These methods also call the base class DescribeCar method to demonstrate how they have inherited the properties of the Car class.
In order to highlight the difference, the ConvertibleCar class is defined with the new keyword, while the Minivan class is defined with override.
class Car
{
    public virtual void DescribeCar()
    {
        System.Console.WriteLine("Four wheels and an engine.");
    }
}
 
// Define the derived classes
class ConvertibleCar : Car
{
    public new virtual void DescribeCar()
    {
        base.DescribeCar();
        System.Console.WriteLine("A roof that opens up.");
    }
}
 
class Minivan : Car
{
    public override void DescribeCar()
    {
        base.DescribeCar();
        System.Console.WriteLine("Carries seven people.");
    }
}

We can now write some code that declares instances of these classes, and calls their methods so that the objects can describe themselves:

public static void TestCars1()
{
    Car car1 = new Car();
    car1.DescribeCar();
    System.Console.WriteLine("----------");
 
    ConvertibleCar car2 = new ConvertibleCar();
    car2.DescribeCar();
    System.Console.WriteLine("----------");
 
    Minivan car3 = new Minivan();
    car3.DescribeCar();
    System.Console.WriteLine("----------");
}
As you might expect, the output looks like this:
Four wheels and an engine.
----------
Four wheels and an engine.
A roof that opens up.
----------
Four wheels and an engine.
Carries seven people.
----------
However, in this next section of code, we declare an array of objects derived from the Car base class. This array can store Car, ConvertibleCar, and Minivan objects. The array is declared like this:
public static void TestCars2()
{
    Car[] cars = new Car[3];
    cars[0] = new Car();
    cars[1] = new ConvertibleCar();
    cars[2] = new Minivan();
}
 
foreach (Car vehicle in cars)
{
    System.Console.WriteLine("Car object: " + vehicle.GetType());
    vehicle.DescribeCar();
    System.Console.WriteLine("----------");
}

The output from this loop is as follows:
Car object: YourApplication.Car
Four wheels and an engine.
----------
Car object: YourApplication.ConvertibleCar
Four wheels and an engine.
----------
Car object: YourApplication.Minivan
Four wheels and an engine.
Carries seven people.
----------
Notice how the ConvertibleCar description is not what you might expect. As the new keyword was used to define this method, the derived class method is not called—the base class method is called instead. The Minivan object correctly calls the overridden method, producing the results we expected.
If you want to enforce a rule that all classes derived from Car must implement the DescribeCar method, you should create a new base class that defines the method DescribeCar as abstract. An abstract method does not contain any code, only the method signature. Any classes derived from this base class must provide an implementation of DescribeCar

What's the benefit i get if i use Method Overriding ?


What's the benefit i get if i use Method Overriding ?


The derived class or sub class can have its own business logic in it..

Say for example, considering a real time example..


Let us say we have a class where some features of bank transactions are 
defined..

Now, we want to write our own bank transactions using already existing
 methods defined in the class but extending its behaviiour and with some 
modifications...

this is the sitaution where we inherit the class and overrride the 
methods which are required to suit our business requirement...


method overriding will be used to redefine the base class methods 
functionality in child classes... so base class and child clas 
method have different functioality.... It will work differently 
depends on the object used to call that one...

In some situations we want to have a class that has types(specially methods) that can possibly be modified in the derived class. In such situation we declare the method in the base class as virtual and precede the same method in the derived class with 'override' keyword
Consider the following code in which situation is that, a base class method Area() returns square of a parameter and suppose we want to extend this method in the derived class sothat this square may just be multiplied by a constant pi(3.142) so that it can return area of a circle:

What's the benefit i get if i use Method Overriding ?


What's the benefit i get if i use Method Overriding ?


The derived class or sub class can have its own business logic in it..

Say for example, considering a real time example..


Let us say we have a class where some features of bank transactions are
 defined..

Now, we want to write our own bank transactions using already existing
 methods defined in the class but extending its behaviiour and with some
 modifications...

this is the sitaution where we inherit the class and overrride the
 methods which are required to suit our business requirement...


method overriding will be used to redefine the base class methods 
functionality in child classes... so base class and child clas method 
have different functioality.... It will work differently depends on the
 object used to call that one...

In some situations we want to have a class that has types(specially methods) that can possibly be modified in the derived class. In such situation we declare the method in the base class as virtual and precede the same method in the derived class with 'override' keyword
Consider the following code in which situation is that, a base class method Area() returns square of a parameter and suppose we want to extend this method in the derived class sothat this square may just be multiplied by a constant pi(3.142) so that it can return area of a circle:

How override works


I'm learning about the virtual table in association with virtual methods. I got most of the logic and understanding down (I believe) for when you use 'override' and no 'override' or no 'new' modifier. What I'm trying to figure out now is what happens when you do use modifier 'new' on a derived class method inherited from the base class?

My understanding for the override mechanism with the following code:

class Base
{
public virtual void Method( )
{
}
}

class Derived : Base
{
public override void Method( )
{
}
}


...is that the vtable will replace the Base.Method() address by making the slot in the array of pointers (where Base.Method resides) point to the new override method declared in the Derived class. Correct?

My understanding for when you do not 'override' or 'new' the base method in the derived class is this: The vtable will not change where the Base.Method address resides in the the slot in the array of pointers, of the vtable, so, we'll be using the same base method. Yes?

Now to my main question: what happens when you do use the 'new' modifier on the derived method? How will the vtable look?


vtable, is a mechanism used in a programming language to support dynamic dispatch (or run-time method binding).
Suppose a program contains several classes in an inheritance hierarchy: a superclass, Cat, and two subclasses, HouseCat and Lion. Class Cat defines a virtual function named speak, so its subclasses may provide an appropriate implementation (i.e., either meow or roar).
When the program calls the speak method on a Cat pointer (which can point to a Cat class, or any subclass of Cat), the run-time environment must be able to determine which implementation to call, depending on the actual type of object that is pointed to.