Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overriding and OverLoading

 
Suri Babs
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.Why we need overridding and overloading?
2. These are bad according to me, since these process will happen at run time, i mean late-binding. And this is a performance issue.

Could any satisfy me about these questions.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Overloading occurs at compile-time, so there is no real issue there. (See here for an example)
While it is true that over-riding does create a performance issue, so does the rest of Object-Oriented Programming. Thankfully, machines are fast enough these days (and the languages optimized enough) that the hit to performance is not noticed. While it is true that you could write an extremely fast program in C, development and maintenance of said program would be extremely difficult. Programs today do so much more than their predecessors of a decade ago that not using an OO-language is a major drawback (ever look at the souce code for a large PERL program? They are difficult to get through.)
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Suri
"Overriding" gives a class an easy way to intercept messages before they get to its superclass.As you know, most OOP languages implement overriding based on the run-time class of objects. In C++, run-time overriding is an option invoked with the "virtual" keyword.
"Overloaded method" can give you convenient to write you programme,the best way for me is that it groups the relevent method into the same name, do not need to write a new method with differ name if the method just contains different parameter lists.
The following is a example
PrintWriter object is used to do output some text to a file.It has many methods, I use some methods to illustrate what I want to say:
void print(String s)
void print(int i)
void print(long l)
void print(float f)
Now,you only need to know what data type you want to put it to a text file.
I know it would confuse some beginner,but after you master this concept,you may like it.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are bad according to me, since these process will happen at run time, i mean late-binding.
The current programming paradigm is "Write clear code that is easy to understand, extend, and maintain, and resist the temptation to optimize."
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming is loaded with comprimises and balancing acts. In this case, it is the question of maintenance vs. performance. Many such conflicts exist and you have to understand the pros and cons of different approaches in order to decide what approach is best for a given problem. With polymorphism and overriding, the pay off is in maintenence and code re-use. As you mentioned, the cost is some performance overhead for late-binding. I don't view this as bad. It's simply a trade-off.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being an old curmudgeon, I remember the early days of C vs C++ on PCs and the argument that late binding would never perform. Not only was there dynamic lookup, but in jumping between classes to execute methods you were more likely to execute long jumps than short jumps (oh the agony!) and (after the PC got virtual memory) more likely to cause page faults. There were regular articles excitedly describing how various compiler vendors were tweaking their VTables.
Now hardware is fast enough we tend to forget all that stuff, except maybe keep your inheritance tree wider than deep.
But to repeat a conclusion from above: Write source code for humans to read. Don't compromise readability for what the compiler or JVM might do!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic