This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Overwriting and Overloading Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Overwriting and Overloading Question" Watch "Overwriting and Overloading Question" New topic
Author

Overwriting and Overloading Question

Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Hi guys

I have a little question on these two features of Java that I need to submit into as a project. Whilst I'm not asking for a direct answer, I do need help on finding information. I've been to my library, looked on google, wikipedia and the sun website but there seems to be no decent explaination out there. If anyone could point me out to a good source of information or be able to give me any help on this

1.What is the difference between overloading and overwriting?


Thank you very much
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Did someone really give you a test question asking about "overwriting?" The word is "overriding"; more correctly known as "polymorphism."


[Jess in Action][AskingGoodQuestions]
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Suprisingly yeah, thats a quoute straight from the essay paper.

Would the wording be the reason why I can't find any information online?
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
lol yeah most likely....

overloading is when you have a method that takes distinct parameters that can be manipulated in diff. applications

such as..

public int Num ( int r, Num a, Num b)
{}

in this method you can set a or b not only to ints, but to doubles and etc..

now overRIDING, for example, say you have an abstract method:

abstract int Num();

you can use this method in any applications that extends this methods class

as long as it has return type int..

this is an example of overriding, because this method can do different things in different classes

hope this helps a little bit..

justin


You down with OOP? Yeah you know me!
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Thanks Justin for that, it helps very much
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

to clarify, overloading is when you have the same function NAME, but with different signiatures... i.e. different types of parameters being passed in. the return type is not part of the signiature. so if you have

you have overloaded the method.

overriding is when you re-define a method in a subclass. the Object class defines the toString() method, but in a way that's generally not much help.

when your class then re-defines it to something useful, you are overriding it. when you call the toString method on your object, the JVM knows to use the most specific version of the method it can find. even if you have an Object reference, since the object itself is a myClass, the JVM will use the version in myClass. the Object method is 'overridden'.
[ April 13, 2006: Message edited by: fred rosenberger ]

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139

now overRIDING, for example, say you have an abstract method:

abstract int Num();

you can use this method in any applications that extends this methods class

as long as it has return type int..


-------------------------------------------------------
Really justin, then why this error

abstract class Foo
{

abstract int show();

}

class Goo extends Foo
{
int show(int j)
{
System.out.println("Hello");
return 1;
}


public static void main(String ar[])
{
Foo f = new Goo();
f.show(7);
}

}

My compiler says

Goo.java:21: Goo should be declared abstract; it does not define show() in Foo
class Goo extends Foo
^
Goo.java:33: show() in Foo cannot be applied to (int)
f.show(7);


regards
[ April 13, 2006: Message edited by: faisal usmani ]
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Well from all the info gather I have compiled the following

1. What is the difference between overloading and overriding?�

Overloading

A good name is better than a precious ointmen..- Ecclesiates 7:1

Overloading in Java stands for the overloading of a method. This feature of Java allows the programmers to have a class (es) call on the same named method multiple times, but have different outcomes in the same class (es). The class which contains a method of, say, myMethod (String firstName, String secondName) can contain an element of coding which is unique to that method. But if there is another method or class which needs to call on the myMethod�s internal structure, but allow it to contain different characteristics, the programmer will now need to call on the overloading technique. The structure of the method is known as the signature. When using the overloading technique, the programmer need not keep the signature (parameter types and number of parameters) the same. An example is as follows

Class Gambles
String firstName= new �John
String secondName= new � Smith�
myMethod(String firstName, String secondName)
System.out.println("Inside the Class Gambles the first and second name is� + � � myMethod);

Which will then out put John Smith

String firstName= new �George�
String secondName= new � Bush�
myMethod(String firstName, String secondName)
System.out.println("Inside the Class Gambles the first and second name is� + � � myMethod);

This will then output George Bush

Overriding
�To give a specific definition by the derived class for a method implemented in the base class�

If programmers chose to inherit methods to their new class from a base class but wants to by pass or re-define a method contained in the base class it is inheriting from, a technique called overriding must be initiated. If for instance, if a programmer created a ASeen class, but wanted to inherit from another class called BSeen and control a method in it, then the derived class will require the method to be overridden, ergo, controlling the behaviour of the definition of the method has been made

Overriding is somewhat similar to overwriting in a sense of inheriting information from another source. When overriding a method in an inheriting class, the method must still remain the same as before (same as overloading), but this time the signature information MUST remain the same. This is the one of the main differences. The reason why the keeping of the signature the same lies in the fact that of the parameters in the signature do differ between classes, the derived class with get two methods, instead of the one. This will then create a new method of the same name, therefore overloading the method. Another difference of overriding and overloading is that one; overloading forces new information into a method. Whilst overwriting is a technique which helps the programmer control the behaviour of a classes methods in which everyway they wish.


If anyone could give it a read and point out any mistakes and inconsitancies, i'll be greatful
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by faisal usmani:

-------------------------------------------------------
Really justin, then why this error

abstract class Foo
{

abstract int show();

}

class Goo extends Foo
{
int show(int j)
{
System.out.println("Hello");
return 1;
}


public static void main(String ar[])
{
Foo f = new Goo();
f.show(7);
}

}

My compiler says

Goo.java:21: Goo should be declared abstract; it does not define show() in Foo
class Goo extends Foo
^
Goo.java:33: show() in Foo cannot be applied to (int)
f.show(7);


regards

[ April 13, 2006: Message edited by: faisal usmani ]



Goo does not override show() in Foo. It has a method show(int) not a method show(). The signatures are not the same one does not override another.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Bo Gambles:
If anyone could give it a read and point out any mistakes and inconsitancies, i'll be greatful [/QB]


The description of overloading is inaccurate. You're just passing the same method different parameters, that's not overloading, that's just invoking a method. Overloading is merely when two methods have the same name and different signatures.

Overloading as described by the JLS:
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded.


Example of overloading:



The key here is that, once again, they all have the same name of "foo" but different signatures. What makes up a signature? The arguments primarily. With the introduction of generics the type parameters (if any) play a role as well but you don't need to worry about that yet. Just understand that, for all intents and purposes, if a method has the same name but different arguments it's overloaded.

Overridding is when a subclass declares a method with the same name and signature as a visible non-final method in it's superclass.



The method foo(int) in SubFoo overrides the method foo(int) in SuperFoo. Consequently if I were to use this code:



It would invoke the method in SubFoo, which overrides the one in SuperFoo.

Edit: Typos.
[ April 13, 2006: Message edited by: Ken Blair ]
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Ken, any chance of you explaining a little deeper with the explaination I used. Thanks
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Bo Gambles:
Ken, any chance of you explaining a little deeper with the explaination I used. Thanks


I'm not sure what you mean by 'deeper'. They're both fairly straightforward. Perhaps you want more detail as to the issues I have with your explanations? I'll try that.

Overloading in Java stands for the overloading of a method. This feature of Java allows the programmers to have a class (es) call on the same named method multiple times, but have different outcomes in the same class (es).


This is totally wrong. You're talking about invoking a method multiple times and passing it different values. Overloading happens when a class declares (well, technically it can inherit one and declare one but this is besides the point) two methods with the same name and different signatures. A method's signature is it's name and argument types. So in order for two methods to have the same name but different signatures they must obviously have different argument types. We're talking about the type, in the declaration, not what value happens to be passed to it when it's invoked.



Here foo is overloaded because they have the same name (foo) but different argument types. One has a single argument of type String, the other has a single argument of type int. Thus the argument types are not the same and so the method signatures are different. For argument types to be the same they must have the same number of arguments of the same type in the same order.

The class which contains a method of, say, myMethod (String firstName, String secondName) can contain an element of coding which is unique to that method. But if there is another method or class which needs to call on the myMethod�s internal structure, but allow it to contain different characteristics, the programmer will now need to call on the overloading technique. The structure of the method is known as the signature. When using the overloading technique, the programmer need not keep the signature (parameter types and number of parameters) the same.


I can't make heads nor tails of this. The last sentence seems appropriate, but the example you give has nothing to do with overloading and the rest of the paragraph is nonsensical.

If programmers chose to inherit methods to their new class from a base class but wants to by pass or re-define a method contained in the base class it is inheriting from, a technique called overriding must be initiated. If for instance, if a programmer created a ASeen class, but wanted to inherit from another class called BSeen and control a method in it, then the derived class will require the method to be overridden, ergo, controlling the behaviour of the definition of the method has been made


First of all, you cannot "inherit" a method that is "overridden". By definition a method that has been overridden is not inherited. Overridding is polymorphism and I would encourage you to Google that as I'd only butcher any attempted explanation of it.
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
^Thanks for the post, I don't think I really understood it properly in the first place

Was the last paragraph which concentrated on the differences OK>?
[ April 13, 2006: Message edited by: Bo Gambles ]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
No one explicitly said it, so I will. Different return types of a method of the same name in the same class is not enough to constitute a different signature.


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
so i got the over loading right then huh??

sorry about the overriding, i guess i dont know as much as i thought..

just trying to help....
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Bo Gambles:
^Thanks for the post, I don't think I really understood it properly in the first place

Was the last paragraph which concentrated on the differences OK>?

[ April 13, 2006: Message edited by: Bo Gambles ]


If I understood what you were trying to say, then it sounded pretty close. You're using a lot of odd terminology though.
Bo Gambles
Greenhorn

Joined: Mar 27, 2006
Posts: 14
Yeah, the terminology was because I've been using different sources of information and it was a big mish mash..Sorry

But basically I just was trying to explain the meaning of overriding. Do you inherit the class, and override the method? Thats the main thing I dont understand
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Yes, that is reasonably accurate though your wording is odd. I don't think I've ever seen it written that you "inherit a class", thought it may still be technically accurate.



B is said to be a direct subclass of A and A is said to be the direct superclass of B. You might also say that B is subclassing A, or extending A, but I don't think I've ever seen anyone call it inheriting.

If we now add a method to A that is visible to B:



B inherits the method aMethod, if we invoke aMethod() on an instance of B we'll get "Class A" printed. However, what if B did not want to inherit A? What if B wants to provide some other implementation of aMethod() in it's place? Because aMethod() is not final B can override it.



Now whenever aMethod() is invoked on an instance of B this implementation will be invoked instead of A's implementation. aMethod() in B is said to override aMethod() in A. But what if B didn't want to override aMethod(), but instead wanted to provide a very similar method with the same name but with a single int argument?



Now aMethod(int) overloads aMethod() because they have the same name, but different signatures. Remember the signature is the name and arguments. Since the arguments are different, the signatures are different. Instead of overridding aMethod() it overloads aMethod(). Now invoking myB.aMethod() will result in "Class A" being printed and invoking myB.aMethod(12) will reuslt in "Class B where parameter i = 12" being printed. We could take this a step further and both override aMethod() and overload it:



Now invoking myB.aMethod() will result in "Class B" being printed and invoking myB.aMethod(12) printing "Class B where i = 12".

So the difference between overloading and overridding is that one allows us to provide methods with the same name and different arguments where the other allows a subclass to provide a different implementation of a method in it's superclass.

I would suggest taking a look at the Java Tutorial or some other reputable source such as Bruce Eckel's Thinking In Java as you've either been reading some really screwed up stuff or misunderstood it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Overwriting and Overloading Question
 
Similar Threads
Unable to update my data.. why is that so..?
Random Access File - Overwrite Contents
Servlet initialization
Failed by one question
give XML and XSLT to view in HTML