Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between Abstract Class and Interface, Which scenarios we are using both?

 
Koti Samadhi
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Difference between Abstract Class and Interface, Which scenarios we are using both?

Any one can , Please reply with examples.

Thanks
Koti
 
Anu satya
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract class can also have non-abstract methods in it. but, interfaces can't have non-abstract methods.

Abstract class can have constructors where as interfaces can't have constructors

Abstract class can be extended to only one sub class....(Single inheritance) where as you can implement multiple interfaces

bot abstract classes and interfaces can't be instantiated.

Regards,
Anu





 
Vijitha Kumara
Bartender
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Koti Samadhi wrote:Difference between Abstract Class and Interface, Which scenarios we are using both?

Hi Koti,
Both expose the concept "Abstraction". Abstract classes are useful when you need to provide a common set of method implementation(s) for all the subclasses to share. Most modern frameworks build around the concept of interfaces.
 
Santhosh Reddy
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Purpose of both abstract class and interface is almost the same, except you can use abstract class when some of the implementation is fixed(few methods are implemented) and rest of the implemtation is left to the user.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Santhosh Reddy wrote: . . . and rest of the implemtation is left to the user.
Probably better to say "and the rest of the implementation will be different in the subclasses."
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi this is simple....
In Interface only abstract and public access specifiers are allowed...
In abstract classes all normal access specifiers are allowed..
abstract classes should not be instantiated..
Use abstract keyword for abstract classes..
Use interface keyword for interfaces..

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s.palanivel rajan wrote:hi this is simple....
abstract classes should not be instantiated..
[/color]


Do you mean that if you want you can but you should not ?

s.palanivel rajan wrote:


cool yaar. from where you have picked up these habits ?
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why ??dont you saw the left side of message window
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid pretty coloured text; many people find it difficult to read. I have changed the previous text to black.
 
s.palanivel rajan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok cambell.. no problem i understand...
but if it is the case then why these colours are given...
 
Rakib Jahin
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice replies.it was helpful for me
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. A few colours might be helpful, but . . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic