Geeks With Blogs
Rodney Vinyard - .NET & SQL Developer When all is said and done, more will be said than done
  • Encapsulation – Only nececesary portions of class fields and methods should be publicly accessible.  Hide inner workings of an object.
  • Inheritance – ability of derived class to inherit the properties & methods from base class. The derived class can add to and override base class behavior.
  • Polymorphism – the ability for related classes to have the methods or functions with the same name and signatures but different implementations.
  • Aggregation/Composition - groups of objects being used to make up a greater object
  • Interfaces – derived Classes that inherit interfaces are required to have certain methods, fields and/or properties defined and implemented. This allows related classes to have a consistent manner of using them. For example, all the .NET collection classes inherit from the IEnumerator interface, forcing them to have the methods MoveNext(), Reset() and Current property.
  • Reference and value types
    • Structs are value types, while classes are reference types
    • Stack
      • What is executing
    • Heap
      • Heap -> pointers to ref types

Value Types –

·          if a field, -> stack, else (part of an object), -> heap:

·          box value type to reference type

In C#, all the "things" declared with the following list of type declarations are Value types (because they are from System.ValueType):

  • bool , byte , char , decimal , double , enum , float , int , etc

Reference Types

·          Heap

·          Unbox box reference type to value type

  • string
  • class
  • interface
  • delegate
  • object

Garbage Collection

1.     stop all running threads (a FULL STOP),

2.     find all objects in the Heap that are not being accessed by the main program and delete them. 

3.     reorganize all the objects left in the Heap to make space and adjust all the Pointers to these objects in both the Stack and the Heap. 


Inheritance

A C# class may only subclass—inherit from—one other class. Therefore, by inheriting from (subclassing) an abstract class, the derived class has used up its ability to participate in a meaningful type hierarchy.

On the other hand, a class can implement—inherit from—any number of interfaces. And, it can still inherit from (subclass) a base class which makes sense.

[edit]

Single inheritance

C# supports single inheritance: C# does not support multiple inheritance from multiple classes like C++ does. But, by using abstract classes and interfaces, C# programs can achieve most of the same functionality without the confusion and maintenance problems associated with multiple inheritance.

[edit]

Value type polymorphism

.NET value types are objects descending from Object; but, they cannot inherit from other types. They can implement interfaces. Thus, primitives—such as Int32— can implement the IComparable interface, for example, making them comparable.

[edit]

Separation of contract and implementation

Interfaces separate the syntax rather than the semantic contract from the implementation. Classes can be designed to decouple the semantic contract from the implementation. For example, abstract classes can be separated in a different assembly than their concrete implementations.

[edit]

Versioning

An abstract class can contain an interface plus implementations. This simplifies versioning. An abstract class can be extended by adding new nonabstract methods with default implementations. Also, a convenience method is easily added to an abstract class.

An interface cannot be modified without breaking its contract with the classes which implement it. Once an interface has been shipped, its member set is permanently fixed. An API based on interfaces can only be extended by adding new interfaces.

[edit]

Design flexibility

Interfaces offer more design flexibility; precisely because, they can be implemented by any class regardless of its type hierarchy.

[edit]

Visual C# Best Practices

*                   Use abstract classes and interfaces in combination to optimize your design trade-offs.

[edit]

Use an abstract class

*                   When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. (COM was designed around interfaces.)

*                   Use an abstract class to define a common base class for a family of types.

*                   Use an abstract class to provide default behavior.

*                   Subclass only a base class in a hierarchy to which the class logically belongs.

[edit]

Use an interface

*                   When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.

*                   Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.

*                   Use an interface to design a polymorphic hierarchy for value types.

*                   Use an interface when an immutable contract is really intended.

*                   A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.

[edit]

Summary

The following table compares the features of abstract classes and interfaces.

Abstract class

Interface

Derived classes exhaust their single base class inheritance option.

Classes can implement multiple interfaces without using up their base class option. But, there are no default implementations.

Cannot be instantiated except as part of subclasses. Only derived classes can call an abstract class constructor.

Cannot be instantiated.

Defines abstract member signatures which derived classes must implement. Otherwise, the derived class itself will be abstract.

Defines abstract member signatures—all of which—implementing classes must implement. Otherwise, a compiler error results.

New non-abstract members may be added that derived classes wil inherit without breaking version compatibility.

Extending an interface with new members breaks version compatibility.

Optionally, provide default (virtual) member implementation.

All members are virtual and cannot provide implementations.

Can include data fields.

Cannot include data fields. However, abstract properties may be declared.

 

 

See also

*                   .NET Type Design Guidelines

*                   Classes, Structs, and Objects

*                   Implementing Interfaces

*                   Test for an interface implementation

*                   Using Inheritance Effectively

Retrieved from "http://en.csharp-online.net/Should_I_use_an_abstract_class_or_an_interface%3F"

Posted on Friday, July 20, 2007 3:14 AM Interview Questions | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Object Oriented

# re: Interview Questions
Requesting Gravatar...
I don't get why you mention pointers if this is all about C#. Isn't half the point of C# that you can avoid managing memory at such a low level? Why can't you rely on the built-in garbage collection classes to take out the trash for you?
Left by Alex Bransky on Jul 20, 2007 9:17 AM

# re: Object Oriented
Requesting Gravatar...
I think this issue ought to have careful examination of the question. One cannot be too careful. To be more precise I completely consent with all what you said.
Left by service writing on Jul 24, 2010 6:16 AM

# re: Object Oriented
Requesting Gravatar...
I have read a few of the articles here and I really like your style of blogging. I bookmarked it and will be checking back soon. Please check out my site as well and let me know what you think.
Left by thesis on Feb 03, 2011 12:41 PM

# re: Object Oriented
Requesting Gravatar...
Hello my friend! I want to say that this article is awesome, nice written and include almost all vital infos. I would like to see more posts like this .
Left by business dissertation on Mar 22, 2011 12:04 PM

# re: Object Oriented
Requesting Gravatar...
Ha, nicely put! I'm so glad to read this construction view. I'm really surprised to read this article views. I think this is an interesting placement, keep it up though!
Left by SEO Marketing services on Aug 30, 2011 4:53 AM

Your comment:
 (will show your gravatar)


Copyright © Rodney Vinyard | Powered by: GeeksWithBlogs.net