GeekInterview.com
Series: Subject: Topic:
Question: 26 of 78

What is difference between abstract class and interface

Asked by: apt_dpl | Member Since Feb-2006 | Asked on: Feb 11th, 2006

View all questions by apt_dpl

Showing Answers 1 - 39 of 39 Answers

There is no difference in the functionality of these two.The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.Also user defined exceptions can be defined within an interface itself, which is not the case with an abstract class.

Yes  1 User has rated as useful.
  
Login to rate this answer.
Praveen Kumar V

Answered On : Mar 3rd, 2006

An Abstract Class can contain default Implementation, where as an Interface should not contain any implementation at all. An Interface should contain only definitions but no implementation. where as an abstract class can contain abstract and non-abstract methods. When a class inherits from an abstract, the derived class must implement all the abstract methods declared in the base class. an abstract class can inherit from another non-abstract class.

Yes  1 User has rated as useful.
  
Login to rate this answer.
kotesh

Answered On : Mar 5th, 2006

View all answers by kotesh

1. Abstract class is a class which contain one or more abstract methods, which has to be implemented by sub classes. Inteface is a Java Object containing method declaration and doesn't contain implementation. The classes which have implementing the Interfaces must provide the method defination for all the methods.2. Abstract class is a Class prefix wtih a abstract keyword followed by Class definaton. Interacace is a Interface which starts with interface keyword.3. Abstract class contatins one or more abstract methods. where as Interface contains all abstract methods and final declarations.4. Abstract class contains the method defination of the some methods. but Interface contains only method declaration, no defination provided.5. Abstract classes are useful in a situation that Some general methods should be implemented and specialization behaviour should be implemented by child classes. Interafaces are useful in a situation that all properties should be implemented we can use this scenario

Yes  1 User has rated as useful.
  
Login to rate this answer.
n.shah

Answered On : Mar 5th, 2006

View all answers by n.shah

diff 1 )  interface must always have by default public and abstract methods

while abstract class can have non abstract methods

diff 2 ) all the variables in interface are by default public static final while ion abstract class you may have instant variables

Yes  1 User has rated as useful.
  
Login to rate this answer.
achantakrishna

Answered On : Mar 27th, 2006

View all answers by achantakrishna

the main differences between an interface class and abstract class are

1.abstract classes must be given by keyword abstract,where as in interface class with key word interface

2.every method in interface class should be abstract,but this is not necessary in abstract class

3.by default what ever variables we decalre in interface are public static final,where as in abstract class they can be default and instance variables also

  
Login to rate this answer.
shankar

Answered On : Apr 4th, 2006

Abstract Class is a class which must have at least one abstract method.Interface is a class which is having all abstract methods. By default interface methods are public abstract.

  
Login to rate this answer.
vara Rama Krishna Chadaram

Answered On : Apr 24th, 2006

      Interface                             Abstract Class

---------------------------------------------------------------------------

(1)    Decelear with keyword                  (1)  Decelear with keyword  

         interface                                                Abstract.

(2)    It will not                               (2) Itwill may or may            contain      Constructor.                                     Constructor.

(3)  By default all methods                   (3) It may contact Abstract

   are Public AbstractMethods.                  methods and non

                                                          Abstract methods.

(4) it will contain only                          (4) It will final variables and

     final variables,                                    Instances Variables.

  
Login to rate this answer.
chandrahasa reddy

Answered On : Oct 22nd, 2006

Dear Greekintreview.com,

 everyday i searched this website regarding java information.i got sufficient information diff between interfaces and classes given answer for Mr Koteswara Reddy.

thanks for koteswara reddy

  
Login to rate this answer.
Dharmendra

Answered On : Oct 27th, 2006

Abstract Class means Blueprint of the model.

Interface means functionality of the model(Blueprint) if the class implements one(an interface).

Using Abstract class we can take n number of instances. Like using the Blueprint of a Car
we can take n number of similar cars. But we cannot use the Bluprint or model to drive the
car.That is the meaning of abstract. So, Car is the Blueprint and Ford Car is the derived class from car which uses all the
properties of class and some more properties which is specific to Ford.

Frog is a derived class from Mammal.

When it lives on land it implements the function of hopping.

when it lives on water it implements the function of swimming.

So we find that frog uses both methods hopping and swimming. So it is better to declare
in some interface so that any frog which implements the interface will define its own
way of transportation.

So let me create an Interface called Amphibian and give two method Swimming() and hopping().
It looks like

public interface Amphibian{

public void swim();
public void hop();
}

Let we define the Frog class as

public class Frog implements Amphibian{

public void swim(){

//Define your words
}

public void hop(){

//Define your words
}

}

The above class Frog when implements the interface Amphibian it has to define the methods
declared in the interface Amphibian. It can be an empty method. But it should be defined.

The use of an interface is any class which wants to use some remote functionalities can
implement that particular interface and can perform the task like man using Parachute to
fly in air.

- Compiled by Dharmendra

Yes  3 Users have rated as useful.
  
Login to rate this answer.
perireddy

Answered On : Oct 30th, 2006

abstract class contains both final and instance variablea .afinalvariable is also ainstance variable then what is the difference mr.koteswara reddy

  
Login to rate this answer.
SrideviMohan

Answered On : Nov 1st, 2006

   If the functionalites(declaration and definition) is same for all classes in the project and that is mandatory that time we use Abstract class.

If the declaration is same but the implementation logic is going  differ in class to class that time we use Interface

  
Login to rate this answer.
atul931

Answered On : Feb 28th, 2007

The difference between Interface and Abstract classis,

1:In abstarct class you can use Final and Instance Variable.
2:In abstarct class you can use Abstract method or non-abstract method, But in Interface, only Abstarct method can be used.

  
Login to rate this answer.
S.JEELAN BASHA

Answered On : Mar 6th, 2007

It's very nice to see such a difficult topic in a simple and effective manner.
Thank Q very much..

  
Login to rate this answer.
sourav

Answered On : May 9th, 2007

Abstract class can have constructor but interface can not have constructor.


Abstruct class constructor run when one of its subclass constructor execute.

  
Login to rate this answer.
Binesh Speridian

Answered On : May 25th, 2007

There is no need to declare abstract method in Abstract classes

  
Login to rate this answer.

1. Abstract class contain both abstract andnonabstract methods.
Butinterface can only contain abstarct methods.
2. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change. But, If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method.
3. Aclasscan implement more than one interfacebutcanextendonly one abstarct class.

  
Login to rate this answer.
gyoganandam

Answered On : Mar 25th, 2008

View all answers by gyoganandam

1. Abstract  class cannot instantiate must be inherited
2. abstract class have one abstract method.
3. interface have all abstract methods.

  
Login to rate this answer.
dgopisinght

Answered On : Jul 4th, 2008

View all answers by dgopisinght

Abstract class may contain non adbstract methods. But by default all the methods under Interface are abstract.

  
Login to rate this answer.
sankara123

Answered On : Jul 22nd, 2008

View all answers by sankara123

) Abstract class defines few or none of the methods, but 
interface defines all the methods..
2) Abstract classes should have subclasses else that will
be useless..
Interfaces must have implementations by other classes else
that will be useless
3)only an interface can extend another interface, but any
class can extend an abstract class..
4)All variable in interfaces are final by default

  
Login to rate this answer.
nishu7nov83

Answered On : Jan 1st, 2009

View all answers by nishu7nov83

All the answers are fine.But there is a very important difference. In the special case, if you have a abstract class, and full hierarchy of subclasses under that, suppose there is class A which is abstract. And Class B, Class C, Class D are extending that. so if you make any change in abstract class, you have to recomplile all the subclasses while in the case of interface, you have to simply compile just interface and you are done.

  
Login to rate this answer.
mveeraselvam

Answered On : Mar 6th, 2009

View all answers by mveeraselvam

Abstract :
1) There won't be any code for methods.
2) We got to give our own code.
3) Abstract class was uncompleted.

Interface:
1)
Compulsory write the code in all methods.
2) More "Parent" classes implements in one Child class.
3) You got to implement the Parent class to child class and can overwrite the methods in the Parent class.

  
Login to rate this answer.

Abstract class is not fully implemented class. The main purpose of abstract class is providing abstraction.
1)It may be or may not be contains abstract methods.it may has defined methods also.
2) It contains any kind of fields.

3) Subclass extends only one abstract class (Bcoz Java doesn't support multiple inheritance).
Interface is included in Java to support multiple inheritance.
1) It should contain abstract methods only.
2) It contains only final fields.
3) Subclass may implements any no of interfaces.

Correct me if I am wrong

  
Login to rate this answer.
ariramk

Answered On : Jan 25th, 2010

View all answers by ariramk

Interface - loosely coupled

  • All the Methods must be public.
  • By Default. variable/constants are static final.
Abstract - tightly coupled
  • Can have private/public methods

  
Login to rate this answer.
Chinna1983

Answered On : Sep 25th, 2010

View all answers by Chinna1983

Abstraction:
1) All the fields are abstract fields.  If any one of the method is abstract methods that class is going to AbstractClass.
2) Unable to create the object for Abstract class.
3) Abstract class having concrete methods (non abstract methods) and abstract methods.
4) In the future you want to add the some
feature (implementation code) to all derived classes for the particular AbstractClass Just place that feature (implemented code (non abstract method)) into the abstract class.
You cannot do this in interface.

Interface:
1) Only given
structure. All the methods is non implemented methods.
2) Unable to create the object.

  
Login to rate this answer.
Andy Nguyen

Answered On : Nov 2nd, 2010

View all answers by Andy Nguyen

"Abstract Class is a class which must have at least one abstract method.": NOT TRUE!!

Try it:
public abstract class b
{
    public void methodOne()
    {
    }
}
compiles just fine.

  
Login to rate this answer.

1. Interface methods implementation should need to be given in the sub class but not in the case of abstract class.
2. Abstract class have the common implementation methods in it, which can be used again in the sub class. Whereas in interface implementation for the methods should need to given in the sub class(Mandatory).

  
Login to rate this answer.
khaleek_ahmad

Answered On : Dec 23rd, 2010

View all answers by khaleek_ahmad

Abstract class is a special kind of class which can not be instantiated while Interface is an Entity. You can define some methods in Abstract class but can not in interface.

  
Login to rate this answer.
mlazur

Answered On : Mar 24th, 2011

View all answers by mlazur

There is a difference in functionality.  While subclasses can only use one inherited class, they can use multiple interfaces.

  
Login to rate this answer.
Mujeeb Ahmed

Answered On : Jun 14th, 2011

View all answers by Mujeeb Ahmed

There are quite a few known differences:

Extensibility:

Interface: If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

Abstract class: If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.

Attributes:

Interface: No fields can be defined in interfaces

Abstract class: can have fields and constants defined

Access modifiers

An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public

Abstract class can contain access modifiers for the subs, functions, properties

Implementation

An interface cannot provide any code, just the signature.

Abstract class can provide complete, default code and/or just the details that have to be overridden.

  
Login to rate this answer.
IKRAM RANA

Answered On : Jul 30th, 2011

abstract class are the partially implementation of abstraction.interfaces are the fully implementation of abstraction.abstract class can represent both behavior and properties of an object.interfaces can represents only the behavior of an object.In abstract class we can have normal function but in interface we can not have normal function.

  
Login to rate this answer.
Srinivasulu.D

Answered On : Aug 8th, 2011

The differences between abstract class an interface as fallows:

1. Abstract class has the constructor, but interface doesnt.

2. Abstract classes can have implementations for some of its members (Methods), but the interface cant have implementation for any of its members.

3. Abstract classes should have subclasses else that will be useless..

4. Interfaces must have implementations by other classes else that will be useless

5. Only an interface can extend another interface, but any class can extend an abstract class..

6. All variable in interfaces are final by default

7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.

8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

9. A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

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

11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesnt allow accessibility modifier

12. 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.

13. 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 propertys signature but no implementation.

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

15. Abstract scope is upto derived class.

16. Interface scope is upto any level of its inheritance chain.

  
Login to rate this answer.
maddy

Answered On : Aug 20th, 2011

Code
  1.     protected void btnsubmit_Click(object sender, EventArgs e)
  2.     {
  3.         #region try block
  4.         try
  5.         {
  6.             if (Page.IsValid)
  7.             {
  8.                 string sourcePath = txtsoure.Text;
  9.                 string targetPath = txtdestination.Text;
  10.                 string[] files = Directory.GetFiles(@sourcePath, "*.*", SearchOption.AllDirectories);
  11.  
  12.                 #region loop thru for all diretories&subdiretories
  13.                 foreach (string p in files)
  14.                 {
  15.  
  16.                     string ext = Path.GetExtension(p);
  17.                     string filename = Path.GetFileName(p);
  18.  
  19.                     #region if file type=doc
  20.                     if (ext == ".doc")
  21.                     {
  22.                         string destinationpath = targetPath + "DOC";
  23.                         string sourcefile = Path.Combine(sourcePath, filename);
  24.                         if (Directory.Exists(destinationpath))
  25.                         {
  26.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  27.                             File.Move(p, @destFile);
  28.                             //System.IO.File.Copy(sourcePath, @destFile , true);
  29.                         }
  30.                         else
  31.                         {
  32.                             Directory.CreateDirectory(targetPath + "DOC");
  33.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  34.                             File.Move(p, @destFile);
  35.                             //System.IO.File.Copy(sourcePath, @destFile, true);
  36.                         }
  37.  
  38.                     }
  39.                     #endregion
  40.  
  41.                     #region if file type=rtf
  42.                     else if (ext == ".rtf")
  43.                     {
  44.                         string destinationpath = targetPath + "RTF";
  45.                         string sourcefile = Path.Combine(sourcePath, filename);
  46.                         if (Directory.Exists(destinationpath))
  47.                         {
  48.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  49.                             File.Move(p, @destFile);
  50.                         }
  51.                         else
  52.                         {
  53.                             Directory.CreateDirectory(targetPath + "RTF");
  54.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  55.                             File.Move(p, @destFile);
  56.                         }
  57.  
  58.                     }
  59.                     #endregion
  60.                     #region if file type=pdf
  61.                     else if (ext == ".pdf")
  62.                     {
  63.                         string destinationpath = targetPath + "PDF";
  64.                         string sourcefile = Path.Combine(sourcePath, filename);
  65.                         if (Directory.Exists(destinationpath))
  66.                         {
  67.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  68.                             File.Move(p, @destFile);
  69.                         }
  70.                         else
  71.                         {
  72.                             Directory.CreateDirectory(targetPath + "PDF");
  73.                             string destFile = System.IO.Path.Combine(destinationpath, filename);
  74.                             File.Move(p, @destFile);
  75.                         }
  76.  
  77.                     }
  78.                     #endregion
  79.  
  80.                 }
  81.                 #endregion
  82.  
  83.                 #region code to write data on xml file
  84.                 try
  85.                 {
  86.                     //code to write data on xml file
  87.                     string[] docs = Directory.GetFiles(@targetPath, "*.doc", SearchOption.AllDirectories);
  88.                     int documents = docs.Length;
  89.                     string[] rtfs = Directory.GetFiles(@targetPath, "*.rtf", SearchOption.AllDirectories);
  90.                     int wordpads = rtfs.Length;
  91.                     string[] pdfs = Directory.GetFiles(@targetPath, "*.pdf*", SearchOption.AllDirectories);
  92.                     int pdf = pdfs.Length;
  93.                     int total = documents + wordpads + pdf;
  94.                     DataSet ds = new DataSet();
  95.                     ds.ReadXml(Server.MapPath("XMLFile.xml"));
  96.                     DataRow row;
  97.                     row = ds.Tables[0].NewRow();
  98.                     row[0] = documents;
  99.                     row[1] = wordpads;
  100.                     row[2] = pdf;
  101.                     row[3] = total;
  102.                     ds.Tables[0].Rows.Add(row);
  103.                     row = ds.Tables[0].Rows[0];
  104.                     //row.BeginEdit();
  105.                     //row[0] += "'" + documents ;
  106.                     //row.EndEdit();
  107.                     ds.WriteXml(Server.MapPath("xmlfile.xml"));
  108.                     //lblmsg.Text = "record inserted";
  109.  
  110.                 }
  111.                 catch (Exception ex)
  112.                 {
  113.                     lblmsg.Text = "Error: " + ex.Message.ToString();
  114.  
  115.                 }
  116.                 //Server.Transfer("XMLFile.xml");
  117.                 Response.Redirect("XMLFile.xml");
  118.                 #endregion
  119.             }
  120.         }
  121. #endregion
  122.         #region Catch block
  123.         catch (Exception ex)
  124.         {
  125.             lblmsg.Text = "Error:" + ex.Message.ToString();
  126.  
  127.         }
  128.         #endregion
  129.  
  130.     }
  131.  
  132. -------------------------------------------------------------------------------------------------
  133. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MoveFiles.aspx.cs" Inherits="MoveFiles" %>
  134.  
  135. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  136.  
  137. <html xmlns="http://www.w3.org/1999/xhtml">
  138. <head runat="server">
  139.     <title>Copy Files</title>
  140.     <style type="text/css">
  141.         .style1
  142.         {
  143.             width: 32%;
  144.         }
  145.         .style2
  146.         {
  147.             width: 155px;
  148.         }
  149.     </style>
  150.     <script type ="text/javascript" src ="valid.js" >
  151.       </script>
  152. </head>
  153. <body>
  154.     <form id="frmsample" runat="server">
  155.     <div align="center">
  156.    
  157.         <br />
  158.         <br />
  159.         <br />
  160.         <br />
  161.         <br />
  162.         <br />
  163.    
  164.         <table class="style1" style="border: thin double #0000CC">
  165.             <tr style="background-color: #00CCFF">
  166.                 <td colspan="2"
  167.                     style="font-family: Arial; font-size: large; font-style: normal; color: #0000CC; text-decoration: underline">
  168.                     Copy Files</td>
  169.             </tr>
  170.             <tr>
  171.                 <td align="right" style="background-color: #00FFFF">
  172.                     <asp:Label ID="Label1" runat="server" Font-Bold="True" Text="Source"></asp:Label>
  173.                 </td>
  174.                 <td class="style2">
  175.                     <asp:TextBox ID="txtsoure" runat="server" Width="192px"></asp:TextBox>
  176.                 </td>
  177.             </tr>
  178.             <tr>
  179.                 <td align="right" style="background-color: #66FFFF">
  180.                     <asp:Label ID="Label2" runat="server" Font-Bold="True" Text="Destination"></asp:Label>
  181.                 </td>
  182.                 <td class="style2">
  183.                     <asp:TextBox ID="txtdestination" runat="server" Width="193px"></asp:TextBox>
  184.                 </td>
  185.             </tr>
  186.             <tr>
  187.                 <td style="background-color: #66FFFF">
  188.                      </td>
  189.                 <td align="left" class="style2" style="background-color: #66FFFF">
  190.                     <asp:Button ID="btnsubmit" runat="server" Font-Bold="True"
  191.                         onclick="btnsubmit_Click" OnClientClick ="return valids()" Text="Submit"
  192.                          />
  193.                 </td>
  194.             </tr>
  195.             <tr style="background-color: #00CCFF">
  196.                 <td colspan="2">
  197.                     <asp:Label ID="lblmsg" runat="server" Font-Bold="True"></asp:Label>
  198.                 </td>
  199.             </tr>
  200.         </table>
  201.    
  202.     </div>
  203.     </form>
  204. </body>
  205. </html>
  206.  
  207. -------------------------------------------------------------
  208.  
  209.    function valids()
  210.    {
  211.     if(document.getElementById('txtsoure').value=="")
  212.         {
  213.           alert("Please Enter source");
  214.           document.getElementById('txtsoure').focus();
  215.           return false;
  216.         }
  217.         if(document.getElementById('txtdestination').value=="")
  218.         {
  219.           alert("Please Enter destination");
  220.           document.getElementById('txtdestination').focus();
  221.           return false;
  222.         }
  223.         var source=document .frmsample .txtsoure.value;
  224.         var dest=document .frmsample .txtdestination.value;
  225.         if (source ==dest )
  226.         {
  227.         alert ('source&destination must not be same');
  228.         document .getElementById ('txtsoure').style.background='linen';
  229.     document .getElementById ('txtdestination').style.background='linen';
  230.     document.getElementById('txtsoure').focus();
  231.         return false ;
  232.         }
  233.        
  234.         //      script for source path checking
  235.         var path = document.getElementById('txtsoure').value;
  236.         if(navigator.platform == "Windows")
  237.         {
  238.                 if(path.charAt(0) != "/")
  239.                 {
  240.                         alert("Enter Valid source path");
  241.                         return false;
  242.                 }
  243.                 if(path.charAt(0) == "/" && path.charAt(1) == "/")
  244.                 {
  245.                         alert("Enter Valid source path");
  246.                         return false;
  247.                 }
  248.                
  249.         }
  250.         else
  251.         {              
  252.                 if((path.charAt(0) != "" && path.charAt(1) != "") && (path.charAt(0) != "/" && path.charAt(1) != "/"))
  253.                 {
  254.                         if(!path.charAt(0).match(/^[a-zA-z]/))  
  255.                         {
  256.                                 alert("Enter Valid source path");
  257.                                 return false;
  258.                         }
  259.                         if(path.charAt(1) == "" ||!path.charAt(1).match(/^[:]/) || !path.charAt(2).match(/^[/]/))
  260.                         {
  261.                                 alert("Enter Valid source path");
  262.                                 return false;
  263.                         }
  264.                                
  265.                 }
  266.         }
  267.  
  268.        
  269. //      script for destination path checking
  270.         var path = document.getElementById('txtdestination').value;
  271.         if(navigator.platform == "Windows")
  272.         {
  273.                 if(path.charAt(0) != "/")
  274.                 {
  275.                         alert("Enter Valid Destination path");
  276.                         return false;
  277.                 }
  278.                 if(path.charAt(0) == "/" && path.charAt(1) == "/")
  279.                 {
  280.                         alert("Enter Valid Destination path");
  281.                         return false;
  282.                 }
  283.                
  284.         }
  285.         else
  286.         {              
  287.                 if((path.charAt(0) != "" && path.charAt(1) != "") && (path.charAt(0) != "/" && path.charAt(1) != "/"))
  288.                 {
  289.                         if(!path.charAt(0).match(/^[a-zA-z]/))  
  290.                         {
  291.                                 alert("Enter Valid Destination path");
  292.                                 return false;
  293.                         }
  294.                         if(path.charAt(1) == "" ||!path.charAt(1).match(/^[:]/) || !path.charAt(2).match(/^[/]/))
  295.                         {
  296.                                 alert("Enter Valid Destination path");
  297.                                 return false;
  298.                         }
  299.                                
  300.                 }
  301.         }
  302.  
  303. }
  304.  
  305.  

  
