• 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

abstract class and interface speed comparison

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which is faster abstract class or interface?

I can think of it as:
Class loader has to load type information for class as well as interface so they score equally on this point

Class loader in addition will load type information for methods and assuming that both have same methods with abstract class having all abstract methods and interface having same number of methods, they also score same on loading type information for methods

When a method is called on the sub class object in both cases then the same method will be overridden and hence will be decided at run time and hence seems to be equal for both.

But the difference may arise if the method being called is defined in the super class (abstract in this case) and not overridden in sub class. Thus there is no need for run time polymorphism and hence reduces time but in case of interface, there is always going to be sub class version of the method and I see some kind of run time computation which makes abstract classes more faster in this particular case when implementation has been provided for the method.

Please do give some thought and more inputs......
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, this question does not belong in the SCJP forum. You will not get questions like this one on your SCJP exam. I will move the question to a more appropriate forum.

Furthermore, in my opinion, a question like this is not very useful at all. Whether one or the other is faster is not normally a reason why you would choose between an interface and an abstract class.

If you really want to know if there is any speed difference (whatever you mean exactly by that, with regard to interfaces and abstract classes), you should write a test program and measure the execution time.

Note that the JVM contains a very sophisticated JIT (just-in-time) compiler which does a lot of complicated optimizations, which makes it almost impossible to predict which will be more efficient and run faster.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though not related to performance, from design point its better to code using interfaces because of lower coupling, See the fllowing lines from http://www.lalitbhatt.com/tiki-index.php?page=Spring+Introduction

It’s best to program to interface, rather than classes. Spring reduces the complexity cost of using interfaces to zero. - This is a basic programming principal and is actually at a higher level than Spring. It's about loose coupling and building services which are bound with contract and not glued tightly with implementations.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic