Description of C # inheritance

Source: Internet
Author: User

inheritance, encapsulation, and polymorphism are important features of object-oriented programming .

The class whose members are inherited is called the base class , also called the parent class , and the class that inherits its members is called the derived class , also called a subclass .

Derived classes implicitly obtain all members of the base class except constructors and destructors .

derived classes can have only one direct base class, so C # does not support multiple inheritance , but a base class can have multiple direct derived classes.
inheritance can be passed .

That

if ClassB derives from ClassC,ClassA derived from ClassB,The ClassC (grandchild) inherits the members declared in the ClassB (child) and ClassA (parent).
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacetestclass{classProgram {Static voidMain (string[] args) {Son Son1=Newson (); Son1.minus (9,6);        Console.read (); }        //Parent Class         Public classFather { Public voidSum (int_a,int_b,string _identity) {                intsum = _a +_b; System.Console.WriteLine ("I am the {3} class, I calculated the result is: {0}, I calculate is {1}+{2}", Sum, _a, _b, _identity); }        }        //subclass subclass inherits elements of parent class         Public classSon:father { Public voidMinus (int_i,int_j) {                 This. Sum (Ten,Ten,"Parent");  This. Sum (_i,_j,"Child"); }        }    }}

I am the parent class, and the result I calculated is: 20, I calculate the 10+10
I am a subclass, and the result I calculated is: 15, I calculated the 9+6

When the base class sum () method is private, does the derived class also inherit the method?

If the method is not found in Class B, is it not inherited? not really, private members have actually been inherited,
However, they cannot be accessed because private members can only be declared in their class or struct, so they do not appear to be inherited.

If we want to reduce the basic access, we can define the base class sum () method as protected.
Is it possible to prevent a class from being inherited by another class?
The answer is yes, C # provides a sealed modifier that prevents other classes from inheriting from the class.

 classProgram {Static voidMain (string[] args) {Son Son1=Newson (); Son1.minus (9,6);        Console.read (); }        //Parent Class       Sealed classFather { Public voidSum (int_a,int_b,string _identity) {                intsum = _a +_b; System.Console.WriteLine ("I am the {3} class, I calculated the result is: {0}, I calculate is {1}+{2}", Sum, _a, _b, _identity); }        }        //subclass subclass inherits elements of parent class         Public class son:father { Public voidMinus (int_i,int_j) {                This  . Sum (Ten,  " father "  )); //This three will be an error  .This  . Sum (_i,_j," sub " ); }        }    }

Description of C # inheritance

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.