Abstract vs Interface

July 2, 2009

ü  Abstract class is a class which contains one or more abstract methods, which has to be implemented by sub classes. An abstract class can contain no abstract methods also i.e. abstract class may contain concrete methods. A Java Interface can contain only method declarations and public static final constants and doesn’t contain their implementation. The classes which implement the Interface must provide the method definition for all the methods present.

ü  Abstract class definition begins with the keyword “abstract” keyword followed by Class definition. An Interface definition begins with the keyword “interface”.

ü  Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses. Interfaces are useful in a situation when all its properties need to be implemented by subclasses

ü  All variables in an Interface are by default – public static final while an abstract class can have instance variables.

ü  An interface is also used in situations when a class needs to extend another class apart from the abstract class. In such situations it’s not possible to have multiple inheritances of classes. An interface on the other hand can be used when it is required to implement one or more interfaces. Abstract class does not support Multiple Inheritance whereas an Interface supports multiple Inheritances.

ü  An Interface can only have public members whereas an abstract class can contain private as well as protected members.

ü  A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.

ü  The problem with an interface is, if you want to add a new feature (method) in its contract, then you MUST implement those methods in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass

ü  Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast

ü  Interfaces are often used to describe the peripheral abilities of a class, and not its central identity,

 E.g. an Automobile class might implement the Recyclable interface, which could apply to many otherwise totally unrelated objects.

Note: There is no difference between a fully abstract class (all methods declared as abstract and all fields are public static final) and an interface.

Note: If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class. If all they
share is a set of method signatures, then tend towards an interface.

Similarities:
Neither Abstract classes nor Interface can be instantiated.


SPRING 3.0

January 28, 2009

I’m pleased to inform that SPRING 3.0 M1 is finally available to download.

This release has several major changes like

Ø  revised project layout and build system with module-based sources

Ø  updated entire codebase for Java 5 code style (generics, varargs)

Ø  updated to JUnit 4.5 and JRuby 1.1

Ø  introduced Spring EL parser (org.springframework.expression package)

Ø  introduced support for #{…} expressions in bean definitions

Ø  introduced expression-enabled @Value annotation for embedded expressions

Ø  introduced @PathVariable annotation for URI template handling in MVC handlers

Ø  introduced default value support for @RequestParam in MVC handlers

Ø  introduced @RequestHeader annotation for HTTP header access in MVC handlers

Ø  introduced AbstractAtomFeedView and AbstractRssFeedView base classes

Ø  introduced <spring:url> and <spring:param> JSP tags

Ø  Support EL and REST

Ø  new support for the Java EE 6 JPA (Java Persistence API) 2, in addition to already existing support for JDBC, Hibernate, iBatis, and much more.

Ø  Use the now fully integrated SpringSecurity framework, formerly Acegi.

 

As well as various minor changes.

This version is specifically based on JAVA 6 and J2EE 5 .but its compatibility with JAVA 5 and J2EE1.4 or above.

 


Encapsulation

November 12, 2008

Encapsulation refers to an object hiding its attributes behind its operations.

 

For more information:

It seals the attributes in a capsule, with operations on the edge.

Hidden attributes are said to be private.

 

 

Way to think of encapsulation is to imagine that objects are courteous to one another. If you wanted to borrow some money from a colleague to buy food in the staff canteen, you wouldn’t grab their wallet and look through it to see if they had enough cash.

Instead, you would ask them whether they could lend you some money and they would look

in their own wallet.