5 Replies - 569 Views - Last Post: 07 April 2012 - 08:11 PM Rate Topic: -----

#1 anomaly  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-March 12

Overloading and Overriding methods in classes

Posted 06 April 2012 - 09:46 PM

I am working with Overriding and Overloading and inheritance. The assignment is to modify a class, begin, so it overrides another class, begin, that it is inheriting. I then have to modify a class, increase, so it overloads the inheriting class. If that doesn't confuse the daylights out of you.

Basically what I need help with is an example that shows how to override and overload methods in classes. Any help is appreciated.
Is This A Good Question/Topic? 0
  • +

Replies To: Overloading and Overriding methods in classes

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Overloading and Overriding methods in classes

Posted 06 April 2012 - 09:51 PM

class Animal {

   void makeNoise() {
     System.out.printl("noise");
   }
}

class Dog extends Animal {

   void makeNoise() {
     System.out.printl("bark");
   }
}
class Cat extends Animal {

   void makeNoise() {
     System.out.printl("miaou");
   }
}


Was This Post Helpful? 1
  • +
  • -

#3 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Overloading and Overriding methods in classes

Posted 07 April 2012 - 05:08 AM

Well pbl, that's only half of what he wanted to do. He also wanted to overload as well.

So I think it'd be more like...
public class Parent {
    public void talk() {
        System.out.println("I'm a parent!");
    }

    public void talk(String s) {
        System.out.println(s);
    }
}

public class Child {
    public void talk() {
        System.out.println("I'm a child!");
    }
}




Of course not exactly, but it has the overloading too.

Hope it helped!
~Crockeo

This post has been edited by Crockeo: 07 April 2012 - 05:08 AM

Was This Post Helpful? 2
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,885
  • Joined: 06-March 08

Re: Overloading and Overriding methods in classes

Posted 07 April 2012 - 04:46 PM

Let's combined them :)
class Animal {

   void makeNoise() {
     makeNoise("noise");
   }

   void makeNoise(String str) {
      System.out.println(str);
   }
}

class Dog extends Animal {

   void makeNoise() {
     makeNoise("bark");
   }
}
class Cat extends Animal {

   void makeNoise() {
     makeNoise("miaou");
   }
}



Was This Post Helpful? 1
  • +
  • -

#5 Crockeo  Icon User is offline

  • D.I.C Head

Reputation: 44
  • View blog
  • Posts: 247
  • Joined: 21-June 11

Re: Overloading and Overriding methods in classes

Posted 07 April 2012 - 07:48 PM

Wooh! Teamwork!
Was This Post Helpful? 0
  • +
  • -

#6 anomaly  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 27-March 12

Re: Overloading and Overriding methods in classes

Posted 07 April 2012 - 08:11 PM

View PostCrockeo, on 07 April 2012 - 07:48 PM, said:

Wooh! Teamwork!



Thanks to the both of you. This helps shine light on the matter for me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1