Abstract class discussion

Source: Internet
Author: User
Tags protected constructor
I. When to use abstract classes

An abstract class can be used when a group of related components are required to contain a group of methods with the same functions but require flexibility in other method implementations. Abstract classes are also valuable when version issues are expected, because the base classes are flexible and easy to modify. For more information, see abstract classes and interface suggestions.

For more information about abstract classes and interfaces, see
Abstract class | polymorphism in components | mustinherit (Visual Basic) | abstract (C #)
Designing a function as an interface or an abstract class (mustinherit in Visual Basic) is sometimes difficult. An abstract class is a class that cannot be instantiated but must be inherited from it. Abstract classes can be fully implemented, but more commonly, they are partially implemented or are not implemented at all, so as to encapsulate the usage functions of the inherited classes. For more information, see abstract classes.

On the contrary, an "interface" is a fully abstract set of members and can be seen as defining contracts for operations. The implementation of interfaces is left to developers.

Interfaces and abstract classes are useful for component interaction. If a method requires an interface in the form of a parameter, any object implementing this interface can be used in this parameter. For example:

'Visual basic
Public sub spin (byval widget as iwidget)
End sub
// C #
Public void spin (iwidget)
{}
This method can accept any object that implements iwidget as a widget parameter, even if the implementation of iwidget may be very different. Abstract classes also allow this polymorphism, but note the following:

Classes may only inherit from a base class, so if you want to use abstract classes to provide polymorphism for a group of classes, these classes must all inherit from that class.
Abstract classes may also provide implemented members. Therefore, you can use abstract classes to ensure that a specified number of functions are the same, but you cannot use interfaces to do so.
Here are some suggestions to help you decide whether to use interfaces or abstract classes to provide polymorphism for components.

If you want to create multiple versions of a component, create an abstract class. Abstract classes provide a simple way to control component versions. By updating the base class, all inheritance classes are automatically updated with the change. On the other hand, the interface cannot be changed once it is created. If you need a new version of the interface, you must create a new interface.
If the created function is used across a wide range of different objects, the interface is used. Abstract classes are mainly used for closely related objects, and interfaces are most suitable for providing general functions for irrelevant classes.
If you want to design small and concise functional blocks, use interfaces. If you want to design a large functional unit, use an abstract class.
If you want to provide common implemented functions among all the implementations of a component, use an abstract class. Abstract classes allow partial implementation classes, while interfaces do not include the implementation of any member.
Bytes ------------------------------------------------------------------------------------------
II. Questions from netizens

Using system;
Abstract Public class window
{
Protected int top;
Protected int left;
Public window (INT top1, int left1)
{
This. Top = top1;
This. Left = left1;
}
Abstract Public void drawwindow ();
}

Public class ListBox: Window
{
Protected string listcontents;
Public ListBox (INT top1, int left1, string contents): Base (top1, left1)
{
This. listcontents = contents;
}

Public override void drawwindow ()
{
Console. writeline (listcontents );
}
}

Public class Tester
{
Static void main ()
{
Window [] Win = new window [2];
Win [0] = new ListBox (1, 2, "fitst ");
Win [1] = new ListBox (3, 4, "second ");
For (INT I = 0; I <2; I ++)
{
Win.Drawwindow ();
}

Console. Read ();
}
}
It cannot be instantiated. Window [] Win = new window [2]; isn't this an instance?
Bytes ------------------------------------------------------------------------------------------
3. In this case

Fortunately, I consulted a patient boss.

Window [] Win = new window [2]
The actual instantiation of this sentence is an array object, but a window abstract type.

After that, you write:
Win [0] = new ListBox (1, 2, "fitst ");
Win [1] = new ListBox (3, 4, "second ");
In fact, we still put the instantiation of the derived class into the array element.

If you delete the two sentences in the new ListBox above, change them to win [0] = new window () and try again.
At this time, the compilation will fail and you will be given a clear prompt.
Bytes ------------------------------------------------------------------------------------------
4. The system summarizes the description of the abstract class in ms.

Abstract classes should not be instantiated under any circumstances. Therefore, it is very important to define their constructors correctly. It is also important to ensure the correctness and scalability of abstract classes. The following guidelines help ensure that abstract classes are correctly designed and can work as expected after implementation.

Do not define public or protected internal constructors in abstract types (protected friend in Visual Basic.
Constructors with public or protected internal visibility are types that can be instantiated. Abstract types cannot be instantiated under any circumstances.

A protected constructor or internal constructor should be defined in the abstract class.
If a protected constructor is defined in an abstract class, the base class can execute initialization tasks when creating an instance of the derived class. The internal constructor prevents abstract classes from being used as the base classes of types in other assemblies.

Each abstract class you provide should at least provide a specific inheritance type.
This helps the library designer locate the problem when designing the abstract class. At the same time, it means that developers can use specific classes without learning these concepts even if they do not understand abstract classes and inheritance.

Abstract classes have the following features:

Abstract classes cannot be instantiated.
Abstract classes can contain abstract methods and abstract accessors.
You cannot use the sealed (C # reference) modifier to modify the abstract class, which means that the abstract class cannot be inherited.
A non-abstract class derived from an abstract class must include all the inherited abstract methods and the actual implementation of the abstract accessors.
Use the abstract modifier in a method or attribute declaration to indicate that a method or attribute does not contain an implementation.

Abstract methods have the following features:

The abstract method is an implicit virtual method.
Only abstract methods can be declared in abstract classes.
The abstract method declaration does not provide actual implementation, so there is no method body. The method declaration ends with a semicolon and there is no braces ({}) after the signature ({}). For example:
The implementation is provided by an override method, which is a non-abstract class member.
It is incorrect to use the static or virtual modifier in the abstract method declaration.
Except for the differences in the declaration and call syntax, abstract attributes behave the same way as abstract methods.
It is incorrect to use the abstract modifier for static attributes.
In a derived class, you can override abstract inheritance attributes by including the attribute declaration using the override modifier.
Abstract classes must be implemented for all interface members.
Abstract classes that implement interfaces can map interface methods to abstract methods.

Qltouming)
2006-11-02

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.