Hi Guys
NP85 new keyword
In this program what is the purpose of using new keyword because without new keyword program is producing similar output. Anyone knows please explain the reason.
Thank you
namespace Shapes
{
using System;
// Abstract base class.
public abstract class Shape
protected string petName;
// Constructors.
public Shape() { petName = "NoName"; }
public Shape(string s)
this.petName = s;
}
// All child objects must define for themselves what it means to be drawn.
public abstract void Draw();
public string PetName
get { return this.petName; }
set { this.petName = value; }
#region rerived types.
public class Circle : Shape
public Circle() { }
// Call base class constructor.
public Circle(string name) : base(name) { }
public override void Draw()
Console.WriteLine("Drawing " + PetName + " the Circle");
public class Oval : Circle
public Oval() { base.PetName = "Joe"; }
// Hide base class impl if they create an Oval.
new public void Draw()
Console.WriteLine("Drawing an Oval using a very fancy algorithm");
public class Hexagon : Shape
public Hexagon() { }
public Hexagon(string name) : base(name) { }
Console.WriteLine("Drawing " + PetName + " the Hexagon");
#endregion
public class ShapesApp
public static int Main(string[] args)
// The C# base class pointer trick.
Console.WriteLine("***** Using an array of base class references *****");
Shape[] s = {new Hexagon(), new Circle(), new Hexagon("Mick"),
new Circle("Beth"), new Hexagon("Linda")};
for (int i = 0; i < s.Length; i++)
s[i].Draw();
// Oval hides the base class impl of Draw()
// and calls its own version.
Console.WriteLine("\n***** Calling Oval.Draw() *****");
Oval o = new Oval();
o.Draw();
return 0;
/*
***** Using an array of base class references *****
Drawing NoName the Hexagon
Drawing NoName the Circle
Drawing Mick the Hexagon
Drawing Beth the Circle
Drawing Linda the Hexagon
***** Calling Oval.Draw() *****
Drawing an Oval using a very fancy algorithm
*/