• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract & interace  RSS feed

 
Reshma Reddy
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Please anybody tell me, exactly when we use abstract and interfaces in our application and what is the the main reasons to use these things.

Thanks,
vardhan.
 
Dinesh Arora
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Java guys syas that they got rid of multiple inheritance and so you can extend one and only one class in Java. However they knew the importance of multiple inheritance, hence they introduced "Interfaces".

Going by the thumb rule -
Use interface when you want inheritance from multiple source.
ex - Interface A -- widthA()
Interface B -- LengthB()

class testImplement Box implements A,B{
}


Remember Interfaces are nothing more than,"pure" , 100% abstract classes. Unlike interfces you can have non abstarct methods in a abstarct class, but you cannot have a non- abstract method in an interface.

Also defining an inetrface also gives you the flexibility to add a new property in the implementing class.

For ex -
Let's say you have an interface A and abstract class B.

class TestAbstract extends B{}
class TestInterface implements A{}

when we use TestInterface class, it gives us freedom to implement any number of interface in the future, let's say we want to implement a new interface C in future, we simply need to write
- class TestInterface implements A,c{}

while you do not have this freedom while extending abstarct class... since you can only extend one and only one class.

Hope, this resolves your query.

Thanks,
Dinesh
[ April 29, 2007: Message edited by: Dinesh Arora ]
 
Joe Wolfe
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it was decribed to me interfaces are a set of rules you have to follow when you write a class. They are a list of methods you must write to use the interface. (I hope there is more to it than that.) You can implement multiple interfaces in a single class so it is sort of like multiple inheritance.

Only the methods in the interface are dummy ones and contain no code and the data in the interface are only constants. There does not seem to be much in them besides dummy methods and constants.

In the implementing class you provide code for all those methods required by the interface. I think they should call it overriding the interface methods.

There is some clever coding that makes them useful. I'll let someone who know it better explain tat.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!