Inheritance in C #

Source: Internet
Author: User

has been engaged in development work with vs,c#, but also as a learning, while writing, and now the way to record the accumulation of knowledge, so that later to review, some of the essays for their own writing, and some are from the Internet to find other blog information, the main purpose is to accumulate their own knowledge base.

For beginners Reference:

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 except constructors and destructors for the base class.

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

If ClassB derives Classc,classa derived ClassB, ClassC inherits the members declared in ClassB and ClassA. Class A
{
public void Sum (int i,int j)
{
int sum = i + j;
Console.WriteLine ("I am A, my sum ={0}", sum);
}
}
Class B:a
{
public void minus (int i,int j)
{
int minus = I-j;
Console.WriteLine ("I am B, my minus ={0}", minus);
This.        Sum (3, 4); }

}
Class InheritanceTest1
{
static void Main (string[] args)
{
b b = new B ();
B.minus (3, 4);
Console.read ();
}
}

Result: I am B, my minus=-1

I am A, my sum = 7

Imagine if the base class sum () method is private, will the derived class inherit the method as well?

After I test, did not find the method in class B, then it is not inherited it? 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.

Sealed class A
{
int test;
public void Sum (int i,int j)
{
int sum = i + j;
Console.WriteLine ("I am A, my sum ={0}", sum);
}
}
Class B:a
{
public void minus (int i,int j)
{
int minus = I-j;
Console.WriteLine ("I am B, my minus ={0}", minus);
This.       Sum (3, 4); Compiler will error
}
}

As mentioned earlier, a derived class implicitly obtains all members except constructors and destructors for the base class.
So how do we get the constructor of the base class and its own constructor?
We know that the initialization of the base class is done by the constructor of the base class, and the initialization of the derived class is done with the constructor of the derived class.
However, this gives rise to the order of execution of derived class constructors.
When a base class has no constructors and the derived class does not have constructors, the initialization work of the new member of the derived class is done by other public functions.

public class A
{
int test=0;
public void sum ()
{
test++;
Console.WriteLine ("I AM Test ={0}", test);
}
}
Class B:a
{
int i;
public void Printint ()
{
i = 3;
Console.WriteLine ("I Am I ={0}", i);
}
}
Class InheritanceTest1
{
static void Main (string[] args)
{
b b = new B ();
B.printint ();
Console.read ();
}
}

Results: I am i=3

if only the derived class defines the constructor, you can simply construct the derived class object. The base class part of the object is created automatically using the default constructor. When both the base class and the derived class have constructors defined, what is the order of execution?
if a constructor with no parameters is in the base class if a constructor with no arguments is in the base class, you can customize the constructor with parameters in the derived classpublic class A
{
int test=0;
Public A ()
{
Test = 5;
Console.WriteLine ("I am A Public default constructor, test={0}", test);
}
}
Class B:a
{
}
Class InheritanceTest1
{
static void Main (string[] args)
{
b b = new B ();
Console.read ();
}
}

Result: I am A Public default constructor, test=5

As you can see, the constructor of the base class is executed and is called in the derived class.
if the base class defines a constructor with parameters, then this constructor must be executed and implemented in the derived class, at which point we can use the Base keyword
Class A
{
int test=0;
Public A (int i)
{
Test = i;
Console.WriteLine ("I am a public parametric constructor, test={0}", test);
}
}
Class B:a
{
Public B (Int j): Base (j)
{
Console.WriteLine ("I am B Public parametric constructor, j={0}", j);
}
}
Class InheritanceTest1
{
static void Main (string[] args)
{
b b = new B (1);
Console.read ();
}
}

Results: I am a public with a parametric constructor, test=1

I am B Public has a parametric constructor, j=1

Thus:  A derived class implicitly executes a constructor with parameters in the base class, in which the base class defines a constructor with parameters.
is inherited in its derived class and uses the base keyword to call the constructor in the base class to pass the arguments. We can see from the code that after the object of the derived class is created, the program first runs the contents of the base class's constructor, and then the content in the derived class. if the base class of a derived class is also a derived class, then each derived class only has to be responsible for the construction of its direct base class, not the construction of the indirect base class.
and the order in which the constructors are executed begins with the topmost base class, until the last derived class ends.

Inheritance in C #

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.