aspose file tools*
The moose likes Beginning Java and the fly likes Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overriding " Watch "Overriding " New topic
Author

Overriding

pvnaidu naidu
Greenhorn

Joined: Apr 17, 2007
Posts: 2
Why we need to overload a method ?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Overload or override ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Eric Daly
Ranch Hand

Joined: Jul 11, 2006
Posts: 143
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


Studying for SCJP 6
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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 ]
Roseanne Zhang
Ranch Hand

Joined: Nov 14, 2000
Posts: 1953
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
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
I knew + was overloaded, but not ++. I can't look any books up before tomorrow. Thank you.

CR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Overloaded as in ++n and n++?
Yes, what a silly mistake for me to make
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

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

Joined: Nov 14, 2000
Posts: 1953
int i = 0; i++;
long l = 5; l++;
[ April 22, 2007: Message edited by: Roseanne Zhang ]
Anand Hariharan
Rancher

Joined: Aug 22, 2006
Posts: 257

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


"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 22, 2006
Posts: 257

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
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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" ?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
You are right about rambling threads and irrelevant trivia, Mr Yingst. Sorry
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overriding