Inheritance

Inheritance allows objects to assume the properties and methods of other objects. Listing A.33 shows a simple C# inheritance example.

Listing A.33. A Simple Inheritance Example
 1: using System; 2: 3: class Person{ 4: private string name; 5: private int age; 6: 7: public Person(string name, int age){ 8: this.name = name; 9: this.age = age; 10: } 11: 12: public string Name{ 13: get{ return name;} 14: set{ name = value;} 15: } 16: 17: public int Age{ 18: get{ return age;} 19: set{ age = value;} 20: } 21: } 22: 23: class Employee: Person{ 24: DateTime hireDate; 25: 26: public Employee(string name, int age, DateTime hireDate): 27: base(name, age){ 28: this.hireDate = hireDate; 29: } 30: 31: public DateTime HireDate{ 32: get{ return hireDate.Date;} ...

Get Building e-Commerce Sites with the .NET Framework now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.