Welcome to Dream.In.Code
Become a Java Expert!

Join 150,170 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,292 people online right now. Registration is fast and FREE... Join Now!




Overloading and Overriding

 
Reply to this topicStart new topic

Overloading and Overriding, I am trying to figure out the difference between the two.

jgear
23 Aug, 2008 - 07:53 AM
Post #1

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

java

//I am having a hard time figuring out the difference between the two. I was hoping some one could expalin it in laymen's terms.

//An overriding example from java.com


public class Animal {

public static void testClassMethod(){

System.out.println("The class method in Animal.");

}

public void testInstanceMethod(){

System.out.println("The instance method in Animal.");

}

}

public class Cat extends Animal{

public static void testClassMethod(){

System.out.println("The class method in Cat.");

}

public void testInstanceMethod(){

System.out.println("The instance method in Cat.");

}

public static void main(String[] args){

Cat myCat = new Cat();

Animal myAnimal = myCat;

Animal.testClassMethod();

myAnimal.testInstanceMethod();

}

}


//An overloading example from java.com

public class DataArtist {



public void draw(String s) {

}

public void draw(int i) {

}

public void draw(double f) {

}

public void draw(int i, double f) {

}

}

*edit: Please use code tags in the future, thanks! code.gif

This post has been edited by Martyr2: 23 Aug, 2008 - 08:03 AM
User is offlineProfile CardPM
+Quote Post

jgear
RE: Overloading And Overriding
23 Aug, 2008 - 08:04 AM
Post #2

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

Never mind. I think I have figured it out. Sorry for any inconvenience.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Overloading And Overriding
23 Aug, 2008 - 08:10 AM
Post #3

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Think of the two like this...

Overloading is when you have more than one method of a class that has the same name but different parameters. You often see this with constructors where you will have a constructor with no parameters, another constructor (with the same name of course) but takes in a parameter like integer and yet another constructor that might take in a parameter like string.

When you look at the java documentation you may see methods with the same names and have different types of parameters with them. Then you know they are overloaded. This takes place within THE SAME CLASS.

Overriding is when a child class inherits from a parent class (aka base class) and instead of using the method of the parent defines its own class with the EXACT SAME SIGNATURE but with its own implementation.

So lets say you have an animal base class. This class has a method called "speak". In the method it says "animal speaks!". Now you derive from this class a class called "cat". If I was to call the speak() method it would say "animal speaks" because it inherited it from the base class. But lets say I then give the cat class its own speak method (exact same method and parameters as the parent) and inside it I write "cat meows!". Now when I call "speak" on the cat class it will meow, not call its parent method speak which would say "animal speaks". I have overridden the parent method and created my own version of speak in the child class. This happens with A CHILD AND A PARENT CLASS.

Hope that clarified it better for you. smile.gif
User is offlineProfile CardPM
+Quote Post

jgear
RE: Overloading And Overriding
23 Aug, 2008 - 09:31 AM
Post #4

New D.I.C Head
*

Joined: 17 Aug, 2008
Posts: 47

Hey thanks a lot. That really clarified the difference between the two. Now I think I can look at code and tell when either one is being used.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:12AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month