1

I know we cannot create an abstract class instance, but I cannot understand why could use base invoke the constructor of the abstract class.

abstract class Fruit
{
    public string Name { get; private set; }

    public Fruit(string name)
    {
        Name = name;
    }
}

class Apple : Fruit
{
    public Apple(string name) : base(name) { }
}


Fruit f = new Fruit("Fruit");  // Coimple Error
Apple a = new Apple("Apple");  // Success
  • Dose that base keyword just invoke constructors, methods, etc?

  • What's the differences between create an instance and invoke a constructor?

Thanks in advance.

Lei Kan
  • 487
  • 7
  • 20
  • 2
    Read [`base` (C# Reference)](http://msdn.microsoft.com/en-us/library/hfw7t1ce%28v=vs.120%29.aspx) first. – Soner Gönül Sep 29 '14 at 07:41
  • 1
    The *only* code that can invoke the constructor is a `base` call for a constructor of a derived class. Beyond that, I'm not actually sure what you're asking. – Damien_The_Unbeliever Sep 29 '14 at 07:42
  • 1
    Possible duplicate: http://stackoverflow.com/questions/5601777/constructor-of-an-abstract-class-in-c-sharp – James Sep 29 '14 at 07:44
  • @judder Yes, he answered the question, but he did not answer why. And question 2. – Lei Kan Sep 29 '14 at 07:50
  • @Soner Gönül I've been read this, but I want to know the detail about question 2. – Lei Kan Sep 29 '14 at 07:51
  • Eric Lippert's answer answers your question pretty well on the linked question by @judder. – Dan Sep 29 '14 at 07:53
  • I dont have enough confidence to post this as an answer but pretty sure the difference between creating an instance and invoking a constructor is that creating the instance actually creates the object ie assigns memory and so forth. The constructor is 'a little extra' that bootstraps the instance into a usable state as defined by the programmer. – Dan Sep 29 '14 at 07:54
  • @Damien_The_Unbeliever the detail about question 2 – Lei Kan Sep 29 '14 at 07:55

4 Answers4

2

Only derived class (e.g. Apple) can call the constructor of its parent (abstract class) with special base word. Constructor cannot be invoked (called) directly.

Uros Majeric
  • 416
  • 6
  • 10
1

Does that base keyword just invoke constructors, methods, etc?

No, use it anytime you want to explicitly invoke the parent class' methods and avoid invoking a override in the derived class. Though : base(...) syntax is exclusive to constructors, usually you would call base.method();

What are the differences between create an instance and invoke a constructor?

Creating an instance with the new operator does a number of things:

  • Allocates memory for the object
  • Initialises fields
  • Then finally the constructor is invoked, which will invoke base constructor first if specified.

A more in-depth explanation of the order is in this answer: https://stackoverflow.com/a/1882778/360211 but that should be enough to explain the difference.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
1

I would add that the fact that an abstract class may provide a constructor doesn't mean that it's not yet abstract.

By definition, an abstract class is a class where some or none of its members don't provide a default implementation, and derived classes must provide an implementation to these members. In the other hand, since an abstract class has some of its members as just signatures - the whole abstract members -, code mustn't be able to instantiate that class.

But if a derived class - either abstract or concrete - couldn't be able to call a base abstract class constructor, abstract classes would lack polymorphic constructors and there may be no way to initialize class properties or define a default class initialization code, even if that code calls an abstract method or property.

This is why a derived class can call a parent class constructor, even if the class is abstract!

What's the differences between create an instance and invoke a constructor?

We might try to address this question with a deep explanation with low-level details, but I feel that it's more a conceptual issue rather than a low-level thing.

If you want a summary, calling the constructor is a part of class instantiation process. It's a method which is called once the instance has been created and initializes the instances with custom code before any other code might use that instance.

When you use base keyword in a constructor to call parent's class one, you're just chaining constructor calls from the most derived class to the base class.

Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

Creating an instance with the new keyword creates a new object and returns a reference to the object.Invoking the constructor with the base keyword won't create a reference to the object(neither it will create an actual object), it will simply execute the code in the constructor.

Take a deep look onto this answer to for more information https://stackoverflow.com/a/14453366/3789232

Community
  • 1
  • 1
Christo S. Christov
  • 2,268
  • 3
  • 32
  • 57
  • I don't think that this answers the question very well and it does not go into sufficient depth before giving a link to another answer – Dan Sep 29 '14 at 07:57