100 likes | 242 Vues
In this lecture, we explore the fundamentals of abstract classes and inheritance in programming. We define what abstract classes are, their role in creating generalized classes, and how they enforce standardized implementations in derived classes. Key concepts include defining interfaces in abstract classes, implementing abstract methods in derived classes, and prohibiting inheritance through sealed classes. Examples from cryptography illustrate the application of these concepts, demonstrating the significance of the abstract class in symmetric algorithms. This guide is essential for mastering object-oriented programming principles.
E N D
Programming Pillars Inheritance Part 2
Lecture Overview • Abstract classes
Abstract Classes (Introduction) • Abstract classes define an interface • The implementation is either undefined or partially defined • It's not possible to create an instance of an abstract class • It's only possible to inherit from an abstract class • The purpose of abstract classes • Use to create generalized classes • Use to force standardized implementations of derived classes
Abstract Classes (Creating) • In the abstract class, define the interface for the class • Declare an abstract class or member with the abstract keyword in the class declaration
Abstract Classes (Using) • Define the implementation in the derived class • A derived class can inherit from an abstract classA derived class must implement all abstract members • (those declared as abstract) • A derived class can inherit members implemented in the abstract class
Abstract Classes (Example 1) • Cryptography uses standard algorithms to encrypt and decrypt messages • There are different cryptographic algorithms • DES, RC2, Rijndael, TripleDES • They all “work” the same way • They are all symmetric algorithms • The SymmetricAlgorithm class is the abstract class for all symmetric cryptographic algorithms
Abstract Classes (Example 2) • Declare an abstract class named person public abstract class Person {} • Declare an abstract method named public abstract bool SetID(intargID); • Note that the method has NO implementation
Implementing an Abstract Method (Example) • Use the override keyword to declare the implementation of an abstract method public override bool SetID( int argID) { RNumberHidden = argID; }
Prohibiting Inheritance • A sealed class is a class that cannot be inherited • Declare sealed classes with the sealed keyword • The string class is not inheritable