The following example shows an abstract type that violates this rule.
[C#]
using System;
namespace Samples
{
// Violates this rule
public abstract class Book
{
public Book()
{
}
}
}
[Visual Basic]
Imports System
Namespace Samples
' Violates this rule
Public MustInherit Class Book
Public Sub New()
End Sub
End Class
End Namespace
[C++]
using namespace System;
namespace Samples
{
// Violates this rule
public ref class Book abstract
{
public:
Book()
{
}
}
}
The above abstract type has a public constructor, which can confuse new users. They see the public constructor, but do not understand why they are unable to create the type.
The following example fixes the above violation by changing the constructor's accessibility from public to protected.
[C#]
using System;
namespace Samples
{
public abstract class Book
{
protected Book()
{
}
}
}
[Visual Basic]
Imports System
Namespace Samples
Public MustInherit Class Book
Protected Sub New()
End Sub
End Class
End Namespace
[C++]
using namespace System;
namespace Samples
{
public ref class Book abstract
{
protected:
Book()
{
}
}
}