Docsity

Understanding Inheritance in Object-Oriented Programming: A Comprehensive Guide, Slides of C Sharp Programming

Partial preview of the text

Download Understanding Inheritance in Object-Oriented Programming: A Comprehensive Guide and more Slides C Sharp Programming in PDF only on Docsity!

Programming Pillars

Introduction to Inheritance

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

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

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

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

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

Inheritance and .NET

 Everything in .NET inherits from the superclass named System.Object  In Java, the superclass is named Object

Inheritance and .NET

(Illustration)

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

Overriding Members

(Keywords)

 Optional virtual keyword appears in a member declaration  It indicates that the member may be overridden in a derived class

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

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

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

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