My contemplations and diurnal novelties are publicized here

Archive for January, 2013

Difference between an interface and abstract class


Interfaces

An interface is a contract: the guy writing the interface say “hey, I accept things looking that way“, and the guy using the interface says “OK, the class I write looks that way“.

An interface is an empty shell, there are only the signatures (name / params / return type) of the methods. The methods do not contain anything. The interface can’t do anything. It’s just a pattern.

E.G (pseudo code):

// I say all motor vehicles should look like that :

interface MotorVehicle

{

void run();

 

int getFuel();

}

 

// my team mate complies and write vehicle looking that way

class Car implements MotorVehicle

{

 

int fuel;

 

void run()

{

print(“Wrroooooooom”);

}

 

 

int getFuel()

{

return this.fuel;

}

}

Implementing an interface consume very little CPU, because it’s not a class, just a bunch of names, and therefor there is no expensive lookup to do. It’s great when it matters such as in embedded devices.

Abstract classes

Abstract classes, unlike interfaces, are classes. There are more expensive to use because there is a lookup to do when you inherit from them.

Abstract classes look a lot like interfaces, but they have something more : you can define a behavior for them. It’s more about a guy saying “these classes should look like that, and they got that in common, so fill in the blanks!”.

e.g:

// I say all motor vehicles should look like that :

abstract class MotorVehicle

{

 

int fuel;

 

// they ALL have fuel, so why let others implement that ?

// let’s make it for everybody

int getFuel()

{

return this.fuel;

}

 

// that can be very different, force them to provide their

// implementation

abstract void run();

 

 

}

 

// my team mate complies and write vehicle looking that way

class Car extends MotorVehicle

{

void run()

{

print(“Wrroooooooom”);

}

}

Implementation

While abstract classes and interfaces are supposed to be different concepts, the implementations make that statement sometimes untrue. Sometimes, they are not even what you think they are.

In Java, this rule is strongly enforced, while in PHP, interfaces are abstract classes with no method declared.

In Python, abstract classes are more a programming trick you can get from the ABC module and is actually using metaclasses, and therefore classes. And interfaces are more related to duck typing in this language and it’s a mix between conventions and special methods that call descriptors (the __method__ methods).

As usual with programming, there is theory, practice, and practice in another language 🙂

 

Copied from http://stackoverflow.com/questions/1913098/what-is-the-difference-between-an-interface-and-abstract-class


 

When to Use Inheritance or Interfaces


Inheritance is a useful programming concept, but it is easy to use inappropriately. Often interfaces do the job better.

Inheritance is a good choice when:

  • Your inheritance hierarchy represents an “is-a” relationship and not a “has-a” relationship.
  • You can reuse code from the base classes.
  • You need to apply the same class and methods to different data types.
  • The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  • You want to make global changes to derived classes by changing a base class.

These considerations are discussed in order below.

Inheritance and “Is a” Relationships

Two ways to show class relationships in object-oriented programming are “is a” and “has a” relationships. In an “is a” relationship, the derived class is clearly a kind of the base class. For example, a class namedPremierCustomer represents an “is a” relationship with a base class named Customer because a premier customer is a customer. However, a class named CustomerReferral represents a “has a” relationship with theCustomer class because a customer referral has a customer, but a customer referral is not a kind of customer.

Objects in an inheritance hierarchy should have an “is a” relationship with their base class because they inherit the fields, properties, methods, and events defined in the base class. Classes that represent a “has a” relationship with other classes are not suited to inheritance hierarchies because they may inherit inappropriate properties and methods. For example, if the CustomerReferral class were derived from the Customer class discussed previously, it might inherit properties that make no sense, such as ShippingPrefs and LastOrderPlaced. “Has a” relationships such as this should be represented using unrelated classes or interfaces. The following illustration shows examples of both “is a” and “has a” relationships.

relationship

Copied & edited from http://msdn.microsoft.com/en-us/library/27db6csx(VS.80).aspx#Mtps_DropDownFilterText

When to Use Interfaces

Interfaces are a powerful programming tool because they allow you to separate the definition of objects from their implementation. Interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects.

Flexibility in Implementation

There are several other reasons why you might want to use interfaces instead of class inheritance:

  • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
  • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
  • Interfaces are better in situations in which you do not need to inherit implementation from a base class.
  • Interfaces are useful in cases where you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.

Copied and edited from  http://msdn.microsoft.com/en-us/library/3b5b8ezk(v=vs.80).aspx

Best CMS for Newspaper Sites


The two most specialized CMS for newspapers are

  1. Escenic (they use the .ece extension on URLs)
    1. The Globe and Mail
    2. The Independent
    3. The Dayton Daily News.
    1. Columbus Dispatch,
    2. The Chicago Sun Times,
    3. The New York Post

    1. The New York Times
    2. The Washington Times
    1. The Economist,
    2. New York Observer,
    3. Florida Times-Union
    4. List of news sites using Drupal
  2. WordPress
    1. Wall Street Journal,
    2. Express & Star
    3. The Gazette.
    1. The Boston Globe
  3. Newscoop

References:-

http://www.mequoda.com/articles/website-architecture/what-is-the-best-cms-for-newspapers/

%d bloggers like this: