Overloading and Overriding

I am trying to figure out the difference between the two.

Page 1 of 1

3 Replies - 1593 Views - Last Post: 23 August 2008 - 10:31 AM Rate Topic: -----

#1 jgear  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 17-August 08

Overloading and Overriding

Posted 23 August 2008 - 08:53 AM

//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:

This post has been edited by Martyr2: 23 August 2008 - 09:03 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Overloading and Overriding

#2 jgear  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 17-August 08

Re: Overloading and Overriding

Posted 23 August 2008 - 09:04 AM

Never mind. I think I have figured it out. Sorry for any inconvenience.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4436
  • View blog
  • Posts: 12,307
  • Joined: 18-April 07

Re: Overloading and Overriding

Posted 23 August 2008 - 09:10 AM

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

#4 jgear  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 47
  • Joined: 17-August 08

Re: Overloading and Overriding

Posted 23 August 2008 - 10:31 AM

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

Page 1 of 1