• 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

difference between interface and abstracts

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which is the difference between this clans? I want to mention to that I am greatful that you help me with my problems to solve it
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Based on your current understanding, what do you think the difference is? That'll give us a good starting point for the discussion.
 
ekte spiriopoulos
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Cooke wrote:Based on your current understanding, what do you think the difference is? That'll give us a good starting point for the discussion.

the interface doesn't accept for example int a; .I think it extend only ONE class .It hasn't methods .
Mm abstract it has methods .It has many methods that extend the abstract class.That I have understood maybe iam wrong
 
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may have explained what you thought badly, but I am afraid it doesn't look correct to me. We have an FAQ which will probably help you
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The behaviour of interfaces changed in Java8; it may change again in Java9 or Java10.
 
Ranch Hand
Posts: 233
1
Eclipse IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Campbell, we now have static and default methods in interface now in Java8. Is that violating abstraction principle?
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why should the existence of static and default methods violate abstraction? An abstraction does not mean the same as an abstract class.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I'm new here and to Java so please correct me if I'm wrong but here is the major differences to me;
1. Interfaces are not objects, abstract classes are.
2. Interfaces don't have constructors where sub-classes can inherent field members
3. It used to be that interfaces could have no methods with implementation, rather only abstract methods but with Java 8 interfaces can have default and static methods
4. And I almost forgot; you can implement multiple interfaces but because Java has single inheritance, you can only extend one abstract class at a time.
5. Oh and also, because interfaces are not objects, they are much faster
(But I never proved it myself, conceptually Java handles interface abstract methods as basic names, I'm not quite sure the overhead java has when handling these as objects, it's just something I'm told)

 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

JohnG Jackson wrote:Hello, I'm new here . . .

Welcome to the Ranch


1. Interfaces are not objects, abstract classes are.

That I am afraid doesn't make sense. Both interfaces and abstract classes can be used to make objects, but a class/interface isn't an object as such.
The techniques for creating instances of abstract classes and interfaces differ slightly.

2. Interfaces don't have constructors where sub-classes can inherent field members

I think that is correct but it isn't clear. Abstract classes can have instance variables, constructors, etc. Interface have neither; any fields in interfaces are implicitly public static final, therefore intended for use as constants. The use of interface fields is controversial.


3. It used to be that interfaces could have no methods with implementation, rather only abstract methods but with Java 8 interfaces can have default and static methods
4. And I almost forgot; you can implement multiple interfaces but because Java has single inheritance, you can only extend one abstract class at a time.

Both correct


5. Oh and also, because interfaces are not objects, they are much faster . . . it's just something I'm told

That last bit looks as if somebody had told you something misleading. Neither classes nor interfaces have a speed as such. The only things that have speed are bits of code being executed, so I cannot see there is a difference between methods inherited from a superclass and those implemented from an interface for speed.
 
JohnG Jackson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
That I am afraid doesn't make sense. Both interfaces and abstract classes can be used to make objects, but a class/interface isn't an object as such.



Right.. I should know this. Objects are created at runtime. I was told this on an online video and I may have misrepresented it. I guess you can't instantiate either anyways so the comparison sucks.

All valid points!
 
I am a man of mystery. Mostly because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic