Summary

Section 12.1 Introduction

  • With polymorphism, we can design and implement systems that are easily extensible—new classes can be added with little or no modification to the general portions of the app.

Section 12.2 Polymorphism Examples

  • With polymorphism, the same method name and signature can be used to cause different actions to occur, depending on the type of object on which the method is invoked.

  • Polymorphism promotes extensibility: Software that invokes polymorphic behavior is independent of the object types to which messages are sent. New object types that can respond to existing method calls can be incorporated in a system without requiring modification of the base system.

Section 12.3 Demonstrating Polymorphic Behavior

  • Invoking a method on a derived-class object via a base-class reference invokes the derived-class functionality—the type of the referenced object determines which method is called.

Section 12.4 Abstract Classes and Methods

  • Abstract classes are incomplete classes for which you never intend to instantiate objects.

  • The purpose of an abstract class is primarily to provide an appropriate base class from which other classes can inherit, and thus share a common design.

  • Classes that can be used to instantiate objects are called concrete classes.

  • You make a class abstract by declaring it with keyword abstract.

  • Each concrete derived class of an abstract base class must provide concrete implementations of the base class’s abstract methods and properties.

  • Failure to implement a base class’s abstract methods and properties in a derived class is a compilation error unless the derived class is also declared abstract.

  • Although we cannot instantiate objects of abstract base classes, we can use them to declare variables that can hold references to objects of any concrete class derived from those abstract classes.

Section 12.5 Case Study: Payroll System Using Polymorphism

  • By declaring a method abstract, we indicate that each concrete derived class must provide an appropriate implementation.

  • All virtual method calls are resolved at execution time, based on the type of the object to which the reference-type variable refers. This process is known as dynamic binding or late binding.

  • The is operator determines whether the type of the object in the left operand matches the type specified by the right operand and returns true if the two have an is-a relationship.

  • The as operator performs a downcast that returns a reference to the appropriate object if the downcast is successful and returns null if the downcast fails.

  • A base-class reference can be used to invoke only the methods declared in the base class. If an app needs to perform a derived-class-specific operation on a derived-class object referenced by a base-class variable, the app must first downcast the base-class reference to a derived-class reference.

  • Every object knows its own type and can access this information through method GetType, which all classes inherit from class object.

  • Assigning a base-class reference to a derived-class variable is not allowed without an explicit cast or without using the as operator. The is operator can be used to ensure that such a cast is performed only if the object is a derived-class object.

Section 12.6 sealed Methods and Classes

  • A method that’s declared sealed in a base class cannot be overridden in a derived class.

  • A class that’s declared sealed cannot be a base class (i.e., a class cannot extend a sealed class). All methods in a sealed class are implicitly sealed.

Section 12.7 Case Study: Creating and Using Interfaces

  • Interfaces define and standardize the ways in which things such as people and systems can interact with one another.

  • An interface declaration begins with keyword interface and can contain only abstract methods, properties, indexers and events.

  • All interface members are implicitly declared both public and abstract. They do not specify any implementation details, such as concrete method declarations.

  • Each interface can extend one or more other interfaces to create a more elaborate interface that other classes can implement.

  • A class must specify that it implements the interface by listing it after the colon (:) in the class declaration.

  • A class that implements an interface but doesn’t implement all the interface’s members must be declared abstract and contain an abstract declaration of each unimplemented interface member.

  • The UML expresses the relationship between a class and an interface through a realization. A class is said to “realize,” or implement, an interface.

  • To implement more than one interface, use a comma-separated list of interface names after the colon (:) in the class declaration.

  • Inheritance and interfaces are similar in their implementation of the is-a relationship. An object of a class that implements an interface may be thought of as an object of that interface type.

  • All methods of class object can be called by using a reference of an interface type—the reference refers to an object, and all objects inherit the methods of class object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset