• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between interface and abstracts  RSS feed

 
ekte spiriopoulos
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • 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
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 217
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The behaviour of interfaces changed in Java8; it may change again in Java9 or Java10.
 
Rajdeep Biswas
Ranch Hand
Posts: 231
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • 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: 56541
172
  • Mark post as helpful
  • send pies
  • 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.
 
JohnG Jackson
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 56541
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!