C # class, interface, virtual method, and abstract Method

Source: Internet
Author: User

Virtual and abstract methods can be rewritten by the derived classes. What is the difference between them?

1. The virtual method must have an implementation part and provide the option to override this method for the derived class. The abstract method does not provide the implementation part. The abstract method is a method that forces the override of the derived class. Otherwise, the derived class cannot be instantiated.

// Abstract Method
Public abstract class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}

// Virtual Method
Public class animal
{
Public Virtual void sleep (){}
Public Virtual void eat (){}
}

2. abstract methods can only be declared in abstract classes. abstract methods must be rewritten in Derived classes. Virtual methods are not and do not need to be rewritten. In fact, if a class contains abstract methods, the class is also abstract and must be declared as abstract.

Public class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}

In this way, the compiler reports an error.

3. the abstract method must be rewritten in the derived class. This is similar to the interface, and the virtual method is not required. Abstract METHODS cannot declare method bodies, but virtual methods can. Classes that contain abstract methods cannot be instantiated. classes that contain virtual methods can be instantiated!

Public abstract class animal
{
Public abstract void sleep ();
Public abstract void eat ();
}

Public class Cat: Animal
{
Public override void sleep ()
{
Console. writeline ("cat is sleeping ");
}
// We need implement animal. Eat () here
}

The compiler reports an error because we have not implemented all abstract methods in the abstract class.

What are the differences and links between virtual and abstract methods:

1. abstract methods must be declared without implementing code and must be implemented in subclasses. Virtual methods can be used to generate and implement code, and can be rewritten in subclasses, you can also use the default Implementation of the parent class without rewriting.

2. abstract classes cannot be instantiated (New is not allowed). They can only be instantiated to implement the derived classes of all abstract methods. Classes containing virtual methods can be instantiated.

3. There is a close relationship between the virtual method and polymorphism. The virtual method allows the derived class to completely or partially override the method of this class. You need to write the method body. An abstract method is just a definition. There is no method body, that is, no {}. Do not write content in it.

4. abstract methods are similar to virtual methods. They are overwritten.

 

Differences between non-abstract classes and interfaces

The differences between non-abstract classes and interfaces are as follows:

1. Non-abstract classes can have member variables, constants, and constructors. The interfaces cannot contain constants, fields, operators, constructors, or destructor, and cannot contain any static members.
2. A non-abstract class can inherit multiple interfaces, but can inherit only one class;
3. Non-abstract classes can define specific methods and implementations, while interfaces only define methods without specific implementations;
4. The interface members must be public and cannot be declared as virtual or static, rather than abstract classes without these restrictions.
5. Non-abstract classes can be instantiated, but interfaces cannot. Generally, the content of a non-abstract class is much richer than that of an interface. A non-abstract class can have a vast majority of object-oriented elements. However, the interface is more flexible than non-abstract classes, and there is little conflict between the two in use. It depends on the actual situation.

 

, Abstract classes and interfaces:
1. If you want to create multiple versions of a component, create an abstract class. Abstract classes provide simple methods to control component versions.
2. If the created function is used across a wide range of different objects, the interface is used. If you want to design small and concise functional blocks, use interfaces.
3. If you want to design a large functional unit, use an abstract class. If you want to provide general implemented functions among all the implementations of the component, use an abstract class.
4. abstract classes are mainly used for closely related objects. interfaces are suitable for providing general functions for irrelevant classes.

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.