Regarding interface and abstract class
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Dhanu_Sudha
Posted On:   Sunday, March 10, 2002 10:09 PM

Can anybody help me out in this case?
When should we use Interface?
At what situation we should use
abstract class?

I need a clear idea regarding this

Re: Regarding interface and abstract class

Posted By:   Bozidar_Dangubic  
Posted On:   Monday, March 11, 2002 06:18 AM

use interface to define a contract between a class and any other class component that is to make use of it. interface is a client view of the class/component that you are developing. you can use abstract class when there are common properties and behavior for classes that will be extending from abstract class. so to avoid code duplication that would exist in case of an interface, you can define an abstract class, implement common behavior and define specific behavior as abstract to make the classes that will extend abstract class implement the abstract methods. well, let's do an example. say you are creating a class/component which will be use to persist data to the database. so here is the interface

public interface DatabaseFacade
{
public Key create(Object obj) throws DatabaseException;
public Object load(Key key) throws DatabaseException;
public void update(Object obj) throws DatabaseException;
public void remove(Key key) throws DatabaseException;
}

there would be many more methods here, but for simplicity of the example I chose only those four. Suppose now that you need to create a different implementation of this interface, one for each different database such as Oracle, mySQL, Informix, etc... Each of the classes implementating the interface would have to look something like this.

public class OracleDatabaseFacade implements DatabaseFacade
{
public Key create(Object obj) throws DatabaseException
{
// implementation here
}
public Object load(Key key) throws DatabaseException
{
// implementation here
}
public void update(Object obj) throws DatabaseException
{
// implementation here
}
public void remove(Key key) throws DatabaseException
{
// implementation here
}

}

Now suppose that all databases can create(), remove() and update() in the same manner (since they are using the same SQL standard). If this is the case, code to create(), remove() and update() would be the same and duplicated in each implementation of our DatabaseFacade class, i.e. OracleDatabaseFacade, InformixDatabaseFacade, MySQLDatabaseFacade will have the exact same implementation of create(), remove() and update() methods. so to avoid this code replication, you can create an abstract class like AbstractDatabaseFacade

public abstract class AbstractDatabaseFacade
{
public Key create(Object obj) throws DatabaseException
{
// implementation here
}
public void update(Object obj) throws DatabaseException
{
// implementation here
}
public void remove(Key key) throws DatabaseException
{
// implementation here
}


}


Now, the implementation classes need to only implement load() method as they inherit common functionality of create(), remove() and update() from AbstractDatabaseFacade.

public class OracleDatabaseFacade extends AbstractDatabaseFacade
{
public Object load(Key key) throws DatabaseException
{
// implementation here
}
}
About | Sitemap | Contact