File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what's difference between an interface and abstract class

 
vasanthi
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
I am preparing for the scjp exam,can anybody tell me the difference between an interface and an abstract class.i am just getting confused,please explain me in detail.
regards
vasanthi
 
bill bozeman
Ranch Hand
Posts: 1070
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vasanthi,
We have a naming policy here at the Ranch, and you name doesn't follow the rules. Could you please re-register with a more apprpriate name. Check out this link for more details: www.javaranch.com/name.jsp
Thanks,
Bill
Also, I am going to move this to the Certification Forum since it doesn't relate directly to Mock Exam errors.
Bill
 
Velmurugan Periasamy
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this link
http://java.sun.com/docs/books/tutorial/java/interpack/interfaceDef.html
HTH
------------------
Velmurugan Periasamy http://www.geocities.com/velmurugan_p/
 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's an easy one!
An abstract has a mixture of complete methods, and declarative methods. The methods that do not have anycode are declared abstract indicating the developer(s) that the implementation of the class is not complete.
One has to extend an abstract class and provide the implementation of the abstract methods.

In an interface, all the methods are just declarative. ie., they are just declared with no body. So, theoretically an interface is a "pure abstract class".

One has to impelement an interface and write code for the methods declared therein.
If you browse the Java API documentation, you will find a lot of excellent examples for interfaces and abstract classes.
Hope that helps!
Ajith
 
chak ms
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces and abstract classes are related in the means of implementing methods own by interface or abstract class.
U can't instantiate either Interface or abstract class. Interface is just collection of method names without body. It means, if a class implements any interface, it has to implement all the methods in that interface. It means, u have to define those methods which are declard in the interface of the class. No method has definition in the interfaces. U have to provide ur own definition whenever u want.
Where as in abstract classes, these may contain data members and collection of methods too. This may contain non abstract methods too. Non abstract methods means the methods which have definition and abstract means, which don't have method definition in the class definition itself. Whenever u extend a class with an abstract class, u have to provide the definitions to all the nonabstract methods of the abstract class.
To make it more clear for u, If an abstract class contains only abstract methods and no non abstract methods and no data members becomes an interface.
I hope this could help u to understand about interfaces and abstract classes. If u have still more doubts, u can post a reply to this.
with regards
chak
Originally posted by vasanthi:
hi all,
I am preparing for the scjp exam,can anybody tell me the difference between an interface and an abstract class.i am just getting confused,please explain me in detail.
regards
vasanthi

 
Ajith Kallambella
Sheriff
Posts: 5782
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chak,
One small correction to what you said -
Interfaces can contain data members. They are implicitly public and final, cannot be declared private and they have to be initialized.
Ajith
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic