Download presentation
Presentation is loading. Please wait.
Published byMelvin Scott Modified over 9 years ago
1
Programming Pillars Introduction to Object- Oriented Programming
2
Slide 2 Review UML Class diagrams depict the classes that make up a component the members of those classes The associations between classes Class blocks are used to implement classes The Visual Studio Class designer does some of the grunt work
3
Slide 3 Inheritance (Introduction) It’s all about generalization Inheritance involves an “is a” relationship Examples Graduate students and undergraduate students are both students A graduate student is a student Faculty and classified staff are employees Students and employees are people
4
Slide 4 Inheritance (Goals) Delegate responsibilities from several specific classes to a more generalized class More specific classes can then reuse the code of more general classes Don’t create too many levels of generalized classes There is no hard-and-fast rule though
5
Slide 5 Inheritance (UML) UML depicts a generalization using a solid line and an unfilled arrow The arrow points from the specific class to the general class
6
Slide 6 Inheritance (UML) (Illustration)
7
Slide 7 Inheritance (Terms 1) Inheritance is the ability of one class to obtain, or inherit, all of the members of another class Base class - inherited class Derived class - inheriting class Inheritance can be hierarchical One class can inherit from a base class, which inherits from another class
8
Slide 8 Inheritance (Terms 2) Inheritance applies to all members Sub procedures, Function procedures, Property procedures Delegation: A derived class can leave the behavior alone Extension: A derived class can add new behaviors to the base class Overriding : A derived class can change the behavior of a member declared in the base class
9
Slide 9 Inheritance (Terms 3) By definition, all.NET Framework classes support single inheritance A derived class can inherit from one and only one base class Every class ultimately inherits from System.Object Some languages support multiple inheritance C++, for example Interfaces give us the means to get around the single inheritance rule but we will not discuss interfaces in this class
10
Slide 10 Inheritance and.NET Everything in.NET inherits from the superclass named System.Object In Java, the superclass is named Object
11
Slide 11 Inheritance and.NET (Illustration)
12
Slide 12 Inheriting a Class In VB, you would use the Inherits keyword In C#, the syntax is more terse In the class declaration, the base class follows the derived class named separated by a “:” Example: The Student class derives from the Person class class Student : Person { }
13
Slide 13 Overriding Members Use overriding when a member in a derived class should perform a different action than the member of the same name in a base class Use the override keyword in the derived class member declaration It is not necessary to override all base class members – just the ones whose actions should be changed
14
Slide 14 Overriding Members (Keywords) Optional virtual keyword appears in a member declaration It indicates that the member may be overridden in a derived class
15
Slide 15 Overriding Members (Example 1) public class Person { public string FirstName; public string LastName; public override string ToString() { return FirstName + "," + LastName }
16
Slide 16 Preventing and Forcing Inheritance abstract keyword appears in the declaration for a member in a base class Use to declare an abstract member Derived class must provide an implementation for the abstract member sealed keyword indicates that a class cannot be inherited Use to create a sealed class
17
Slide 17 base (Introduction) The base keyword is used to call a method in a base class Use base to augment a base class method Rules: The call to base must be the first statement in the method body It’s illegal to assign base
18
Slide 18 Using base base keyword works like an object variable Use in a derived class to call a member in a base class Use base to call a constructor in a base class base is not a real object Cannot be used in modules because modules cannot be inherited
19
Slide 19 Inheritance and Constructors (Introduction) Constructors have different inheritance rules than other procedures Constructors without arguments are implicitly inherited Constructors with arguments are not implicitly inherited
20
Slide 20 Default Constructors The constructor (without arguments) in the base class is called from the constructor in the derived class Inheritance is “automatic” The statements in the base class constructor are called before the statements in the current (derived) class execute MyBase is optional but not required
21
Slide 21 Default Constructors (Call Sequence) Public Class Base1 Public Sub New() ' Statements End Sub End Class Public Class DerivedFromBase1 Inherits Base1 Public Sub New() ' Statements End Sub End Class Public Class DerivedFromBase2 Inherits DerivedFromBase1 Public Sub New() MyBase.New() ' Statements End Sub End Class Call base class constructor (1) Call base class constructor (2) Return (4) Return (3)
22
Slide 22 Custom Constructors Constructors with arguments are not implicitly inherited Usage rules: In the base class, call base base MUST be the first statement in the constructor Call base with the desired argument to call the appropriate base class constructor Remember that standard overloading rules apply
23
Slide 23 Inheritance and Shared Members Shared members CANNOT be overridden Shared members can be inherited "as is" Shared members can be shadowed and redefined from scratch base is not allowed in shared members
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.