Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Archive for the '.NET' Category

Oct 10 2008

Abstract Class versus Interface

Published by Prajwal Tuladhar under .NET, C#, Patterns, Programming

In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declared subtype.
Interface generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer. Because interfaces are a form of indirection, some additional overhead is incurred versus direct communication. -Wikipedia

In Java, .NET and PHP 5 there are three ways to create and object i.e. inheritance, composition and interface.

  • Inheritance defines “is-a” relationship. Example: Student and Programmer is a Person
  • Composition defines “has-a” relationship. Example: Class Student contains class Book
  • Interface only models the behavior of an object. Example: Student and Programmer are both Nameable and both may have actions.

In Java, .NET and PHP 5, multiple inheritance (child class with more than one parent class) is not allowed so, interface can be used as a powerful tool to separate implementation. Interface is not a strict “is a” relationship. Abstract class represents some sort of implementation and it is a strict “is a” relationship. For example: A dog is a mammal and a reptile is not a mammal showing strict relationship whereas both dog and reptile may be nameable and both may have some actions.

abstract interface example

As we know that interface and abstract class both provide abstract methods so, making the best use of them is crucial in the object oriented paradigm. Abstract class provides both concrete and abstract methods whereas interface provides only abstract methods. Lets go through an example:

Assume that we have an abstract class Person and its concrete implementation class Student and Programmer. We have an interface called IWork that may or may not be implemented by the concrete classes Student and Programmer. We also have a composite class Address, though it is not required while distinguishing between interface and abstract class, using composite class will help us to clarify the scenario.

Class Diagram

Abstract Interface Class Diagram

Abstract Interface Class Diagram

C# Code

Abstract Class - Person

Abstract Class - Person

Interface - IWork

Interface - IWork

Class Address

Class - Address

Class - Programmer

Class - Programmer

Class - Program

Class - Program

Conclusion

  • Classes in a strict inheritance relationship must be related.
  • Interfaces can be used for classes that are not related. In the above example, only Programmer class is implemented but it is not necessary that Student also implements the IWork interface.
  • An interface never provides any implementation, only behavior.

References

You can download the example from here.

No responses yet

Jul 18 2008

ASP.NET MVC Preview 4 Released

Published by Prajwal Tuladhar under .NET, Links

Microsoft has released ASP.NET MVC Preview 4. It has one of the most awaited release since, Microsoft’s first attempt to make MVC based framework (not Web Forms). It can be downloaded from Codeplex.

Useful Links:

No responses yet

Jul 08 2008

Handy cheat sheet aka reference guide collection links

Published by Prajwal Tuladhar under .NET, Links

According to the author, this is a collection of cheat sheets and reference guides links related to various tech components and languages from VIM to CSS to Javascript to LINQ to Design Patterns especially dedicated to .NET and ALT.NET developers. But really these links could prove to be handy to all developers and IT professionals. Here is the link.

No responses yet

Jul 07 2008

A cool programming concept book

Published by Prajwal Tuladhar under .NET, Links

One can download really cool programming foundation bool from codebetter.com. Moreover the book is free and it is worth to read for all developers of my level i.e. college students or enthusiast learner. The link is here. Happy programming and reading ;)

No responses yet

May 13 2008

LINQ to XML Object Hierarchy Figure

Published by Prajwal Tuladhar under .NET, LINQ

I have create a PDF file describing the hierarchial structure of LINQ to XML. You can download it from here.

Technorati Tags: ,

No responses yet

Next »

RSS Feed

Related Posts


follow infynyxx at http://twitter.com