Understanding Interface
Interface used to establish contracts through which objects can interact with each other without knowing the implementation details.
- Interfaces are defined by using the interface keyword.
- An interface definition consists a set of signature for methods, delegates, events or indexers.
- An interface cannot consist of any data fields or any implementation details such as methods body.
Icomparable interface
- A common interface defined in the system namespace is the Icomparable.
- Icomparable definition:
{
int compareTo(object obj);
}
- The Icomparale has a single method that accepts an object and returns an int.
- The return value of this method indicates the result of comparing the given parameter with the current object.
CompareTo method documentation
- If the instance is equal to the parameter, CompareTo returns 0.
- If the parameter value is less then the instance or if the parameter is equal to null then, CompareTo returns a positive value.
- If the parameter value is greater then the instance, then a negative value is returned.
- if the parameter is not of the compatible type, then an ArgumentException is thrown.
Interface vs Abstract
Interface and abstract are similar but there are some differences.
- Implementation, abstract provide incomplete implemention, interface provides no implemention.
- inherite, A class can inhirite a multiple interface but limit to one base class.
- When you have an "is-a" relationship between the two concepts then you use abstract, on the other hand, if you there is no "is-a" relationship between then you can use interface.
You are creating a new class named Polygon. You write the following code:
class Polygon : IComparable
{
public double Length { get; set; }
public double Width { get; set; }
public double GetArea()
{
return Length * Width;
}
public int CompareTo(object obj)
{
// to be completed
}
}
You need to complete the definition of the CompareTo method to enable comparison of the
Polygon objects. Which of the following code segments should you use?
a) public int CompareTo(object obj)
{
Polygon target = (Polygon)obj;
double diff = this.GetArea() - target.GetArea();
if (diff == 0)
return 0;
else if (diff > 0)
return 1;
else return -1;
}
b) public int CompareTo(object obj)
{ Polygon target = (Polygon)obj;
double diff = this.GetArea() - target.GetArea();
if (diff == 0)
return 1;
else if (diff > 0)
return -1;
else return 0;
}
c) public int CompareTo(object obj)
{
Polygon target = (Polygon)obj;
if (this == target)
return 0;
else if (this > target)
return 1;
else return -1;
}
d) public int CompareTo(object obj)
{
Polygon target = (Polygon)obj;
if (this == target)
return 1;
else if (this > target)
return -1;
else return 0;
}
Answer: a
Difficulty: Medium
Section Reference: Understanding Interfaces
The return value of the CompareTo method indicates the result of comparing the given
parameter with the current object. According to the documentation of the CompareTo
method,
• If the instance is equal to the parameter, CompareTo returns 0.
• If the parameter value is less than the instance or if the parameter is null, a positive
value is returned.
• If the parameter value is greater than the instance, a negative value is returned.
• If the parameter is not of the compatible type, an ArgumentException is thrown.
5. You need to provide printing functionality to several of your classes. Each class’s algorithm for printing will likely be different. Also, not all the classes have an “is-a” relationship with each other. How should you support this functionality?
a) Add the print functionality to a base class with the public access modifier.
b) Have all classes inherit from an abstract base class and override the base-class method to provide their own print functionality.
c) Have all the classes inherit from a base class that provides the print functionality.
d) Create a common interface that all classes implement.
Answer: d
Difficulty: Medium
Section Reference: Understanding Interfaces
You should create a common interface that is implemented by all classes. Interfaces are used to establish contracts through which objects can interact with each other without knowing the implementation details.
- Implementation, abstract provide incomplete implemention, interface provides no implemention.
- inherite, A class can inhirite a multiple interface but limit to one base class.
- When you have an "is-a" relationship between the two concepts then you use abstract, on the other hand, if you there is no "is-a" relationship between then you can use interface.
You are creating a new class named Polygon. You write the following code:
class Polygon : IComparable
{
public double Length { get; set; }
public double Width { get; set; }
public double GetArea()
{
return Length * Width;
}
public int CompareTo(object obj)
{
// to be completed
}
}
double diff = this.GetArea() - target.GetArea();
if (diff == 0)
return 1;
else if (diff > 0)
return -1;
else return 0;
}
c) public int CompareTo(object obj)
{
Polygon target = (Polygon)obj;
if (this == target)
return 0;
else if (this > target)
return 1;
else return -1;
}
d) public int CompareTo(object obj)
{
Polygon target = (Polygon)obj;
if (this == target)
return 1;
else if (this > target)
return -1;
else return 0;
}
Answer: a
Difficulty: Medium
Section Reference: Understanding Interfaces
The return value of the CompareTo method indicates the result of comparing the given
parameter with the current object. According to the documentation of the CompareTo
method,
• If the instance is equal to the parameter, CompareTo returns 0.
• If the parameter value is less than the instance or if the parameter is null, a positive
value is returned.
• If the parameter value is greater than the instance, a negative value is returned.
• If the parameter is not of the compatible type, an ArgumentException is thrown.
5. You need to provide printing functionality to several of your classes. Each class’s algorithm for printing will likely be different. Also, not all the classes have an “is-a” relationship with each other. How should you support this functionality?
a) Add the print functionality to a base class with the public access modifier.
b) Have all classes inherit from an abstract base class and override the base-class method to provide their own print functionality.
c) Have all the classes inherit from a base class that provides the print functionality.
d) Create a common interface that all classes implement.
Answer: d
Difficulty: Medium
Section Reference: Understanding Interfaces
You should create a common interface that is implemented by all classes. Interfaces are used to establish contracts through which objects can interact with each other without knowing the implementation details.
Comments
Post a Comment