Three characteristics of object-oriented language

Source: Internet
Author: User
Tags abstract extend inheritance

The three main characteristics of object-oriented language are: encapsulation inheritance polymorphism

Recently, it is quite necessary to summarize this question, so I forwarded some paragraphs of this article.


Encapsulation is one of the object-oriented features and is the main feature of object and class concepts. Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding. Example code:

public class Department
     private string departname;

     Read method public
     string Getdepartname () {
          return departname;

     Write method public
     void Setdepartname (String a) {



One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).

In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.

? Implementation inheritance refers to the ability to use the properties and methods of a base class without additional coding;

? Interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide the implementation;

? Visual inheritance is the ability of a subform (class) to use the appearance of a base form (class) and to implement code.

When considering using inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human.

Abstract classes only define generic properties and methods that will be created by subclasses, and when creating abstract classes, use the keyword Interface instead of class.

OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances.

Example code for common class inheritance relationships :

Public  class a{
     int test=0;
     Public A () {
          test = 5;
          Console.WriteLine ("I am A Public default constructor, test={0}", test);}

public class B:a {

} public

class InheritanceTest1 {public
     static void Main (string[] args) {
            b b = new B ();

Polymorphism (POLYMORPHISN) is a technique that allows you to set a parent object to be equal to one or more of his child objects, after which the parent object can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.

To achieve polymorphism, there are two ways, covering, overloading.

Overwrite refers to the practice of redefining the virtual function of a parent class by a subclass.

overloading, which means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (perhaps the number of parameters is different, perhaps the parameter types are different, perhaps the two are different).

In fact, the concept of overloading is not "object-oriented programming", the implementation of overloading is: The compiler according to the function of a different parameter table, the name of the same name is decorated, and then the same name function is a different function (at least for the compiler). For example, there are two functions of the same name: function func (p:integer): Integer, and function func (p:string): integer; Then the compiler has done a modified function name may be this: Int_func, Str_func. The invocation of these two functions has been determined between the compilers and is static (remember: Static). That is, their addresses are bound (early bound) at compile time, so overloading and polymorphism are irrelevant. The true and polymorphic correlation is "overlay". When a subclass has redefined the virtual function of the parent class, the parent pointer is dynamic (remember: dynamic) According to the different child-class pointers assigned to it. Call to the function of the subclass, such that the function call cannot be determined during compilation (the address of the virtual function of the calling subclass cannot be given). Therefore, such a function address is bound at run time (late bonding). The conclusion is that overloading is only a linguistic feature, independent of polymorphism and irrelevant to object-oriented. Quote a WORD from Bruce Eckel: "Don't be foolish, if it's not late, it's not polymorphic." ”

So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to-code reuse. Polymorphism is for another purpose--interface reuse. The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.

    public class Animal
        virtual void Eat ()
            Console.WriteLine ("Animal Eat");

    public class Cat:animal
        override void Eat ()
            Console.WriteLine ("Cat Eat");
    } Public

    class Dog:animal
        override void Eat ()
            Console.WriteLine ("Dog Eat");

    Class Tester
        static void Main (string[] args)
            animal[] animals = new Animal[3];

            Animals[0] = new Animal ();
            ANIMALS[1] = new Cat ();
            ANIMALS[2] = new Dog ();

            for (int i = 0; i < 3; i++)
                animals[i]. Eat ();}}}

The article will constantly revise the update, not the final version

Reference website:




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: 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.