interface questin

why some poeple say the many interfaces slow code?

public class my implements one,two, .... ,hunded
{
//code
}

tell me
hey_heyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ravindra76Commented:


Hi,

I think

Getting the Object dynamically  that implements the interface may take time.

Anyhow interfaces to be used eventhough it may slow.

Best of luck

0
heyhey_Commented:
>> why some poeple say the many interfaces slow code?

which people ?
why don't you ask them ? :)
0
mackrubCommented:
I thinks that when you include more interface in your Applet class .Java have to include the code of the libraly that your use in the interface. Then your bytecode will be larger .It can take more time to download from the interface . I thinks that is why it is slower. About take the longer time to download  problem
Regards
MAC
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

shaveriCommented:
Hi,
In my opinion,when you implement any interface you have to either implement all the methods of the interface or you have to provide the null body for the non required methods,so by this   the byte code size increases and in turn may slows down the execution of the process.
0
mbormannCommented:
i think that ,
ravindra76's
plus mackrub's & shaveri's comments combined r the answer
0
mbormannCommented:
also according to me the reason the class which has many interfaces
is slowed down is because of the following reason.

(quoting verbatim from section 3.5.5 of the Java VM Specification)

as you may know ,A runtime constant pool is a per-class or per-interface
runtime representation of the constant_pool table in a class file
so this pool has in addition to many other stuff ,method references that
must be resolved at run time.

The more number of interface types the more number of methods (alongwith
their signatures)it has to dispatch,generally at runtime since
all methods in Java are virtual by default.

But u know as heyhey_ actually meant to say the speed difference is not that much,in most cases it's negligible.this is so since the whole process is
optimized to a very high degree.

see
3.6.3 Dynamic Linking
Each frame (§3.6) contains a reference to the runtime constant pool (§3.5.5) for the type of the current method to support dynamic linking of the method code. The class file code for a method refers to methods to be invoked and variables to be accessed via symbolic references. Dynamic linking translates these symbolic method references into concrete method references, loading classes as necessary to resolve as-yet-undefined symbols, and translates variable accesses into appropriate offsets in storage structures associated with the runtime location of these variables.
This late binding of the methods and variables makes changes in other classes that a method uses less likely to break this code.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heyhey_Commented:
I still don't see why 'using many interfaces slow the code' ... if you use many interfaces, you'll have more code (slower download time, but not slower execution time), but the same is true if you use many classes ... :)

