Thursday, May 5, 2011

Abstract Class Vs Interface


1) Execution Speed:
         Abstract class are Very Speed
         Interface are Slow

2) Methods:
        Abstract class may contain complete or incomplete methods.
              (We can implement methods in side abstract class)
        Interface can only have Signature of the method.
              (We can not implement methods in Interface)

3) Constructor & Destructor:
          Abstract class can contain Constructor & Destructor .
          Interface cannot contain Constructor & Destructor.

4) Multiple Inheritance:
         Abstract class cannot support Multiple inheritance
         Interface can support Multiple inheritance.

5) Access Modifiers:
        Abstract class can contain access modifiers.
        Interface cannot contain modifires.

6) Implementation:
         In abtract class ,if we create any abstract function,
        We must implement in normal subclass
        
        In interface we need not implement all the methods.
        We can implement require methods in sub classes.

7) Scope:  
         Abstract class scope upto derived class
         Interface Scope upto any level of inheritance.

8) We cannot create object for both abstract & interface.

9)  To acquire the abstract class properties , we must use "extends".
      But in Interface, we need to use "implements"


Examples:
           1)Abstract Class:

 abstract class A
{
    abstract void callme();
    void callmetoo()
                {
                     System.out.println("This is concreate method")
                 }
}       


class B extends A
{
 Void callme()
       {
                System.out.println("This is abstract method );
        }
}


class AbstractDemo
{
public static void main(String args[])
    {
        B b=new B();

         b.callme();
         b.callmetoo();
     }
}




2) Interface:


interface I1  
{
  void f();
}
interface I2
{
  int f(int i);
}
 
interface I3  
{
  int f();
}
class  
{
  public int f()  
{
    return 1;
  }
}
class C2 implements I1, I2 
 {
  public void f()
   { }
  public int f(int i)  
{
    return 1;
  // overloaded
}
class C3 extends implements I2 
{
  public int f(int i
{
    return 1;
  // overloaded
}
class C4 extends implements I3 
 {
  // Identical, no problem:
  public int f() 
{
    return 1;
  }
}

0 comments:

Post a Comment

RASIGATECH. Powered by Blogger.

Popular Posts

Followers