This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes why an abstract class is faster than an interface ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "why an abstract class is faster than an interface ?" Watch "why an abstract class is faster than an interface ?" New topic
Author

why an abstract class is faster than an interface ?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Friends,
I come across *abstract classes may be a tiny bit faster (or they may not.) * in this page [JavaRanch.]
http://faq.javaranch.com/java/InterfaceVsAbstractClass

I want to know the reason for why abstract classes may be a tiny bit faster ?

Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

In Java's early days, there was a technical reason for the difference. Do you know what a "virtual table" or "vtbl" is? Basically, a simple vtbl could be used for directly inherited methods, and so the JVM just had to look at two pointers to find the code for a method inherited from an abstract class -- that's relatively fast. But since any class can implement any number of interfaces, finding the code that implements an interface method would involve following a pointer to a table of interfaces implemented by a class, then searching through the table to find a vtbl for the implementation of that interface, and then finding the method pointer in the table. That's obviously a lot more work, so calling an interface method used to be measurably slower.

These days, JVMs are a lot smarter, and most of this sort of lookup is done during dynamic compilation, so the runtime differences are small or nonexistent.


[Jess in Action][AskingGoodQuestions]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thank you very much Ernest for your valuable points!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: why an abstract class is faster than an interface ?
 
Similar Threads
Few queries on Design Principles
interfaces
Difference between interfaces and abstract classes
Difference between inteface & Abstract class
Abstract Class..???