• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface & Abstract class

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to know significance of interface over abstract class.
for example in the following two classes,

public class A implements intfac1,intfac2{
}

public class B extends ABSTR{
}

where intfac1 and intfac2 are two interfaces and ABSTR is an abstract class. ABSTR has all the methods declared which intfac1 and intfac2 both have. Now I would like to know what is the difference in both cases, which one is prefered and why?? As abstract class can be used instead of interface having all the methods of interfaces required, then why interface is prefered. Yeah, inheritence is one way to answer this question, but in my above example, I am using all the methods defined in intfac1 and intfac2 interface in abstract class ABSTR as well.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well in a class tree you might not have the choice to put everything into your abstract class...example:

-Object
--Not Living
---Vehicle
----Nonwater Vehicle
-----Ford
------Ford Float

So a Ford Float inherits everything from: Ford, Nonwater Vehicle, Vehicle, Not Living, and Object. But since a Ford Float should have the power to float in water, we create an interface that gives that Ford car that ability:

public class FordFloat extends Ford implements VehicleFloat{}

I hope that clears you a little bit! Cheers.
 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats all right, But my question is - Does implement interface or extend class make any difference in my case(if an abstract class has all those methods of interface in it). I mean to say that if abstract class is having all those methods of its counterpart interface(having same methods of abstract class), then which one should be used in that specific condition(interface or abstract, and why?)
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it all depends on what the relationships are.
implementing an Interface means "behaves like", extending abstract class means "is a concrete example of this abstract concept".
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it all depends on what the relationships are.
implementing an Interface means "behaves like", extending abstract class means "is a concrete example of this abstract concept".
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Manish, this article provides explanation of when to use abstract and interface classes.
 
Manish Vyas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thax a lot to all of you for providing useful information
 
Clowns were never meant to be THAT big! We must destroy it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic