21 Replies - 1391 Views - Last Post: 13 July 2012 - 07:08 AM
#1
Overriding/Overloading
Posted 03 July 2012 - 01:02 PM
Im busy learning Overriding/Overloading methods.
Would i be correct in saying that in overriding methods you cant pass any arguments in the parameters unless one as been passed in the super class? and all the methods have to have the same return type?
And in overloading methods you can pass in more than one argument in the parameters and you can extend/inherit those arguments into the next class with new arguments?
Thanks!
Replies To: Overriding/Overloading
#2
Re: Overriding/Overloading
Posted 03 July 2012 - 01:05 PM
For overloading, it's not about "extending" methods. For method overloading, each method is considered a distinct method. The only reason the term "overloading" is used is because the methods have the same name. So if you define a method, you can invoke it.
#3
Re: Overriding/Overloading
Posted 03 July 2012 - 01:48 PM
#4
Re: Overriding/Overloading
Posted 03 July 2012 - 02:00 PM
pbl, on 03 July 2012 - 10:48 PM, said:
This is not true. If B.foo is supposed to override A.foo, B.foo needs to either return the same type as A.foo or a subclass. If B.foo returned something else, any code that calls foo on A object and uses its return value would break when a B is passed in instead.
You're right that the return type isn't part of the signature, but that just means that using a different return type won't create an overload. It will cause a compilation error instead.
#5
Re: Overriding/Overloading
Posted 03 July 2012 - 02:10 PM
int foo(int bar) {
}
String foo(int bar) {
}
is not legal
#6
Re: Overriding/Overloading
Posted 04 July 2012 - 02:11 AM
I was told that in overriding methods the return type needs to be the same as the super class but in overloading methods the return types can be different?
If that is right in what i said about the overloading methods then why are you allowed to have different return types?
This post has been edited by BrendanH: 04 July 2012 - 02:16 AM
#7
Re: Overriding/Overloading
Posted 09 July 2012 - 01:13 PM
public class Animal {
public void eat() {}
public void printYourself() {
// code goes here
}
}
class Horse extends Animal {
public void printYourself(){
// code goes here
super.printYourself();
Above is a the sample program in my text book trying to explain Invoking a Superclass Version of an Overridden Method.
Now by using the super() keyword at the end will it then just be coping the code from the Animal class into the Horse class?
This post has been edited by BrendanH: 09 July 2012 - 01:13 PM
#8
Re: Overriding/Overloading
Posted 09 July 2012 - 01:17 PM
#9
Re: Overriding/Overloading
Posted 09 July 2012 - 01:28 PM
Here's the class:
class Horse extends Animal {
public void printYourself(){
System.out.println("I am a horse, and so.... ");
super.printYourself();
}
public static void main (String[] args)
{
Horse h = new Horse();
h.printYourself();
}
}
And the superclass:
public class Animal {
public void eat() {}
public void printYourself() {
System.out.println("I am an animal");
}
}
And the bytecode. (See the lines 8-9 of printYourself)
/home/OMGEO/jon.kiparsky:2039 $ java Horse
I am a horse, and so....
I am an animal
[OMGEO\jon.kiparsky: Mon Jul 09 16:20]
/home/OMGEO/jon.kiparsky:2040 $ javap -v Horse
Compiled from "Horse.java"
class Horse extends Animal
SourceFile: "Horse.java"
minor version: 0
major version: 50
Constant pool:
[snipped]
{
Horse();
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method Animal."<init>":(/>)V
4: return
LineNumberTable:
line 1: 0
public void printYourself();
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String I am a horse, and so....
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: aload_0
9: invokespecial #5; //Method Animal.printYourself:()V
12: return
LineNumberTable:
line 3: 0
line 5: 8
line 6: 12
public static void main(java.lang.String[]);
Code:
Stack=2, Locals=2, Args_size=1
0: new #6; //class Horse
3: dup
4: invokespecial #7; //Method "<init>":(/>)V
7: astore_1
8: aload_1
9: invokevirtual #8; //Method printYourself:()V
12: return
LineNumberTable:
line 10: 0
line 11: 8
line 12: 12
}
This post has been edited by jon.kiparsky: 09 July 2012 - 01:29 PM
#10
Re: Overriding/Overloading
Posted 11 July 2012 - 04:43 PM
#11
Re: Overriding/Overloading
Posted 11 July 2012 - 04:50 PM
private Cursor getCursor() {
}
private int getCursor()
}
Because the compiler thinks those are the same method. Does this help?
#12
Re: Overriding/Overloading
Posted 11 July 2012 - 04:58 PM
Quote
Polymorphism in object-oriented programming (Wikipedia)
The program does this as it needs to during runtime, not when you compile it. Maybe that why it makes programs slower.
#13
Re: Overriding/Overloading
Posted 11 July 2012 - 06:46 PM
BrendanH, on 11 July 2012 - 07:43 PM, said:
What do you mean ? The compiler cannot "look" at the Object. These links are established at compile time.
And avoid words like "reference" this is a C/C++ concept that you should ignore in Java
jared.deckard, on 11 July 2012 - 07:58 PM, said:
No !!!
#14
Re: Overriding/Overloading
Posted 11 July 2012 - 10:28 PM
pbl, on 11 July 2012 - 06:46 PM, said:
pbl, please explain.
Java is translated into an intermediate language which is compiled at runtime by a Just In Time (JIT) compiler. C# .NET works the same way and is slower than statically compiled languages like C and C++. The JIT compiler does "look" at objects and binds them to an interface at runtime.
Overloaded methods share the same name, but have a different signatures (different parameters).
Classes that inherit an interface must implement all method names/signatures the interface defines.
An overloaded method in a class that is not declared in the interface is not accessible when an object of the class is cast as the interface type.
Only methods declared by an interface can be called when an object is cast to the interface.
Off topic, but a reference is a concept you should never forget. Object oriented languages use references heavily.
#15
Re: Overriding/Overloading
Posted 12 July 2012 - 03:35 AM
Thanks!
|
|

New Topic/Question
Reply



MultiQuote








|