Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:

Similar presentations


Presentation on theme: "Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:"— Presentation transcript:

1 Inheritance Building one object from another

2 Background Object-oriented programming is normally described has offering three capabilities Encapsulation: creating self-contained objects consisting of data and functions Inheritance: building new objects that extend and modify the features of existing objects Polymorphism: changing the behavior of members in inherited objects We will now turn to the second of these. The third will wait until Module 3.

3 Overview of Inheritance Three steps: 1. Start with an existing class 2. Create a new class, using the original class as a starting point 3. Add members (data, properties, functions) to extend the original class Benefits: Allows code reuse without “making a copy” of original code Helps manage code complexity

4 UML Representation Comments: Class diagram can be used to specify inheritance (generalization) relationships Child class shows those members that have been added In this example: At some branch offices, we are selling products at the retail level (from a store front) For these offices, we want to hold the sales tax we have paid, and compute a profit after tax

5 Implementing Inheritance Implementing inheritance is easy in C# Add the class you are inheriting from (a.k.a., base class) to the definition of the new class (a.k.a., child or sub-class) public class SubClass : BaseClass { // SubClass definition }

6 Inheriting Constructors The sub-class must define its own constructor functions—these don’t inherit If your base class has useful constructors, the base keyword can be used with arguments, e.g., public class ChildClass : BaseClass { public ChildClass(int x, double y) : base(y) { // Assumes BaseClass(double val) constructor exists }

7 Example class

8 Using inherited object Comments: All of the BranchOffice members can be used in the RetailBranchOffic class The 3 argument constructor is also called If we added capabilities to our BranchOffice class, they would become available in our new class as soon as we recompiled

9 protected Access So far, we’ve introduced two access specifiers public: accessible in child classes private: not accessible in child classes—not even in member functions! protected is the third option Like private, it can only be used in member functions Like public, it inherits—meaning protected member are available in member functions

10 Base Class: object In C#, all classes inherit from object, including classes you create object includes a number of members: ToString(), which creates a string representation of the object A comparison function, to see if one object is the same object as another A hash coding function useful for lookup Etc. Implication: any object you create will support the these members

11 Parting words… Inheritance in C# is very powerful, and very simple to implement The base keyword can be used to inherit constructors Additional capabilities not yet covered: Overriding and polymorphism Multiple inheritance using interfaces


Download ppt "Inheritance Building one object from another. Background Object-oriented programming is normally described has offering three capabilities Encapsulation:"

Similar presentations


Ads by Google