CSC439: Visual Programming: Inheritance, Abstract Classes, Abstract Members
This document discusses inheritance, abstract classes, and interfaces in C#. It includes examples of inheriting from a base class to extend it, using the sealed keyword to prevent inheritance, defining abstract methods in an abstract class that derived classes must implement, and when to use an interface versus an abstract class.
CSC439: Visual Programming: Inheritance, Abstract Classes, Abstract Members
This document discusses inheritance, abstract classes, and interfaces in C#. It includes examples of inheriting from a base class to extend it, using the sealed keyword to prevent inheritance, defining abstract methods in an abstract class that derived classes must implement, and when to use an interface versus an abstract class.
Inheritance A class can inherit from another class to extend or customize the original class. Inheriting from a class lets us reuse the functionality in that class instead of building it from scratch. A class can inherit from only a single class, but can itself be inherited by many classes.
2 Lecture 9 - Constructor, Properties, Indexer
Inheritance Example public class Asset { public string Name; } public class Stock : Asset // inherits from Asset { public long SharesOwned; } Inside main method: Stock msft = new Stock { Name="MSFT", SharesOwned=1000 }; Console.WriteLine (msft.Name); // MSFT Console.WriteLine (msft.SharesOwned); // 1000
3 Lecture 9 - Constructor, Properties, Indexer
Using sealed to Prevent Inheritance As powerful and useful as inheritance is, sometimes we will want to prevent it. In such cases we use sealed keyword sealed class A { // ... } // The following class is illegal. class B : A { // ERROR! Can't derive from class A // ... }
4 Lecture 9 - Constructor, Properties, Indexer
Abstract Class Sometimes we need to create a base class that defines only a generalized form that will be shared by all of its derived classes leaving it to each derived class to fill in the details. Such a class determines the nature of the methods that the derived classes must implement, but does not, itself, provide an implementation of one or more of these methods. In such cases we create Abstract class.
5 Lecture 9 - Constructor, Properties, Indexer
Abstract Class Since an abstract class does not define a complete implementation, there can be no objects of an abstract class. Thus, attempting to create an object of an abstract class by using new will result in a compile-time error When a derived class inherits an abstract class, it must implement all of the abstract methods in the base class.
6 Lecture 9 - Constructor, Properties, Indexer
Abstract Class Example // Create an abstract class. using System; abstract class TwoDShape { double pri_width; double pri_height; public abstract double Area(); }
// A derived class of TwoDShape for triangles.
class Triangle : TwoDShape { string Style; // Override Area() for Triangle. public override double Area() { return Width * Height / 2; }
7 Lecture 9 - Constructor, Properties, Indexer
Choosing Between an Interface and an Abstract Class One of the more challenging parts of C# programming is knowing when to create an interface and when to use an abstract The general rule is this: When you can fully describe the concept in terms of “what it does” without needing to specify any “how it does it,” then you should use an interface. If you need to include some implementation details, then you will need to represent your concept in an abstract class.
Complete Download Pro Functional PHP Programming: Application Development Strategies for Performance Optimization, Concurrency, Testability, and Code Brevity Rob Aley PDF All Chapters