• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Interface

 
jesintha jegan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi this is jesintha.Im a new member.Im preparing for SCJP 1.5 .

Can anyone tell me why we need interfaces apart from abstract classes?Becaz i read that interfaces are 100 percent abstract class.what is the difference between abstract class and interface?



Thanks in advance.
 
Vijitha Kumara
Bartender
Posts: 3914
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jesintha jegan wrote:Hi this is jesintha.Im a new member.Im preparing for SCJP 1.5 .

what is the difference between abstract class and interface?

Welcome to JavaRanch

Mainly interfaces can have only method definitions (as far as the methods are concerned). Abstract classes can have method implementations (which are non-abstract) and abstract methods (with no body, only method definitions). The rule is if you have at least one abstract method defined, the class should declared to be abstract.
You will find Sun tutorials useful to get started.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interface are built to define contracts only. It will define what to do only, not how to do. And abstract classes can also define what to do, but it can also define how to do. Both promotes loose coupling. Make client codes independent to implementation. Client can only see interface's API, client does not know how that API is defined by the interface implementor, so in future you have flexibility to change interface's API definition, if any new and enhanced implementation you got. In programming world, change is constant, so interface make client's code independent of these changes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic