Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance.

Similar presentations


Presentation on theme: "Inheritance."— Presentation transcript:

1 Inheritance

2 Inheritance allows a class to be derived from another class known as the base class. The class that you derive is called the derived class. تَسْمحُ الوراثة باشتقاق صنف مِنْ صنفِ آخرِ والمعروف بالصنفِ الأساسيِ.و إنّ الصنفَ الذي يَشتقُّ يُدْعَى الصنفَ المُشتَقَّ. The concept of inheritance gives us the ability to create a new class based on an existing class. The new class can use all the features of the original class, it can override existing features, it can extend existing features, or it can add its own features. A number of new classes can inherit from an original class; however, only one class can be inherited from. For example, you can have a base class named control that can be inherited by a number of different control types.

3

4 Several basic terms are commonly used when discussing inheritance:
Base class The original class. Parent class Another name for a base class. Derived class A new class, created by inheriting from a base class. Child class Another name for a derived class. Single inheritance A derived class created from only one base class. C# supports only single inheritance. Multiple inheritance A derived class created from two or more base classes. C# does not support multiple inheritance.

5 Inheritance, otherwise known as an “is-a” or “kind-of” relationship, allows a class of objects to reuse(the data and methods of a class by the derived class), redefine, and possibly extend the functionality of an existing class. However, the constructors and destructors of the base class are not implicitly(ضمنياً) inherited. الوراثِة تعرف بعلاقة “kind-of”أو “is-a” ، وتَسْمحُ بإعادة الاستعمال ثانية (البيانات والطرق للصنف من قبل الصنفِ المُشتَقِّ)، ويُعيدُ التعريف، و من المحتمل يُوسع وظيفةَ الصنفِ الحاليِ(الموجود). وان دالة البناء والهدم للصنفِ الأساسيِ لا تورث ضمنياً Rather than being inherited, instance constructors of the base class are called either implicitly or explicitly upon creation of an object from the derived class.

6

7 Readonly Instance Variable: C# provide keyword readonly to specify that an instance variable of object is not modifiable & that any attempt to modify it after the object is constructed is an error. Read variable can be initialized by each of the classes constructor. Example: class A { int x; public A(int i) x = i ; } public void modix( ) // error x = 7;

8 Protect members : A base class s protected members can be accessed by members of that class & by members of its derived class. Example: class B { protected int i, j; public void set(int x, int y) i = x; j = y; } public void show() Console.WriteLine("{0} {1}", i, j);

9 class D:B { int k; public void setk() k =i
class D:B { int k; public void setk() k =i*j; } public void showk() Console .WriteLine ("{0}",k); static void Main(string[] args) D ob=new D(); ob.set (5,6); ob .show (); ob.setk (); ob .showk ();

10 implementing Inheritance The syntax of inheriting a derived class from a base class. class <derived class> : <base class> In the previous code, a colon (:) is used to indicate that a class is derived from an existing class. For example, class C2 : C1 Here, C1 is the base class of C2, and therefore, C2 inherits all members of C1. In this example, could be read as “class C2 inherits from class C1”. In this case, C2 is the derived class and C1 is the base class.

11 Example: class E { public void E_Name() { } } class S : E public void S_Name () { } class B { static void Main() { S obj = new S( ); obj . E_Name( ) ; obj . S_Name ( ) ; This code declares two classes, E and S. The S class is inherited from the E class and, therefore, inherits the method declared by the base class. An instance of the derived class is created to access the members of the base class.

12 The Keyword new The derived class implicitly inherits all the members of the base class. However, you can hide any method of the base class so that the derived class cannot access the method. To do so, you declare another method by a signature that is same as that of the base class method. When a compiler finds such a method, it generates a warning. To suppress this warning, you use the new keyword. This makes the base class method inaccessible to the derived class.

13 In the above case, if you need to declare a method with the name E_Name() in the S class, you need to include the new keyword in the method declaration statement. The next example uses the new keyword. class S : E { new public void E_Name() { } public void S_Name () { } }

14 Using base class constructor: Aderived class can call a constructor in its base class by using an expanded form of the derived class s constractor and the base keyword. The general form is: Derived constructor (parameter_list) :( base _list) { ….. } class Derived : Base { ... public Derived (int x) : Base(x) { ... } ... }

15 Example: class B { protected int i, j; public B(int x, int y) i = x; j = y; } class D:B public D (int x, int y, int z): base (y,z) int k=x; static void Main(string[] args) D d =new D (5,7,8);

16 Assigning Classes You have seen how to declare a variable using a class type, and then use the new keyword to create an object. The type-checking rules of C# prevent you from assigning a variable of one type to an object instantiated from a different type. For example, given the definitions of the Token, Id_Token, and KeywordToken classes, the following code is illegal (غير مسموح): class Token { ... } class Id_Token : Token class KeywordToken : Token ...

17 Id_Token it = new Id_Token(); KeywordToken kt = it; // error – different types However, it is possible to refer to an object from a variable of a different type as long as the type used is a class that is higher up the inheritance hierarchy. So the following statements are legal (مسموح): Id_Token it = new Id_Token(); Token t = it; // legal (قانوني) as Token is a base class of Id_Token


Download ppt "Inheritance."

Similar presentations


Ads by Google