Base Class Usage Guidelines 
Author Message
 Base Class Usage Guidelines

I have read the topic: "Base Class Usage Guidelines" in the .NET Framework
General Reference.
I have found this excerption interesting:

--
Base Classes vs. Interfaces
An interface type is a partial description of a value, potentially supported
by many object types. Use base classes instead of interfaces whenever
possible. From a versioning perspective, classes are more flexible than
interfaces. With a class, you can ship Version 1.0 and then in Version 2.0
add a new method to the class. As long as the method is not abstract, any
existing derived classes continue to function unchanged.

Because interfaces do not support implementation inheritance, the pattern
that applies to classes does not apply to interfaces. Adding a method to an
interface is equivalent to adding an abstract method to a base class; any
class that implements the interface will break because the class does not
implement the new method.
Interfaces are appropriate in the following situations:
Several unrelated classes want to support the protocol.
These classes already have established base classes (for example, some are
user interface (UI) controls, and some are XML Web services).
Aggregation is not appropriate or practical.
In all other situations, class inheritance is a better model.
--

Doesn't that mean that it would be more convenient from the versioning
perspective to provide in some cases
ability to define default implementation for the interfaces?
--------------------------------
Nesterovsky Vladimir



Thu, 26 May 2005 20:38:08 GMT  
 Base Class Usage Guidelines

Quote:
> Doesn't that mean that it would be more convenient from the versioning
> perspective to provide in some cases
> ability to define default implementation for the interfaces?

Use abstract classes for this. These can contain implemtnations, and if they
are virtual methods, they can be overridden. Interfaces is more like a
"contract" or "guideline" for implementation. Keep in mind that once you've
defined a public interface, you can NEVER change it because it'd crash
current applications or classes using the interface.


Fri, 27 May 2005 03:25:40 GMT  
 Base Class Usage Guidelines
Vladimir..... Joshua Bloch (Effective Java) argues with five pages of
logic as to why he favors interfaces over abstract classes.

"to summarize, an interface is generally the best way to define a
type that permits multiple implementations. An exception to  this
rule is the case where ease of evolution is deemed more important  
than flexibility and power."
Remember, C# supports single inheritance of implementation and
multiple inheritance of interfaces.
http://www.aspfree.com/authors/jeff_louie/OOP/twisted2.asp

Regards,
Jeff

Quote:
>Use base classes instead of interfaces whenever possible.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Fri, 27 May 2005 07:28:42 GMT  
 Base Class Usage Guidelines

Quote:
> Use abstract classes for this. These can contain implemtnations, and if
they
> are virtual methods, they can be overridden. Interfaces is more like a
> "contract" or "guideline" for implementation. Keep in mind that once
you've
> defined a public interface, you can NEVER change it because it'd crash
> current applications or classes using the interface.

I general I easely can think of contract that is evolutioning by adding new
methods and properties. In order to keep this contract compatible I could
define define implementations for these new methods.

The second issue here is that the interface in general is implemented
by several diferent classes and it is frequent enough that all they
implement (repeat implementation from class to class) some methods
and properties using methods and properties of this interface.

--
Vladimir Nesterovsky



Fri, 27 May 2005 13:37:52 GMT  
 Base Class Usage Guidelines

Quote:
> "to summarize, an interface is generally the best way to define a
> type that permits multiple implementations. An exception to  this
> rule is the case where ease of evolution is deemed more important
> than flexibility and power."
> Remember, C# supports single inheritance of implementation and
> multiple inheritance of interfaces.

I am talking exactly about that why to create trouble with interface
evolution? Why not to allow default implementation of interface's
methods and properties using another methods and properties.
I believe this does not obligate to change almost anything in architecture.
But only provide possiblity to evolution the interface.
--
Vladimir Nesterovsky



Fri, 27 May 2005 13:44:35 GMT  
 Base Class Usage Guidelines
Vladimir... You can use an interface when you are certain that the
public contract will not change. I don't see how this differs from
COM programming where you are pretty much stuck with the
public methods in future versions and forever kick yourself for
being so short sided at the start <g>. From  Effective Java:
Existing classes can be esily retrofitted to implement a new
interface.
Interfaces are ideal for mixins.
Interfaces allow the construction of nonhierarchical type
frameworks.
Interfaces enable safe, powerful functional enhancements...
without "inheritance".
You can provide the virutes of interfaces and abstract classes by
providing an abstract skeletal implementation class to go with
each nontrivial interface that you export.

However...  It is far easier to evolve an abstract class than it is to
evolve an interface.

Regards,
Jeff

Quote:
>Why not to allow default implementation of interface's

methods and properties using another methods and properties.
I believe this does not obligate to change almost anything in
architecture.
But only provide possiblity to evolution the interface.<

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Fri, 27 May 2005 16:08:58 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Virtual Base Class Usage

2. Protected member in a base class of a base class

3. class class-name: base-class : interface

4. Trying to add a class with no base class in Class Wizard

5. Class Naming Guidelines

6. ECC-based base class template code

7. Memory usage: ATL based COM Server Service

8. VC++ class inheriting C# class loses base functions

9. TreeNode : Cast base class into sub class

10. Converting from Base Class to Sub-Class

11. How to Stop Derived Class from overriding base class method

12. std::vector holding different class types sharing a same base class

 

 
Powered by phpBB® Forum Software