Login to rate this answer.
udaiveer singh

Answered On : Aug 23rd, 2011

the main diff between the java interface and abstract class is that java interface have only abstract methods where as java abstract class can have abstract as well as instance methods also.

  
Login to rate this answer.
Brijendra(Xavient)

Answered On : Oct 5th, 2011

1.Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
2.Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
3.Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
4.Java interface should be implemented using keyword implements; A Java abstract class should be extended using keyword extends.
5.An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
6.A Java class can implement multiple interfaces but it can extend only one abstract class. 7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
8.In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

  
Login to rate this answer.
kuldeep khaware

Answered On : Oct 17th, 2011

The only difference is that a class cannot extend an abstract class if it already is extending some other class.An interface on the other hand can be implemented in any situation which makes them very powerful.

Code
  1. public abstract class kuldeep
  2.  
  3.       {
  4.  
  5.         private int php;
  6.  
  7.         private string color;
  8.  
  9.         public abstract string khaware
  10.  
  11.         {  
  12.  
  13.             get;
  14.  
  15.             set;
  16.  
  17.         }
  18.  
  19.         public abstract void java();
  20.  
  21.       }
  22.  
  23.       //Interface
  24.  
  25. public interface kuldeep
  26.  
  27.       {
  28.  
  29.         string khaware
  30.  
  31.         {  
  32.  
  33.             get;
  34.  
  35.             set;
  36.  
  37.         }
  38.  
  39.         void java();
  40.  
  41.       }

  
