5 Replies - 232 Views - Last Post: 18 May 2012 - 09:25 AM Rate Topic: -----

#1 Spoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-May 12

Override or overload. Can someone explain this?

Posted 18 May 2012 - 07:28 AM

public class InheritanceTest {

    public static void main(String[] args) {


        Parent child = new Child();
        Receiver advancedReceiver = new AdvancedReceiver();
        advancedReceiver.receive(child);
    }
}


class Parent {
    public void outMe() {
        System.out.println("Parent");
    }
}


class Child extends Parent {
    public void outMe() {
        System.out.println("Child");
    }
}


class Receiver {
    public void receive(Parent p) {
        System.out.println("Receiver");
        p.outMe();
    }
}


class AdvancedReceiver extends Receiver{
    public void receive(Child c) {
        System.out.println("AdvancedReceiver");
        c.outMe();
    }
}


Please :code:

This post has been edited by pbl: 18 May 2012 - 12:17 PM
Reason for edit:: Code tags fixed


Is This A Good Question/Topic? 0
  • +

Replies To: Override or overload. Can someone explain this?

#2 Benzoate  Icon User is offline

  • D.I.C Head


Reputation: 51
  • View blog
  • Posts: 230
  • Joined: 29-February 12

Re: Override or overload. Can someone explain this?

Posted 18 May 2012 - 07:34 AM

Please use code tags, but what are you asking? I ran the code and it appears to work fine.. what is the problem?
Was This Post Helpful? 1
  • +
  • -

#3 Spoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-May 12

Re: Override or overload. Can someone explain this?

Posted 18 May 2012 - 07:43 AM

In this code method receive looks like it's overridden since we've been taught the Child is a Parent but the output makes it look like it is overloaded.
Was This Post Helpful? 0
  • +
  • -

#4 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

Re: Override or overload. Can someone explain this?

Posted 18 May 2012 - 08:16 AM

receive is overloaded, because even though the two methods aren't in the same class, the subclass can access the methods of the parent class. So, if you call the receive method, and send in a Parent, it will go to the Receiver receive method. If you send receive a Child object, it will use the AdvancedReceiver receive method.

So, the two methods are overloaded, but if they accept the same parameters, they would have been overridden methods.

And what Benzoate said, please use code tags.
Was This Post Helpful? 2
  • +
  • -

#5 Spoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 16-May 12

Re: Override or overload. Can someone explain this?

Posted 18 May 2012 - 08:35 AM

Thats a great explanation thanks MangoTux. Sorry I will use tags in future :-)
Was This Post Helpful? 0
  • +
  • -

#6 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

Re: Override or overload. Can someone explain this?

Posted 18 May 2012 - 09:25 AM

No problem, it's a good review for me.

As for the code tags, it just helps make your code read easier.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1