2

let's say Polygon is our base class and Square is our derived class and we want to make an instance of a derived class like this:

Polygon polygon = new Square(4.5f);

the above example is from a book which says "it is possible to be actually using an instance of a derived type like the Square, but only know that it is a Polygon"

now my questions are :

1- in which cases a derived class could be a base class? I mean in the situation when computer treat derived class as the base class without we know about it or it is not our intention.

2- why we should pass a value like "4.5f " with derived class ?why we can't treat it like when we make an instance of normal class and write:

Polygon polygon = new Square();
mohsen doraghi
  • 59
  • 2
  • 10
  • 3
    The first part is probably answered by https://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to-an-interface. The second part is presumably "because the Square constructor has a float parameter" - it has nothing to do with derived classes at all. You can't use `new Foo()` if `Foo` doesn't have a parameterless constructor (or a constructor with only optional parameters or a parameter array...) – Jon Skeet Jun 08 '16 at 10:13
  • thanks , I'll read it later , but are you sure it has something to do with the interface? I didn't get the answer for the second part .why we should pass a constant value like 4.5 not a variable with datatype like "int numbers"? – mohsen doraghi Jun 08 '16 at 10:28
  • It's the same principle, just with base classes instead of interfaces. And yes, you *did* get an answer to the second part, but you didn't ask about constants vs variables... it would be fine to pass a `float` variable instead of a constant there. – Jon Skeet Jun 08 '16 at 10:34
  • I meant I couldn't understand the second part .because as I can see Square is a class, not a constructor if I'm not mistaking it's not even a method I'm not sure what happen to 4.5 after you pass it to a class?I never saw before you pass the parameter to a class ,which part of my assumption is wrong? – mohsen doraghi Jun 08 '16 at 10:48
  • Yes, Square is a class, but you're calling a constructor. If you don't know what a constructor is yet, now is the time to go back to a tutorial or book - while Stack Overflow is great for filling in specific holes in knowledge, it's not a good place to learn the basics of a language, as a book can teach you in a much more structured, ordered way., – Jon Skeet Jun 08 '16 at 10:52
  • I know what constructor is but the author of the book said it is the "instance of a derived class" , but I'll search for it .thanks – mohsen doraghi Jun 08 '16 at 11:10
  • *What* is the "instance of a derived class"? The result of the constructor call is... – Jon Skeet Jun 08 '16 at 11:48
  • Thanks , I got it. – mohsen doraghi Jun 08 '16 at 14:20

1 Answers1

1

Starting by the easiest one, number 2:

In that case, you're creating an instance of Square new Square(4.5f) because Square, which could be a Class or Struct defines a constructor signature with a float as parameter.

Square() being a struct (Structs are light-weight objects, also a bit different from classes), all the public fields has to be initialized in its constructor which has parameters, otherwise the compiler will throw a compiler error if you don't initialize the public fields. however, structs has a implicitly parameterless constructor defined which could be used.

The compiler provides a default, parameterless constructor signature if none, only if none constructor is defined (It only applies on classes).

To brief it, if you don't have any other constructor overload, you'll have to instantiate that object passing a float as argument. In that case I assume its a class. If it was a struct, as I said, Structs has a implicitly parameterless constructor defined so the below would be valid.

Polygon polygon = new Square();

Now lets go back to the first question:

This one is related to inheritance and polymorphism.

In C# a class can inherit from a single class and many interfaces, not more and struct can only inherit interfaces. As C# only allows a class to inherit from a single class, in other hand it allows you to have nested inheritance: class A : B : C : D and so on.

Polymorphism infers that a class can take many forms. It works in the basis that a derived class has all the features it's base class.

In C# the usage of public member are determined statically (compiter time rather at runtime) so when a derived object takes the form of its base object, it only can access the member its base defines. Also in the case of using a base in a method signature as parameter, it could receive different implementations of itself.

I'd suggest you to read this book: C# 6.0 Pocket Reference, by Joseph Albahari & Ben Albahari

Sorry if I complicated the things :)

Community
  • 1
  • 1
Alexz S.
  • 2,366
  • 4
  • 21
  • 34