Keywords : Virtual vs Override vs New

The purpose of this article is to explain the difference between  Virtual, Override and New keywords.

These three keywords have one thing in common is that they all are used to achieve some sort of polymorphism technique in OOO design.

1. Virtual :  This keyword is used for methods when we want the methods in base class  to be overridden and give different implementation to it.

Example: In the above code sample, if we don’t want the default color “Red” then we can override in the base class to provide a different color “Black”.

public class Shape {
  private string _color = null;
  public virtual void DrawColor() {
    _color = "Red";
  }
}
public Class CircleShape : Shape {
  private string _color = null;
  public override void DrawColor() {
    _color = "Black";
  }
}

2. Override : This keyword is used to change the implementation of base class method in the derived class methods. The above code snippet shows the usage of “override” keywrod.

3. New : This keyword is used to change the behavior of the base class method when there is no Virtual keyword is provided.

public class Shape {
  private string _color = null;
  public void DrawColor() {
    _color = "Red";
  }
}
public Class CircleShape : Shape {
  private string _color = null;
  public new void DrawColor() {
    _color = "Black";
  }
}

Note: The keyword “new” is not recommended to be used as this will hide the implementation of base class.

Advertisements

Install Windows 7/8/8.1 on MAC Air

Recently, I  had bought a Mac Air and I wanted to install Windows for specific reasons.  There are multiple ways to install Windows OS on iOS.

1. Make a partition and install Windows as a separate OS, which means that boot loader would give you an option to choose to start either MAC or Windows.

2. Use Virtual Machine. This can be achieved using Virtual Box from Oracle (free), or Parallel Desktop.

Without doing much of research, I have ended up choosing the Virtual Box option to try it out. Because of few simple reason. The above Option 1 would have a problem of switching between the OS (iOS and Windows) without restarting your computer. And the Parallel Desktop, obvious reason to eliminate this option is is due to not being a free download.

I was so amazed to successfully compete the Windows installation on MAC in couple of minute, I mean literally in couple of minutes. So hats-off to Virtual Box.