Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controlling Base-Class Creation

Similar presentations


Presentation on theme: "Controlling Base-Class Creation"— Presentation transcript:

1 Controlling Base-Class Creation
Usually, the base class constructors are used to initialize the members of base class. When derived class has few more members and if we need to initialize, then we will write constructor for derived class. In such a situation, there is a repetition of code. For Eg: public class Emp { protected string Name; protected int EmpID; public Emp(string n, int eid, float s) Name=n; EmpId=eid; } ` public class SalesMan:Emp { int number_of_sales; public SalesMan(string n, int eid, int s) Name=n; //code repeated EmpId=eid; //code repeated number_of_sales=s; }

2 protected string Name; protected int EmpID;
If there is more number of data fields in the base class, then in each of the derived class constructors, we need to repeat such assignment statements. To avoid this, C# provides an option to explicitly call base class constructors as shown below – public class Emp { protected string Name; protected int EmpID; public Emp(string n, int eid) Name=n; EmpId=eid; } public class SalesMan:Emp int bonus; public SalesMan(string n, int eid, int b): base(n, eid) bonus=b; //other members are passed to base class to initialize class Test { public static void Main() Emp e1=new Emp(“Ram”, 25); SalesMan s1= new SalesMan(“Rohan”, 12, 10); }

3 Keeping Family Secrets: The protected Keyword
The private members of a class can not be available to derived classes. The protected members of a class can not be accessed from outside, but still be available to derived classes. Preventing Inheritance: Sealed Classes In some of the situations, we may don’t want our class to be inherited by any other class. For example –

4 Here, a class called Part-time SalesMan is derived from SalesMan class which in-turn is derived from Employee class as shown in the diagram. Now, we don’t want any more classes to be derived from Part-time SalesMan class. To prevent such inheritance, C# provides a keyword sealed. public sealed class Part_TimeSalesMan: SalesMan { //body of Part_TimeSalesMan class } Now any attempt made to derive a class from Part_TimeSalesMan class will generate an error: public class Test: Part_TimeSalesMan //compile-time error!!! …………..

5 int CurrSpeed, MaxSpeed; string name; bool carIsDead=false;
Programming for Containment/Delegation class Radio { public void TurnOn(bool on) if(on) Console.WriteLine(“Radio is on”); else Console.WriteLine(“Radio is off”); } class Car int CurrSpeed, MaxSpeed; string name; bool carIsDead=false;

6 public Car() { MaxSpeed=100;} public Car(string n, int m, int c) { name=n; MaxSpeed=m; CurrSpeed=c; } public void SpeedUp(int a) if(carIsDead) Console.WriteLine(“Out of order”); else CurrSpeed+=a; Now, we have two classes viz. Radio and Car. But, we can not say, “Car is a Radio” or “Radio is a Car”. Rather, we can say, “Car has a Radio”. Now, the Car is called containing Class and Radio is called contained class.

7 Nested Type Definitions
To make the contained class to work, we need to re-define Car class as – class Car { ……….. private Radio rd=new Radio(); } Nested Type Definitions class C1 { ……… //members of outer class class C2 ………. //members of inner class In C#, it is possible to define a type (enum, class, interface, struct, delegate) directly within the scope of a class. In containment/delegation, the object of contained class is created within containing class. But, it is possible to create the objects wherever we wish.


Download ppt "Controlling Base-Class Creation"

Similar presentations


Ads by Google