• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Overriding

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why we need to overload a method ?
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overload or override ?
 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My guess is that you're asking about overriding. It makes polymorphism what it is. If you have Head First Java, chapter 7 explains it well (2nd Edition). Overloading AFAIK is not used much in Java, but in my opinion its only use is to make the programmer's job easier. Like enums.

That is using an overloaded operator. If you couldn't overload methods, you'd be forced to use
 
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the ++ operator really overloaded? I don't think it is; the only overloaded operators I can think of are + after a String and maybe &^| for logical and bitwise operations. Apart from that programmers cannot overload their own operators, as they can in C++ or C#.

Method overloading is a very useful and commonly-used practice, even though some languages (eg Eiffel) prohibit it. Look at System.out.println which appears in the API here, ten times. Each version has a different parameter type, so it handles numbers differently from writing, etc.
Look at this example about clocks, which is similar to an example in Deitel and Deitel's book:You can say new Clock(); which gives midnight, new Clock(12); which gives midday, new Clock(12, 34); which gives 26 minutes to 1 at lunchtime, and new Clock(12, 34, 56); which is nearly 25 minutes to 1 at lunchtime.

Look at the setSize method in Component, which does the same thing, but allows the size details to be passed in two different formats.


CR

[edit]Slight change to one URL quoted[/edit]
[ April 22, 2007: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the ++ operator really overloaded?

Yes, most definitely!!!

It is overloaded since there is such operator, such as in C. Read any Programming languages book for answer!

+ operator is overloaded since FORTRAN time!

Thanks!
[ April 22, 2007: Message edited by: Roseanne Zhang ]
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew + was overloaded, but not ++. I can't look any books up before tomorrow. Thank you.

CR
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Overloaded as in ++n and n++?
Yes, what a silly mistake for me to make
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Overloaded as in ++n and n++?
Yes, what a silly mistake for me to make



Overloaded as in other operations as well. You could customize such overloading to suit your needs. For example the complex number 3 + 4j could be changed to 4 + 4j with a ++ increment. Just an example
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 0; i++;
long l = 5; l++;
[ April 22, 2007: Message edited by: Roseanne Zhang ]
 
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
Is the ++ operator really overloaded? I don't think it is; the only overloaded operators I can think of are + after a String and maybe &^| for logical and bitwise operations.
(...)



I doubt if the unary AND, OR and XOR operations could be considered as "overloaded". Aren't they operations on fundamental types? Besides, what other semantics do they have besides those bit-wise operations?

- Anand
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anand Hariharan:


I doubt if the unary AND, OR and XOR operations could be considered as "overloaded". Aren't they operations on fundamental types? Besides, what other semantics do they have besides those bit-wise operations?

- Anand



They can be used with boolean operands. The difference between them and the && and || is that && and || are short-circuit operators.

 
Anand Hariharan
Rancher
Posts: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:


They can be used with boolean operands. The difference between them and the && and || is that && and || are short-circuit operators.



While what you say is true, it doesn't change what I said viz., how are they "overloaded"? In the case of '+', while it conventionally stands for addition, the operator takes the meaning of concatenation in the context of strings.
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't call long++ and int++ overloading; they have the same effect, ie adding 1 to each number. I was mistaken earlier because I forgot there are i++ and ++i which have different effects.

I agree with Keith Lynn about &^|; I meant that they can be used both as bit-wise operators and as logical operators; they have a different result when used on numbers and on booleans. The expression "a & b" gives a totally different result when the operands are booleans from the result with integer numbers.

And John Meyers (not that I know a lot about complex numbers): How can you use ++ to change "3 + 4i" to "4 + 4i" ?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the context of Java programming, ++ seems a very poor example to describe the concept of overloading, since in Java the programmer can't overload any operators themselves. Whether operator "overloading" is really overloading or not is academic; it depends what definition you use. Certainly it's not overloading in the sense that the JLS defines the term. That applies only to methods. There are other definitions from other languages, and you can use one of them I suppose, but I don't see how it really helps a Java beginner who is trying to understand how overloading works IN JAVA.

[Campbell]: I wouldn't call long++ and int++ overloading; they have the same effect, ie adding 1 to each number.

That seems a very odd place to draw the line. Math.abs(float) and Math.abs(double) have the same effect too, aside from operating on (and returning) different types. Are you saying Math.abs() is not overloaded?

Personally I would not use the term "overloaded" to refer to operators in Java at all. But if I did (by analogy to C++), I would certainly apply it where the only difference is the type of the operands. After all, that's the whole point of overloading when we talk about overloading methods. (I.e. traditional overloading in the sense that's actually useful for Java beginners to understand.)

Pvnaidu Naidu , if you're still there, I recommend you start a new thread where you ask about either overloading or overriding. They're not the same thing. This thread is probably too far gone in irrelevant trivia to help you anymore; sorry. Though Campbell's first response is useful if you just ignore the discussion of ++...
[ April 22, 2007: Message edited by: Jim Yingst ]
 
Campbell Ritchie
Marshal
Posts: 79707
381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right about rambling threads and irrelevant trivia, Mr Yingst. Sorry
 
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic