Object-oriented polymorphism, abstract classes, and interfaces

Source: Internet
Author: User

Today, I learned some of the content of polymorphic, abstract classes, and interfaces.

In the actual operation, the same parent class is inherited, but the method in the parent class does not apply to any subclass, then the subclass is required to rewrite the body of the method.

When overriding a member method, the parent class is required to consent to a subclass that can rewrite its own method:Virtual -virtual method; Subclasses can only override methods that are allowed by the parent class , overriding the virtual method: Override-Overrides the virtual method.

A base class is relative to a derived class, and all classes can be treated as parent classes and can have virtual methods.

Abstract class: Abstraction

Abstract classes are meant to be inherited. All the methods in the ordinary class become virtual methods, no one uses the most basic method, all need to rewrite, then do not need so laborious to the ordinary class of the method body is written, but directly defined as abstract class, and are written abstract methods.

I think that abstract class is to put the project we do, the whole into a large class, its primary goal, not to achieve what function, but to limit the project to a scope, under this precondition to expand our project, the concrete module of the project to inherit this abstract class, and then to write the specific functions of the subclass. In an abstract class, you can have an abstract method, or you can have an ordinary method. Abstract methods only need to declare the name and return type of the method, and what the body of the method is, and inherit the past and implement it by subclasses.

Cases:

 public  abstract  class   Ren { public  abstract  string   Chifan ();  public  abstract  string   Shuijiao ();  public  virtual  string   Shuohua () { return   I can talk!          ; }    }

Interface:interface

Interface is to make the class we write more perfect, to complement the classes we write. In the interface, only the abstract method can be written, no specific method can be written, all the content needs to be implemented in the subclass.

Such as:

 Public Interface Gongzuo    {        string  Jineng ();         string Didian ();    }

The subclass's format when inheriting abstract classes and interfaces:

 Public class Nanren:ren, Gongzuo, yule{}

Object-oriented polymorphism, abstract classes, and interfaces

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.