Friday, May 29, 2009

Difference between Abstract Class and Interface ?

Dev Palmistry


Abstract Class:

# IS-A relationship.
# e.g. Student IS A Person, Employee IS A Person.
# cannot be instantiated.
# normally used for framework-type library classes: providing default behavior for some of its class members, but forcing the developer to implement

others.
# It has to be inherited for use .
# You need to INHERIT to use an abstract class.
# Attempting to instantiate an object of an abstract class retults in a compilation error
# Any Class with abstract method or property in it must be declared abstract
# Abstract will allow you to set the access specifier. Ex:- (private,public, protected, internal).
# Abstract will allow you to implement the body in abstract methods.
# Situation where functionality may add/remove "Abstract Class" is best solution.
# Eg. Creating Base Classes,Creating Base for your project where functionality may be added or remove or can be override if needed.Mostly common task are

move to base class and if required than can be overrided.


# in Abstract class you can Declare the Constructors,fields,methods,indexes,destructors etc

# practical example for abstract class:

we can use account class in bank as abstract class.inreality

there will be no account we can use .but only sbaccount and current account will be exist.so we can use account class as abstract

class and we can inherits this class in sbaccount class,current account class


# Example:
pulic Abstract Class A
{
public void Hi();

public void Hello()
{
Console.WriteLine("hello method");
}

}
public Class B
{
public overid void Hi()
{
Console.WriteLine("hi method");
}



2. Abstract Class: Allows common functionality to be shared across similar objects


Of course, don't use an abstract class in cases where functionality doesn't need to be shared.





Interface:

# CAN-DO relationship.
# e.g. Student CAN enrol, Student CAN submit assignment.
# cannot be instantiated.
# group of related methods with empty bodies .
# You need to IMPLEMENT to use an interface.

# The implementation of an interface is left completely to the developer.
# Interfaces can contain only the signature of a method but no body.
# Interfaces are used to declaring functionality.
# By default all interface methods are public.
# Situation where we have fix requirement "Interface" is best solution.
# Eg. Plugin .Where Pluging can be loaded dynamically and executes its functionality.
Thus that Plugin must follow or implement certain functionality.
# In class you can implement the interface method, but can’t implement the body in Interface method.
# in interface you can not Declare the Constructors,fields,methods,indexes,destructors
# Examples:'
//mutltiple inhiritance
Interface IA
{
void cat();
void Dog();
}
Interface IB
{
void door();
void tyres();
}
public class C:IA,IB
{
public void cat()
{
Console.WriteLine("hello cat method");
}
public void dog()
{
Console.WriteLine("hello dog method");
}
public void door()
{
Console.WriteLine("hello door method");
}
public void tyres()
{
Console.WriteLine("hello tyres method");
}
}



Exp : 1. Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality. For example, let bird

be a class with a method fly(). It will be ridiculous for an aeroplane to inherit from bird class just because it has the fly() method. Rather the fly()

method should be defined as an interface and both bird and aeroplane should implement that interface.


Exp 2. Hi what suits my case !! Say a real estate builder is constructing an apartment with many flats.All the rooms in the flats have the same design,except

the bedroom. The bedroom design is left for the ppl who would own the flats i.e; the bedRooms can be of different designs for different flats.
I can achieve this through an abstract class like below:

public abstract class Flat
{
//some properties

public void livingRoom(){
//some code
}

public void kitchen(){
//some code
}

public abstract void bedRoom();

}

An implementation class would be as follows:


public class Flat101 extends Flat
{
public void bedRoom() {
System.out.println("This flat has a customized bedroom");
}

}



# Interface is without implementation, just interface or virtual methods.

# As per my understanding, it is can-do or is-do relationship. Like, from .NET framework, there are many interface like IComparer, ISortable etc. So, it is

something like, classes derived from the interface CAN-DO these things.


Defference :



# A class can inherit one or more interfaces, but only one abstract class.

# An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members

of the interface must override to its derived class.

# The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.

# Interface are similar to abstraction classes.However , interfaces represent the higest level of abstraction in Object- oriented programming.This is because

all the methods in an interface are abstract and do not have implementation.In contrast ,the abstract classes might contain a method that has a body.

# 1).Interface have only signature. whereas Abstract class have signature and definition both r allow. 2). Interface have not allow modifier access. whereas

Abstract class are allowed modifier access. 3).Thurogh the Interface we can create the Multiple Inheritance whereas Abstract class are not allow the Multiple

Inheritance. 4).Interface is slower compare Abstract class.

# An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can

implement methods but an interface can not implement methods. · An abstract class can contain fields, constructors, or destructors and implement properties.

An interface can not contain fields, constructors, or destructors and it has only the property's signature but no implementation. · An abstract class cannot

support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several interfaces but only one abstract class. · A

class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class. · Various

access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.

# Interface:- 1. Interfaces are used to declaring functionality. 2. By default all interface methods are public. 3. In class you can implement the interface

method, but can’t implement the body in Interface method. Abstract:- 1. Abstract will allow you to set the access specifier. Ex:- (private,public, protected,

internal). 2. Abstract will allow you to implement the body in abstract methods. 3. You can inherit the abstract methods in classes

# • Abstract Class
Cannot be instantiated.
Must be inherited and its methods should be overridden.
It have some concreate methods.
Access modifiers allowed.


• Interface
Have definition of a method not implementation. (implement through class)
Multiple inheritance possible through Interface only
Only Public Access modifier only allowed. Defaultly Public
No need of virtual overridden.
It’s used for to define a set of properties, methods and events.

# Following are the difference between abstract and interface,

1>Abstract class having method declaration as well as method method definition whereas interface having method declaration only.

2>Abstract class are known as partial abstract class whereas interface is known as fully abstract class.

3>Abstract class features we have to inherit to the child class whereas interface features we have to implement in the child classes.

4>Abstract class support access specifiers whereas interface doesn't support access specifiers.

5>Abstract class have normal variable as well as constant variable whereas interface have only constant variables.(Discuss it)

6>We can write constructor in abstract class whereas we can't write constructor in interface.

# Interfaces are similar to abstract classes.However,interface represent the highest level of abstraction in object-oriented programming.This is because all

the methods in an interface are abstract and do not have implementation.In contrast,the abstract classes that are created using Abstract keyword might

contain a method that has a body.

# Abstract Class vs. Interface
· An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract

class can implement methods but an interface can not implement methods.

· An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or

destructors and it has only the property's signature but no implementation.

· An abstract class cannot support multiple inheritance, but an interface can support multiple inheritance. Thus a class may inherit several

interfaces but only one abstract class.

· A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.

· Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.

· Abstract classes are faster than interfaces.

2 comments:

Eugene said...

thank you, this is first answer I've found which explained difference from relationship point of view (CAN-DO and IS-A). nice one.

Anonymous said...

Hi bro, i have never seen such an elobrated explination or clear cut view with realtime senario... sending you a BIG thanks to you... i will be in touch with you :)

keep post the real stuff..

cheers