Understanding Polymorphism
Polymorphism is the ability of the derived classes to share common functionality with base classes but still define their own unique behaviour.
You are developing an application that allows users to work with a different kind of polygon.
You have a collection that store a different type of polygons, such as Rectangle, Triangle and square.
Each polygon provides its own implementation of the method Draw.
When working with the collection you don't necessarily know exactly which shape you are working with, but you would like the correct Draw method to be invoked each time.
polymorphism enable you to do it.
You are developing an application that allows users to work with a different kind of polygon.
You have a collection that store a different type of polygons, such as Rectangle, Triangle and square.
Each polygon provides its own implementation of the method Draw.
When working with the collection you don't necessarily know exactly which shape you are working with, but you would like the correct Draw method to be invoked each time.
polymorphism enable you to do it.
Polymorphism allows the objects of the derived class to be treated at runtime as objects of the base class.
Understanding the Override and New Keywords
The override keyword replaces a base class member in a derived class.
3. You created a class named GeoShape. You defined a method called Area in the GeoShape class. This method calculates the area of a geometric shape. You want the derived classes of GeoShape to supersede this functionality to support the area calculation of additional geometric shapes. When the method Area is invoked on a GeoShape object, the area should be calculated based on the runtime type of the GeoShape object. Which keyword should you use with the definition of the Area method in the GeoShape class?
a) abstract
b) virtual
c) new
d) overrides
Answer: b
Difficulty: Medium
Section Reference: Understanding Polymorphism
Use the virtual keyword to define the Area method. When a virtual method is invoked, the runtime type of the object is checked for an overriding member. The overriding member in the most derived class is called, which might be the original member, if no derived class has overridden the member.
8. You are creating a new class named Sphere derived from the Shape class. The Shape class has the following code:
class Shape
{
public virtual void Area()
{
// additional code...
}
}
The Area method in the Shape class should provide new functionality but also hide the Shape class implementation of the Area method. Which code segment should you use to accomplish this?
a) class Sphere : Shape
{
public override void Area()
{
// additional code ...
}
}
b) class Sphere : Shape
{
public new void Area()
{
// additional code ...
}
}
c) class Sphere : Shape
{
public virtual void Area()
{
// additional code ...
}
}
d) class Sphere : Shape
{
public static void Area()
{
// additional code ...
}
}
Answer: b
Difficulty: Medium
Section Reference: Understanding Polymorphism
The new keyword creates a new member of the same name in the derived class and hides the base class implementation. The override keyword is not the correct answer because it replaces a base class member in a derived class.
You are C# developer who is developing a Windows application. You need to provide derived classes the ability to share common functionality with base classes but still define their own unique behavior. Which object-oriented programming concept should you use to accomplish this functionality?
a) encapsulation
b) abstraction
c) polymorphism
d) inheritance
Answer: c
Difficulty: Medium
Section Reference: Understanding Polymorphism
Polymorphism is the ability of derived classes to share common functionality with base classes but still define their own unique behaviour. Inheritance is a feature of object-oriented programming that allows you to develop a class once, and then reuse that code over and over as the basis of new classes.
Comments
Post a Comment