Thursday, March 17, 2011

Difference between method overloading and overriding of methods in java

What is the difference between method overriding and method overloading in Java?
 

In: Java Programming [Edit categories]
Tata Job OpeningsTimesJobs.com/TATA_Jobs
TATA Hiring 8000+ for March Register.Apply for TATA Now
Ads
[Improve]
Method overriding is when a child class redefines the same method as a parent class, with the same parameters.
For example, the standard Java class java.util.LinkedHashSet extends java.util.HashSet. The method add() is overridden in LinkedHashSet. If you have a variable that is of type HashSet, and you call its add() method, it will call the appropriate implementation of add(), based on whether it is a HashSet or a LinkedHashSet. This is called polymorphism.

Method overloading is defining several methods in the same class, that accept different numbers and types of parameters. In this case, the actual method called is decided at compile-time, based on the number and types of arguments. For instance, the method System.out.println() is overloaded, so that you can pass ints as well as Strings, and it will call a different version of the method.


Overriding is an example of run time polymorphism. The JVM does not know which version of method would be called until the type of reference will be passed to the reference variable. It is also called Dynamic Method Dispatch.

Overloading is an example of compile time polymorphism.

Java

Java
  1. Java is an pure object oriented programming language, it uses the concepts of Classes, Objects, Inheritance, Polymorphism. And the execution of a program is non-linear.
  2. It is so called because you can't write a program with out using classes & objects.
  3. Java's motto (so to speak) is "write once run anywhere".
  4. When you compile a Java program, an intermediate bytecode is generated, which itself is interpreted by the Java Virtual Machine. This way you write a program once, and the virtual machine translates the bytecode into instructions a specific processor can understand. Execution of a Java program is by consequence a bit slow, because the intermediate bytecode has to be interpreted.
  5. Java uses a "Garbage Collector" which manages memory automatically so the programmer doesn't have to handle that.
  6. Variables in Java can be declared anywhere in a program. (Although it is recommended to declare/define them at the beginning of blocks).
  7. Reuse of code achieved by inheritance.
  8. By default members are private.
  9. During the execution of bytecode by JVM, it does not substitute the entire classes of package which are imported in the program. It just enters the package and executes the class and returns result in to the program. Due to this less memory is used by java program.
  10. C
  11. C uses concept of structures (not object oriented).
  12. In C we use the concept of pointers whereas there are no pointers used in JAVA
  13. In C the programmer needs to manage memory manually. "malloc ()" and "free ()" are the fundamental memory allocation library calls.
  14. In C the declaration of variables should be on the beginning of the block.
  15. C supports go to statement, struct and union unlike Java
  16. C is compiled to the machines "native language" so it's execution is much faster than Java's.
  17. No reuse in code and by default members are public.
  18. C programs will have a larger memory footprint than an equivalent program written in pure machine code, but the total memory use of a C program is much smaller than the a Java program because C does not require the loading of an execution interpreter like the JVM.
  19. The main differences between Java and C are speed, portability, and object-orientation.
  20. Java was created for the purpose of making a language that could be implemented on many different types of computers (cell phone, mac, PC, Linux, etc.) C on the other hand can only be run on a computer of the same type as the one that compiled the program.
  21. One of the costs of this portability in Java is speed. On numerous benchmarks Java still lags behind C in terms of speed, but that gap is narrowing.
  22. Java is also object-oriented, whereas C is not. Java allows a user to create classes that contain data and methods. C is not capable of doing that.
  23. Note: There are comments associated with this question. See the discussion page to add to the conversation.