Virtual methods, abstract classes

Source: Internet
Author: User

Virtual method:

1. The virtual method indicates that this method can be rewritten, that is, the method is polymorphic. A method in a parent class is a generic method that can be overridden in a subclass to re-specify the method logic.
2, virtual method can be used directly, and the same as the normal method
3, not must be rewritten. Subclasses can be called using the base. method, regardless of whether the subclass uses override to override the

The virtual keyword just explicitly indicates that this method can be rewritten, but it is no different from the general approach
The corresponding
Sealed keyword indicates that this method cannot be overridden

The difference between a virtual method and an abstract method:

1. Virtual methods can have an implementation body, and abstract methods cannot have implementations.

2. Abstract methods
Requires that derived classes must overload this method;


Virtual method
Tell the compiler that this method can be overridden.
When a method of a base class has the same name as a method of a derived class,
You can select a method of a derived class to inherit from and override the base class.
Or, hide the methods of the base class.

This paragraph from CSDN blog: http://blog.csdn.net/lzp_lrp/article/details/8972509

When a method declaration in a class is preceded by the virtual modifier, we call it a C # virtual method, whereas a non-virtual. After the virtual modifier is used, the static, abstract, or override modifiers are not allowed. For a non-virtual method, the method does not change whether it is called by an instance of its class or by an instance of the class's derived class. For virtual methods, the way in which it is executed can be changed by the derived class, which is implemented by overloading the method.

The following example illustrates the difference between a C # virtual method and a non-virtual method.

Example:

  1. Using System;
  2. Class A
  3. {
  4. Public void F () {Console.WriteLine ("A.F");}
  5. Public virtual void G () {Console.WriteLine ("A.G");}
  6. }
  7. Class B:a
  8. {
  9. New public void F () {Console.WriteLine ("B.f");}
  10. Public override void G () {Console.WriteLine ("B.G");}
  11. }
  12. Class Test
  13. {
  14. static void Main ()
  15. {
  16. b b=new B ();
  17. A a=b;
  18. A.F ();
  19. B.f ();
  20. A.G ();
  21. B.G ();
  22. }
  23. }

Example, Class A provides two methods: non-virtual F and C # virtual methods G. Class B provides a new non-virtual method F, which overrides the inherited F; Class B also overloads the inherited method G. Then the output should be:

A.f

B.f

B.g

B.g

Notice in this example, method A. G () actually called B.G, not A.G. This is because the compile value is a, but the run-time value is B, so B completes the actual call to the method.

Virtual method Definition:
Simply put, a virtual method is a method that can be overridden by a quilt class, and if a subclass overrides a virtual method, the runtime uses the rewritten logic and, if there is no Override, uses the logic of the virtual method in the parent class;
The virtual keyword is used to decorate a method, property, indexer, or event declaration and to allow these objects to be overridden in a derived class.


Using System;

Class A

{

public void F ()
{
Console.WriteLine ("A.F");
}

public virtual void G ()
{
Console.WriteLine ("A.G");
}

}

Class B:a

{

New public void F ()
{
Console.WriteLine ("B.f");
}

public override void G ()
{
Console.WriteLine ("B.G");
}

}

Class Test

{

static void Main ()

{
b b = new B ();

A = b;

A.F ();

B.f ();

A.G ();

B.G ();

}

The output is: A.F b.f b.g B.Gabstract class Definition:
its function is to produce subclasses while giving the applies class some specific properties and methods.
the abstract modifier can be used with classes, methods, properties, indexers, and events. Use the abstract modifier in a class declaration to indicate that a class can be a base class only for other classes. A member that is marked as abstract or contained in an abstract class must be implemented by a class derived from an abstract class. Features:
1. Abstract classes cannot be instantiated;
2. Abstract classes can contain abstract methods and abstract accessors;
3. The abstract class cannot be modified with the sealed modifier, which means that the abstract class cannot be inherited;
4. A fly abstract class derived from an abstract class must include all inherited abstract methods and implementations of the abstract accessors.

Summarize:
~ Abstract methods are implicit virtual methods;
~ Only the use of abstract method declarations in abstract classes is allowed;
~ because the abstract method declaration does not provide the actual implementation, there is no method body; The method declaration ends with a semicolon, and there is no curly brace "{}" after the signature, and the implementation is provided by a heavy method, which is a member of the non-abstract class;
~ using static or virtual modifiers in an abstract method declaration is wrong;
~ except in the syntax of Declaration and invocation, the behavior of abstract attributes is the same as that of abstract methods;
~ Using the Absteact modifier on a static property is an error;

~ in a derived class, you can override an abstract inherited property by including a property declaration that uses the override modifier.


Using System;
Abstract class A
{
public abstract void F ();
protected int _x;
public abstract int X
{
Get
Set
}
}
Class B:a
{
public override void F ()
{

}
public override int X
{
Get{return _x;}
Set{_x=value;}
}
}
Class Test
{
static void Main ()
{
b b=new B ();
b.x=10;
Console.Write (b.x);
}
}

The difference between an abstract method and a virtual method:

~ The difference between an abstract method and a virtual method is that the virtual method has an implementation part and provides an option for the derived class to overwrite the method, whereas the abstract method does not provide the implementation part, forcing the derived class to override the method (otherwise the derived class cannot be a concrete class);
The ~abstract method can only be declared in an abstract class, but the virtual method is not;
The ~abstract method must be overridden in a derived class, while virtual does not have to;
The ~abstract method cannot declare a method entity, but a virtual method can.

Virtual methods, abstract 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.