Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# declare subclass as type superclass

Tags:

c#

inheritance

I can't seem to find a satisfactory answer to this inheritance question. Why do you declare a subtype like this sometimes:

Shape myCircle = new Circle();

and this other times?

Circle myShape = new Circle();

Where Circle is a child/sub-class of Shape .....

There doesn't seem to be consistency across the OO books I've been reading and an explanation I can get my head around. People have shown me examples of a Shapes class being instantiated for circles, squares, etc... But I don't understand when you use the 1st declaration and the second.

like image 257
PMS Avatar asked Jun 08 '12 15:06

PMS


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

It is often desirable to treat objects of concrete class polymorphically to avoid creating dependencies on specific types where you can avoid them. For example, if you are creating a Circle with the intention of drawing it, and Draw() is a method of a Shape, then it is better to declare myCircle as a shape, in case you decide to replace it with a square later on.

On the other hand, if your intention is to do something specific to Circle, say, to set its radius, then you need to declare myCircle as Circle, because SetRadius() may not be available on the Shape.

like image 179
Sergey Kalinichenko Avatar answered Oct 10 '22 01:10

Sergey Kalinichenko


Perhaps you have a list of shapes, and you don't particularly care what kind of shapes they are because what you need to do with that list is general to all shapes (ie draw), you would want to add them to the list like this

List<Shape> shapeList = new List<Shape>();
shapeList.Add(new Circle());
shapeList.Add(new Rectangle());
foreach (Shape shape in shapeList)
{
    shape.Draw();
}

This way you can access the general methods of shapes on objects that are actually different types but share a common parent. Then if you want to do something specific to circles you can use the same list like this.

foreach (Shape shape in shapeList)
{
    if (!(shape is Circle)) continue;
    Circle circle = shape as Circle;
    circle.Radius = 100;
}
like image 37
Kevin DiTraglia Avatar answered Oct 10 '22 01:10

Kevin DiTraglia