Save
Busy. Please wait.
Log in with Clever
or

show password
Forgot Password?

Don't have an account?  Sign up 
Sign up using Clever
or

Username is available taken
show password


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
Your email address is only used to allow you to reset your password. See our Privacy Policy and Terms of Service.


Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.
focusNode
Didn't know it?
click below
 
Knew it?
click below
Don't Know
Remaining cards (0)
Know
0:00
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Chapter 12

OOP: Polymorphism, Interfaces, and Operator Overloading

TermDefinition
Polymorphism Enables us to program in the general rather than program in the specific
Systems Are Easy to Extend With polymorphism, we can design and implement systems that are easily extensible
Easily Extensible New classes can be added with little or no modification to the general portions of the app, as long as the new classes are part of the inheritance hierarchy that the app processes generically
Interface Describes a set of methods and properties that can be called on an object, but does not provide concrete implementations for them
Implement To provide concrete implementations for the methods and properties of something. You can declare classes that implement one or more interfaces
Calling a Method Through a Base-Class Variable That Contains a Reference to a Derived Class Object A base-class variable that contains a reference to a derived class object and is used to call a virtual method actually calls the overriding derived-class version of the method.
Base-Class Referencing A base-class reference can be used to invoke only the methods declared in the base class
Trying to Invoke a Derived Class Method by a Base-Class Reference Attempting to invoke a derived-class-only method through a base-class reference results in a compilation error.
1. Downcasting 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 cast the base-class reference to a derived-class reference through this process
2. Downcasting Downcasting is when an app casts the base-class reference to a derived-class reference through. This enables an app to invoke derived-class methods that are not in the base class
Abstract Classes Classes for which you never intend to instantiate objects. Because they're used only as base classes for inheritance hierarchies, we refer to them as abstract base classes
Concrete Classes Classes that can be used to instantiate objects. These classes provide the specifics that make it reasonable to instantiate objects
Abstract Methods A method with keyword abstract in its declaration. Ex: public abstract void Draw();. Abstract methods are implicitly virtual and do not provide implementations.
Abstract Classes Methods An abstract class normally contains one or more abstract method. A class that contains abstract methods must be declared as an abstract class even if it contains some concrete methods
Concrete Derived Classes Each concrete derived class of an abstract base class also must provide concrete implementations of the base class's abstract methods
1. Abstract Properties Can also be declared abstract or virtual, then overridden in derived classes with the override keyword, just like methods. This allows an abstract base class to specify common properties of its derived classes.
2. Abstract Properties Abstract property declarations have the form:... public abstract ProperType MyProperty... {... get;... set;... }. The semicolons after the get and set keywords indicate that we provide no implementation for these accessors
Constructors and static Methods Cannot be Abstract Constructors and static methods cannot be abstract.
Common Programming Error with Abstract Classes 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
Downcasting Example foreach(currentEmployee is BasePlusCommissionEmployee).. {.. BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee;.. employee.BaseSalary *= 1.10M;.
1. as Operator You can use the as operator to perform a downcast rather than a cast operator. For example, in the statement...
2. as Operator BasePlusCommissionEmployee employee = currentEmployee as BasePlusCommissionEmployee;...employee is assigned a reference to an object that is a BasePlusCommissionEmployee, or the value null if currentEmployee is not a BasePlusCommissionEmployee.
Method GetType Every object known its own type nd can access this information through method GetType, which all classes inherit from class object. Ex: employee[j].GetType();
Class Type Method GetType returns an object of class Type(of namespace System), which contains information about the object's type, including its class name, the names of its methods, and the name of its base class. Type class's ToString method returns class name
1. Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables 1. Assigning a base-class reference to a base-class variable is straightforward.. 2. Assigning a derived-class reference to a derived-class variable is straightforward..
2. Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables 3. Assigning a derived-class reference to a base-class variable is safe, because the derived-class object is an object of its base class. However, this reference can be used to refer only to base-class members.
3. Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables If this code refers to derived-class-only members through the base-class variable, the compiler reports errors...
4. Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables 4. Attempting to assign a base-class reference to a derived-class variable is a compilation error. To avoid this error, the base-class reference must be cast to a derived-class type explicitly or must be converted using the as operator.
5. Summary of the Allowed Assignments Between Base-Class and Derived-Class Variables The is operator can be used to ensure that such a cast is performed only if the object is a derived-class object.
Overriding Methods in Derived Classes Only methods declared virtual override, or abstract can be overriden in derived classes
sealed Methods A method declared sealed in a base class cannot be overridden in a derived class. Methods that are declared private and static are implicitly sealed, because it's impossible to override them in a derived class.
Methods Declared override and sealed A derived-class method declared both override and sealed can override a base-class method but cannot be overridden in derived classes further down the inheritance hierarchy
Static Binding A sealed method's declaration can never change, so all derived classes use the same method implementation, and calls to sealed methods(and non-virtual methods) are resolved at compile time - this is known as static binding
Inlining the Code When the compiler optimizes code by removing calls to sealed methods and replaces them with the expanded code of their declarations at each method-call location because it knows that sealed methods cannot be overridden
sealed Classes A class that's declared sealed cannot be a base class(a class cannot extend a sealed class). All methods in a sealed class are implicitly sealed
Class string Class string is a sealed class. This class cannot be extended, so apps that use strings can rely on the functionality of string objects are specified in Framework Class Library
1. Interface Declarations Begin with keyword interface and can contains only abstract methods, abstract properties, abstract indexers, and abstract events. All interface members are implicitly declared both public and abstract.
2. Interface Declarations Each interface can extend one or more other interfaces to created a more elaborate interface that other classes can implement. Ex: public interface IPayable... {.. }
1. Implementing an Interface To use an interface, a class must specify that it implements the interface by listing the interface after the colon(:) in the class declaration. Ex: public class Invoice : IPayable
2. Implementing an Interface When a class implements an interface, the class makes a contract with the compiler stating that the class either will implement each of the method in the interface or will declare them as abstract.
Naming to Interface By convention, the name of an interface begins with I. This helps distinguish interfaces from classes, improving code readability
Declaring a Method in an Interface When declaring a method in an interface, choose a name that describes the method's purpose in a general manner, because the method may be implemented by a broad range of unrelated classes
1. Common Interfaces of the .NET Framework Class Library 1. Interface[IComparable]..Description[Used to allow objects of a class that implements the interface to be compared to one another. Contains one method, CompareTo, that compares the object that calls the method to the object passed as an argument]...
2. Common Interfaces of the .NET Framework Class Library 2. Interface[IComponent]..Description[Implemented by an class that represents a component, including GUI controls such as buttons and labels. Defines the behaviors that components must implement.]...
3. Common Interfaces of the .NET Framework Class Library 3. Interface[IDisposable]..Description[Implemented by classes that must provide an explicit mechanism for releasing resources. Classes that implement this interface provide a Dispose method that can be called to explicitly release resources.]...
4. Common Interfaces of the .NET Framework Class Library 4. Interface[IEnumerator]..Description[Used for iterating through the elements of a collection(such as an array) one element at a time.
5. Common Interfaces of the .NET Framework Class Library Contains method MoveNext to move to the next element in a collection, method Reset to move to the position before the first element, and property Current to return the object at the current location.].
1. Operator Overloading You can overload most operators to make them sensitive to the context in which they're used. Keyword operator, followed by an operator, indicates that a method overloads the specified operator.
2. Operator Overloading The first argument is the left operand, and the second is the right operand. Overload operators to perform the same function or similar functions on class objects as the operators perform on objects of simple types. Avoid non-intuitive use of operators.
Dynamic or Late Binding The process in which all virtual method calls are resolved at execution time, based on the type of the object to which the reference-type variable refers
Created by: TimC#Programming
Popular Engineering sets

 

 



Voices

Use these flashcards to help memorize information. Look at the large card and try to recall what is on the other side. Then click the card to flip it. If you knew the answer, click the green Know box. Otherwise, click the red Don't know box.

When you've placed seven or more cards in the Don't know box, click "retry" to try those cards again.

If you've accidentally put the card in the wrong box, just click on the card to take it out of the box.

You can also use your keyboard to move the cards as follows:

If you are logged in to your account, this website will remember which cards you know and don't know so that they are in the same box the next time you log in.

When you need a break, try one of the other activities listed below the flashcards like Matching, Snowman, or Hungry Bug. Although it may feel like you're playing a game, your brain is still making more connections with the information to help you out.

To see how well you know the information, try the Quiz or Test activity.

Pass complete!
"Know" box contains:
Time elapsed:
Retries:
restart all cards