Basic C # Object-oriented concepts (basic C # OOP Concept) Series two

Source: Internet
Author: User
Tags multiple inheritance in c

6. Encapsulation

Encapsulation is the hiding of members or variables from external classes. I have already said that the security of the House is confined to the entrance of the house and does not need to know what is going on inside the room. The homeowner hid everything that had happened in the House for security. Hiding and restricting is called encapsulation.

For example we have two classes, one is "Houseclass" and the other is "Housesecurityclass".

We can see that all the members are wrapped in the public class Housesecurityclass, so the object of the Houseclass class can access any member of the Housesecurityclass. But Houseclass has 2 members, one public and one private. The private class cannot be accessed by external classes.

1  Public classHousesecurityclass2     {3          Public intnoofsecurity;4          PublicString Securityname =String.Empty;5     }6 7      Public classHouseclass8     {9         Private intNooflockerinhosue =2;Ten          Public stringOwnerName =String.Empty; One}
7. Abstraction

Abstraction is the display and sharing of some common information to the user. Then we discuss the example of the house. There are servants in the room, able to enter any room, do the cleaning and other things. The owner can give the servant all or part of the access to the bedroom. As we can see, the private members and methods in the program are the permissions that the servant does not have, and the members and methods of public are the permissions that the servant has.

1  Public classHouseserverntclass2     {3         4         Private intSaftyloackerkeyno =10001;5          PublicString roomcleaninstructions ="Clean all rooms";6 7             Private voidSaftynos ()8             {9Console.WriteLine ("My Saftyloackerkeyno is"+saftyloackerkeyno);Ten             } One  A          Public voidroomcleaninstruction () -         { - Console.WriteLine (roomcleaninstructions); the         } -  -}
8. Inheritance

Inheritance is for the reuse of code. In the protected internal access Modifier section we have seen examples of inheritance. Inheritance is the ability to access all exposed methods and variables, such as the parent class, in a derived class.

Single-level inheritance--a derived class inherits only one parent class.

1  Public classBaseClass2     {3         Private voidPrivatemethod ()4         {5Console.WriteLine ("Private Method");6         }7 8          Public voidPublicmethod ()9         {TenConsole.WriteLine ("This Method Shared"); One         } A     } -      Public classDerivedclass:baseclass -     { the         Static voidMain (string[] args) -         { -DerivedClass obj =Newderivedclass (); - Obj.publicmethod (); +             //Obj.privatemethod ();//This would be the error as private method can not be accessed in Derived Class -         } +  A}

Note: Sometimes you may not know what a base class is, what is a derived class. The base class is the Super class, and the derived class inherits the base class.

Here is an example of inheritance, base class Guestvist, derived class Houseownerclass. Derived classes houseownerclass inherit from the Guestvisit class.

1Houseownerclass.here HouseownerclassclassInherits theBase classof Guestvist2 classguestvist3     {4          Public voidguestwelcomemessage ()5         {6Console.WriteLine ("Welcome to Home");7         }8          Public voidGuestname ()9         {TenConsole.WriteLine ("Guest name Is:shanu"); One         } A        -     } -      classhouseownerclass:guestvist the     { -         Static voidMain (string[] args) -         { -Houseownerclass obj =NewHouseownerclass (); + obj. Guestwelcomemessage (); - obj. Guestname (); + console.readline (); A         } at}

Multi-level inheritance--examples of more than one derived class. In the following example we can see that baseclass derives from DerivedClass1, and DerivedClass1 derives DerivedClass2. Therefore, DERIVEDCLASS2 can access the public members and methods of the DERIVEDCLASS1 and BaseClass two classes.

1  Public classBaseClass2     {3         Private voidPrivatemethod ()4         {5Console.WriteLine ("Private Method");6         }7 8          Public voidPublicmethod ()9         {TenConsole.WriteLine ("This Method Shared"); One         } A     } -      Public classDerivedclass1:baseclass -     { the          Public voidDerivedClass1 () -         { -Console.WriteLine ("Derived Class 1"); -         } +     } -      Public classDerivedclass2:derivedclass1 +     { A         Static voidMain (string[] args) at         { -DerivedClass2 obj =NewDerivedClass2 (); - Obj.publicmethod (); - obj. DerivedClass1 (); -             //Obj.privatemethod ();//This would be the error as private method can not be accessed in Derived Class -         } in  -}
Multiple inheritance:

. Does the net support multiple inheritance? NO

A class cannot support multiple inheritance.

What is multiple inheritance? Multiple inheritance is a class that inherits multiple base classes. is a derived class that inherits more than 1 base classes.

What happens when you use multiple inheritance in C #?

Continue to see examples of the house. In the following example we have a derived class Ouseownerclass, two base classes "Guestvist" and "Friendsandrelationsclass".

Now there are two people in the house, a guest, a friend. So we've written 3 classes in which the derived class inherits 2 classes. When I write a multiple inheritance in C #, the compiler gives a warning message but can execute the code. The warning message is "expected and interface".

So how can we support multiple inheritance?

The answer is to use interfaces, which support multiple inheritance.

~ ~ ~ Come here today, take a bath to sleep

Basic C # Object-oriented concepts (basic C # OOP Concept) Series two

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.