Login to rate this answer.
sakthi

Answered On : Jan 28th, 2012

Interface: is like a class but it will not contain code implementation in interface method, and also it may be final or static, we can use multiple inheritance here.

Abstract: is like a super class, it may partially implemented function, We can use only single inheritance.

  
Login to rate this answer.
Ambresh Sharma

Answered On : Feb 5th, 2012

1.abstract class contain concrete as well as non-concrete method.But Interface contains concrete method.

2.abstract class method cant be defined as static,private,final,synchronized where as interface method method by default public and abstract.

3.In abstract class you can define static method but In Interface you cant define.

4.you can define constructor of the abstract class but in case of Interface their you cant do that.

  
Login to rate this answer.
sharoon

Answered On : Sep 26th, 2012

In a very simple and high level, Interface is something to tell a guy, Hey X, you should to all these if you need me whereas abstract class tells Hey X, you should do this alone need not to disturb others.

  
Login to rate this answer.
UMA

Answered On : Jan 22nd, 2013

polymorphism are mainly two types
static polymorphism(corresponding method will bind at the
time of compiling)
dynamic polymorphism(corresponding method will bind at the
run time)

Static polymorphism is used the concept of early binding or we can say
compile time binding where as dynamic polymorphism used the concept of
late binding or run time binding.

method overloading would be an example of static polymorphism
whereas overriding would be an example of dynamic polymorphism.

BCoz,in case of overloading,at compile time the compiler knows which method to link to the call .
However,it is determined at runtime for dynamic polymorphism.
9 months ago

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.