Monday, March 25, 2013

Abstract Class vs Interface

Conceptually, abstract classes are very similar to interfaces with a few differences.  One way to think of the two is like this:  An abstract class can be defined as "Is a".  An interface can be defined as "Can do".

When you create an abstract class you are creating a thing that something else would be.  For example, "car" could be an abstract class and everything that inherits from it is a car.  When you create an interface you are defining what something can do.  If you define an interface of "drive", anything that implements that is saying that their object can drive.  The interface doesn't say how to drive it just says your object needs to be able to drive and it defines what things something can do to be able to drive, just not how they do it.

Interface
Interfaces cannot implement method implementations.  They can contain abstract methods.

An abstract method defines a method signature (types of parameters it takes and the type it returns), but does not define the implementation or how the method does what it does.  The implementation will be coded in the class that uses the interface.  Many classes can use an interface and each of them may implement a certain method of the interface entirely different.

Interfaces do not have constructors or destructors (special methods that fire upon creation or disposal of an object to allow the class to initialize things or clean up things)

A class can implement multiple interfaces.

An interface is sometimes called a contract or promise.

An interface cannot extend classes, but they can extend other interfaces.


Interfaces can be used as bridges across classes inherited from different base classes. They can also be used to make it easier for third parties to communicate with your classes. 



Abstract Class
Abstract classes must contain at least one abstract method.  Abstract classes can also contain concrete methods or methods that already have an implementation defined.

Just like any other class abstract classes contain constructors and destructors.

A class can not inherit from multiple classes, not even multiple abstract classes.

Abstract classes cannot be instantiated directly.  They must be inherited from and then the class that inherits from them can be implemented.  The abstract keyword indicates that the class is defined for inheritance. 

To create an abstract class you use the abstract keyword:


public abstract class MyAbstractClass {}


Abstract classes can use interfaces, but every class that inherits from the abstract class will be bound by the interface, which means they must implement every method and property in the interface.

An abstract class is used for creating a class hierarchy. 


Friday, March 22, 2013

Array vs ArrayList vs Generic List vs LinkedList in .Net

Array vs ArrayList vs Generic List vs LinkedList in .Net

Arrays

Arrays cannot shrink or grow unless you copy the array to an entirely new array of a different size.  Once the size of an array is declared that is the size it must remain without heavy overhead.

Arrays can contain objects or primitives.


ArrayLists
An ArrayList uses a dynamically expanding Array internally, so there can be a performance hit when expanding past the size of its internal Array.  The size of the internal array of an array list does not change, in order to increase performance.  When an element is added beyond the capacity of the ArrayList the internal array is copied and a new array of twice the number of elements is created.  Thus reducing the need to expand the internal array for a while.  This uses more memory, but can make it perform more quickly.  Array lists also start with an internal Array with ten elements, also to reduce the need to increase its size.

ArrayLists can only contain objects.

List
List is a generic implementation of ArrayList.  ArrayList appears to be being deprecated.

LinkedList
LinkedList can have performance issues since it can cause memory fragmentation.  There is no set bounds for a LinkedList, so data in the list can end up anywhere in memory.  This can be confusing, since we also said that ArrayLists can have performance issues due to recreating the internal Array on inserts.

You have to determine when you need to gain.  Do you need to free up memory or need to have fast inserts and deletions, then LinkedList.  Do you need fast access to items and will be doing very few inserts, then perhaps Array or List is what you want.

Memory
ArrayLists can use 100s of percent more memory than List.

Types

Under the covers ArrayList is an array of type object[].  List is an array of whatever specific type T you make it.  This can allow for better memory usage and a more precise implementation in code.

Performance

Arrays and Lists allow for very fast read since they are a fix size making each piece of data stored right next to each other in memory.

LinkedList can have performance issues since it can cause memory fragmentation.  There is no set bounds for a LinkedList, so data in the list can end up anywhere in memory.