Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By gimbal2

Thread: polymorphism example ...can anybody explain me this example plz asap

  1. #1
    vineet88 is offline Member
    Join Date
    May 2015
    Posts
    1
    Rep Power
    0

    Default polymorphism example ...can anybody explain me this example plz asap

    class Animal
    {
    void makeSound(){
    System.out.println("Animal Sound");
    }
    }

    class Cow extends Animal
    {
    void makeSound(){
    System.out.println("Cow Sound");
    }
    }

    class Cat extends Animal
    {
    void makeSound(){
    System.out.println("Cat Sound");
    }
    }

    class Dog extends Animal
    {
    void makeSound(){
    System.out.println("Dog Sound");
    }
    }

    class A
    {

    void playSound(Animal x){
    x.makeSound();
    }



    void playSound(Dog x){
    x.makeSound();
    }

    void playSound(Cow x){
    x.makeSound();
    }

    void playSound(Cat x){
    x.makeSound();
    }

    public static void main(String[] args)
    {
    A e = new A();



    Dog d = new Dog();
    e.playSound(d);

    Cat ct = new Cat();
    e.playSound(ct);

    Cow c = new Cow();
    e.playSound(c);


    }
    }

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: polymorphism example ...can anybody explain me this example plz asap

    Can you please ASAP explain what you don't understand about it?
    jim829 likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: polymorphism example ...can anybody explain me this example plz asap

    Also please edit your post and wrap the code in code tags to preserve its formatting.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    Artemia is offline Member
    Join Date
    May 2015
    Location
    Netherlands
    Posts
    39
    Rep Power
    0

    Default Re: polymorphism example ...can anybody explain me this example plz asap

    I actually read an explanation about polymorphism a little while ago that I thought was very nice.
    polymorphism is basically that an object can be viewed different ways through the use of interfaces and inheritance.

    for example take a cat. through inheritance a cat object can be viewed as a pet or an animal (inherited like this: 'Animals -> Pets -> cats')
    you can also say that like many other things in this world a cat can walk, so you give a cat object the 'Walk' interface, a cat can bite and scratch too so you give the cat object the 'Bite' and 'Scratch' interface.
    now when you create a cat object depending on what you want to use it for you can initiate it as an 'animal' object (like this: 'Animal myPet = new Cat();') or you could initiate it with an interface ('Bite myPet = new Cat();')

    the explanation I read talked about this and at the end said something like if you look at it from the front it's just a cat object, but if you change your view to look from above it's viewed as an animal object, then if you look at it from the right side it's viewed as an object that can bite etc.

    I thought it was a fun explanation, hoped it helps a bit :)

  5. #5
    benji2505 is offline Senior Member
    Join Date
    Sep 2014
    Location
    MA, USA
    Posts
    399
    Rep Power
    6

    Default Re: polymorphism example ...can anybody explain me this example plz asap

    I suggest you search for "shadowing and overriding methods".

Similar Threads

  1. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM
  2. How to use Polymorphism
    By nfsmwbe in forum New To Java
    Replies: 10
    Last Post: 01-16-2012, 02:15 PM
  3. Replies: 2
    Last Post: 12-13-2010, 01:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 07:22 AM
  5. what's polymorphism?
    By christina in forum New To Java
    Replies: 2
    Last Post: 08-05-2007, 10:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •