Skip to content

Conversation

@BillWagner
Copy link
Member

Fixes #5523

The inheritance tutorial did not discuss the differences between an abstract class with abstract methods and an abstract class that did not include abstract methods.

@BillWagner BillWagner requested a review from rpetrusha July 5, 2018 15:13
Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, @BillWagner. Left a number of comments that I hope help to improve it further.


- Whether it makes sense to instantiate the base class. If it does not, we should apply the [abstract](../language-reference/keywords/abstract.md) keyword to the class. If an attempt is made to instantiate a class marked with the `abstract` keyword by a direct call to its class constructor, the C# compiler generates error CS0144, "Cannot create an instance of the abstract class or interface." If an attempt is made to instantiate the class by using reflection, the reflection method throws a <xref:System.MemberAccessException>. Otherwise, our `Publication` class can be instantiated by calling its class constructor.

By default, a base class can be instantiated by calling its class constructor. Note that we do not have to explicitly define a class constructor. If one is not present in the base class' source code, the C# compiler automatically provides a default (parameterless) constructor.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit for line 82: I had to think about what "Otherwise" referred to and read the paragraph several times (it complements the "If" in the second sentence." I think that the Otherwise sentence can be moved up to follow the If sentence, and the paragraph still reads well.

For our example, we'll mark the `Publication` class as [abstract](../language-reference/keywords/abstract.md) so that it cannot be instantiated.
For our example, we'll mark the `Publication` class as [abstract](../language-reference/keywords/abstract.md) so that it cannot be instantiated. The `Publication` class does not have any `abstract` methods, but the class itself is `abstract`. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`).

- Whether derived classes must inherit the base class implementation of a particular members, or whether they have the option to override the base class implementation. We have to use the [virtual](../language-reference/keywords/virtual.md) keyword to allow derived classes to override a base class method. By default, methods defined in the base class are *not* overridable.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to your PR: a particular members --> particular members

By default, a base class can be instantiated by calling its class constructor. Note that we do not have to explicitly define a class constructor. If one is not present in the base class' source code, the C# compiler automatically provides a default (parameterless) constructor.

For our example, we'll mark the `Publication` class as [abstract](../language-reference/keywords/abstract.md) so that it cannot be instantiated.
For our example, we'll mark the `Publication` class as [abstract](../language-reference/keywords/abstract.md) so that it cannot be instantiated. The `Publication` class does not have any `abstract` methods, but the class itself is `abstract`. An `abstract` class without any `abstract` methods indicates that this class represents an abstract concept that is shared among several concrete classes (like a `Book`, `Journal`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a bit abrupt, and although it addresses the issue of an abstract class without abstract methods, it doesn't say what an abstract class with abstract methods is. Since we're discussing design decisions, that seems like a major omission. (It's also a major area of confusion.)

I think that what I would do is move the discussion of abstract methods to the next bulleted item, so that the choice becomes inheriting the base class implementation, optionally overriding the base class implementation, or being required to provide an implementation. Note that this can be done on a method-by-method basis, except that if any members are abstract, the type must also be abstract. The abstract member discussion here can be succinct, with a forward-reference to the next section.

## Designing abstract base classes and their derived classes
<a name="abstract"></a>

In the previous example, we defined a base class that provided an implementation for a number of methods to allow derived classes to share code. In many cases, however, the base class is not expected to provide an implementation. Instead, the base class is an *abstract class*; it serves as a template that defines the members that each derived class must implement. Typically in the case of an abstract base class, the implementation of each derived type is unique to that type.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's confusing here is that, since we applied the abstract keyword to the Publication class in the last section, it would seem to be an abstract class. I'd add a one sentence qualification after the first sentence, something like, "We marked the class with the abstract keyword because it made no sense to instantiate a Publication object, although the class did provide implementations of functionality common to publications."

In addition to suggestions:
. update ms.date
. run acrolinx.
. second pass at distinctions on abstract classes and abstract methods.
@BillWagner
Copy link
Member Author

@rpetrusha Thanks for the suggestions. That did lead to a better explanation of the abstract base class and abstract methods.

Copy link
Contributor

@rpetrusha rpetrusha left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This topic is really good now, @BillWagner. It's ready to merge when you're ready.

@BillWagner BillWagner merged commit e6b9727 into dotnet:master Jul 5, 2018
@BillWagner BillWagner deleted the update-abstract-explanation branch July 5, 2018 21:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants