Forums Register Login

difference between interface and abstracts

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Based on your current understanding, what do you think the difference is? That'll give us a good starting point for the discussion.
+Pie Number of slices to send: Send
 

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
1
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
The behaviour of interfaces changed in Java8; it may change again in Java9 or Java10.
+Pie Number of slices to send: Send
Yes Campbell, we now have static and default methods in interface now in Java8. Is that violating abstraction principle?
+Pie Number of slices to send: Send
Why should the existence of static and default methods violate abstraction? An abstraction does not mean the same as an abstract class.
+Pie Number of slices to send: Send
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)

1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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!
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 726 times.
Similar Threads
Factory pattern(s)
Exact Use of Abstract Class And Interface
Give the easiest example which differentiate "interface" and "abstract class" in Java.
abstract class vs interface
Usage of abstract class on various design pattern point of view
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:28:54.