Overriding/Overloading

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 3876 Views - Last Post: 13 July 2012 - 07:08 AM Rate Topic: -----

#1 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Overriding/Overloading

Posted 03 July 2012 - 01:02 PM

Hello

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!

Is This A Good Question/Topic? 0
  • +

Replies To: Overriding/Overloading

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,780
  • Joined: 27-December 08

Re: Overriding/Overloading

Posted 03 July 2012 - 01:05 PM

You are correct for overriding.

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.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Overriding/Overloading

Posted 03 July 2012 - 01:48 PM

View PostBrendanH, on 03 July 2012 - 04:02 PM, said:

and all the methods have to have the same return type?

return type is not part of the signature of the method so they do not require to return the same thing or one may even return something and the other being void
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2101
  • View blog
  • Posts: 3,203
  • Joined: 21-June 11

Re: Overriding/Overloading

Posted 03 July 2012 - 02:00 PM

View Postpbl, on 03 July 2012 - 10:48 PM, said:

return type is not part of the signature of the method so they do not require to return the same thing or one may even return something and the other being void


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.
Was This Post Helpful? 2
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Overriding/Overloading

Posted 03 July 2012 - 02:10 PM

Sorry you are right. What I had in mind was that you can't differentiate methods by their return type

int foo(int bar) {
}

String foo(int bar) {
}


is not legal
Was This Post Helpful? 1
  • +
  • -

#6 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Overriding/Overloading

Posted 04 July 2012 - 02:11 AM

Now that code is not legal because on line four you need to have a string argument in the parameters?

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

Was This Post Helpful? 0
  • +
  • -

#7 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

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

Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,780
  • Joined: 27-December 08

Re: Overriding/Overloading

Posted 09 July 2012 - 01:17 PM

It physically invokes the method in the superclass, just like if you were to invoke any other method. Have you tried running a sample to see what will happen? I don't mean to be rude, but a lot of your questions are textbook questions that could be answered by reading the Oracle tutorials, reading DIC tutorials, and running code samples yourself. We're happy to help, but a big part of learning how to code is learning how to teach yourself a little too.
Was This Post Helpful? 2
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7641
  • View blog
  • Posts: 12,889
  • Joined: 19-March 11

Re: Overriding/Overloading

Posted 09 July 2012 - 01:28 PM

Copying? No. It'll be calling the method of the superclass.


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

Was This Post Helpful? 3
  • +
  • -

#10 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Overriding/Overloading

Posted 11 July 2012 - 04:43 PM

When it comes to overloaded methods the compiler uses the reference type to determine which overloaded method is invoked but when you using polymorphism in a overloaded method does the compiler then change and look at the object and not the reference ?
Was This Post Helpful? 0
  • +
  • -

#11 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: Overriding/Overloading

Posted 11 July 2012 - 04:50 PM

I'm not TOTALLY sure what you are asking, but the compiler uses the method name and the parameters to determine which methods override/overload others. The return type does not determine this. So this is illegal:

private Cursor getCursor() {

}

private int getCursor() 

}



Because the compiler thinks those are the same method. Does this help?
Was This Post Helpful? 1
  • +
  • -

#12 jared.deckard  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 46
  • Joined: 11-July 12

Re: Overriding/Overloading

Posted 11 July 2012 - 04:58 PM

Quote

The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run-time (this is called late binding or dynamic binding).
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. :)
Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Overriding/Overloading

Posted 11 July 2012 - 06:46 PM

View PostBrendanH, on 11 July 2012 - 07:43 PM, said:

does the compiler then change and look at the object and not the reference ?

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

View Postjared.deckard, on 11 July 2012 - 07:58 PM, said:

The program does this as it needs to during runtime, not when you compile it. Maybe that why it makes programs slower. :)

No !!!
Was This Post Helpful? 1
  • +
  • -

#14 jared.deckard  Icon User is offline

  • New D.I.C Head

Reputation: 18
  • View blog
  • Posts: 46
  • Joined: 11-July 12

Re: Overriding/Overloading

Posted 11 July 2012 - 10:28 PM

View Postpbl, on 11 July 2012 - 06:46 PM, said:

No !!!


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.
Was This Post Helpful? 0
  • +
  • -

#15 BrendanH  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 174
  • Joined: 05-May 12

Re: Overriding/Overloading

Posted 12 July 2012 - 03:35 AM

Can someone please tell me the difference between overriding and polymorphism? as at the moment they both look very similar to me.

Thanks!
Was This Post Helpful? -1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2