so
1. I don't think that using many interfaces will slow the execution time of your code.
2. the discussion is quite 'unreasonable ' (can't find better word)
0
Ravindra76Commented:

Hi heyhey,

I think multiple interfaces will slow down execution.

I read in a book that multiple inheritance willl slow down the execution .

Because

THere are so many baseclasses and finding the method from which it is getting if not present in that class is burden to exeution.

Why this rule won't apply to multiple interfaces in java ( Evne though interface is not true multiple inheritance, it can be manipulaed in some cases).

0
heyhey_Commented:
in 'multiple inheritance' you may have more than one implementation of some method.
in Java you have only ONE implementation of some method, so you won't lose time to find this implementation.
0
Ravindra76Commented:
Any how late binding at execution time may cause execution delay opposed to static binding at compilation time.
0
heyhey_Commented:
>> late binding at execution

but what's the connection between interfaces and late binding ? :)

generally speaking you'll have slower execution time if you use more code (more classes / longer methods etc.). if your class implements some interface you'll have only several more checks (only when you explicitly cast some object from that class).
0
mbormannCommented:
heyhey,
I agree with all above that I read in some book or the other that multiple interfaces slow down code ,but at that time I was too naive to know what it meant. I dont know if the JLS /JVM spec has changed that much that they dont slow down code.

ravindra76,
>>>>>Any how late binding at execution time may cause execution delay opposed to static binding at compilation time.

heyhey is right ,there is no relation between interfaces and late binding.FYI all binding in Java is 'late' as all methods are 'virtual' by default.

u get 'static' binding only when u have the method(s) marked as 'final'
0
heyhey_Commented:
>>  I read in some book or the other that multiple interfaces slow down code

can you give me URL or osme quotation ? I am not bytecode / JVM expert - I'm just using my common sence (so I may be wrong of course :)
0
mbormannCommented:
heyhey ,
I am a bit snowed under work will definitely post the info but right off the cuff I think I read this in 'Core Java' the original JDK1.0 edition(i am not sure)

Will post the info soemtime next week after hunting around.
Cheers.
0
hey_heyAuthor Commented:
what book was it ,so i too xcan read.
0
Ravindra76Commented:

Hi Guys,

 Still i think delay in binding.

Hi Amit:-

I disagree with
>>

u get 'static' binding only when u have the method(s) marked as 'final'


1.In my view, static binding is

Assume  thwo methods.

public void someMethod( int a){}
public void someMethod (String s) {}

int a = 5;
String s="ravi",

someMethod(a)//Now in compiling ,compiler knows its reference to int variable method

someMethod(s)//Now in compiler knows it is string object method.

It's also called static binding.

2.Assume a class Vechicle

public abstract class Vehicle{
  abstract void move();

}


two deived classes

public class Cycle extends Vehicle {
   public  void move(){}

}

public class Car extends Vehicle {
public  void move(){}

}


And some another class

class x {

 // got Vehicle object ( IT may be //cycle or car and called move method.

//Till dynamic binding it don't know //which move will be called

}






0
mbormannCommented:
Ravi,
from the Java language spec

8.4.8.5 Example: Invocation of Hidden Class Methods
A hidden class (static) method can be invoked by using a reference whose type is the class that actually contains the declaration of the method. In this respect, hiding of static methods is different from overriding of instance methods. The example:

class Super {
      static String greeting() { return "Goodnight"; }
      String name() { return "Richard"; }
}

class Sub extends Super {
      static String greeting() { return "Hello"; }
      String name() { return "Dick"; }
}

class Test {
      public static void main(String[] args) {
            Super s = new Sub();
            System.out.println(s.greeting() + ", " + s.name());
      }
}

produces the output:

Goodnight, Dick

because the invocation of greeting uses the type of s, namely Super, to figure out, at compile time, which class method to invoke, whereas the invocation of name uses the class of s, namely Sub, to figure out, at run time, which instance method to invoke.

Again ALL methods in Java are virtual and all of them follow Late binding by default unless you change them yourself.


As a aside,here's 1 little titbit.

In Java you can't have methods where the base class has non-virtual (final)
methods which the derived class can override.

I mean to say

class Parent
{
      public void virtualOverrideInDerived()
      {
System.out.println("In Parent's virtualOverrideInDerived()");
      }
      public final void nonVirtualNOTAllowedOverrideInDerived()
      {
System.out.println("In Parent's nonVirtualNOTAllowedOverrideInDerived()");
      }
}

public class Child extends Parent
{
      public void virtualOverrideInDerived()
      {
System.out.println("In Child's virtualOverrideInDerived()");
      }
      
      //Error this code wont compile at all.
      /*
      public final void nonVirtualNOTAllowedOverrideInDerived()
      {
System.out.println("In child's nonVirtualNOTAllowedOverrideInDerived()");
      }
      */
      
      public static void main(String a[])
      {
            new Child().virtualOverrideInDerived();
            new Child().nonVirtualNOTAllowedOverrideInDerived();
      }
}
0
mbormannCommented:
heyhey_ ,
(with the trailing '_'     )

Can you please take the trouble to explain if u have time?
:-)

ravi ,have u understood finally?
0
hey_heyAuthor Commented:
well nobody has time to tell the actuial answer i asked for but instead of deleting it i give mborman points here.

to mbormann,if you come across it as heyhey said post it here.

thank you all for telling me!
0
hey_heyAuthor Commented:
well i give it in hope that i get the answe later,when they have time
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.