Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Polymorphism Java Programming Help

Polymorphism Java Programming Help

Scheduled Pinned Locked Moved Java
javadata-structuresoophelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jude martin 99
    wrote on last edited by
    #1

    I am a beginner in Java Programming and I just started programming as a hobby. I am in need of help in my Polymorphism program. How do I create an application that has an array of some size, say 5. The array should be defined of the superclass type. Then create 5 objects where each class is represented at least once. Store the objects created into the array elements. Finally, have the application go to each element of the array and call the display method. I should find that the display method automatically called is the one defined in the object stored in the element. I already have a superclass which is animal and two subclasses dog and cat and a polymorphism application mainclass Polymorphism Application: MainClass

    public class MainClass {

    public static void main(String \[\] args)
    {
    	Animal a = new Animal();
    	a.displayName("Generic Animal");
    
    	Cat c = new Cat();
    	c.displayName("Fluffy");
    
    	Animal dog = new Dog();
    	dog.displayName("Scooby-Doo"); // Polymorphic method call -- Dog IS-A Animal
    
    	Animal cat = new Cat();
    	cat.displayName("Mittens");   // Polymorphic method call -- Cat IS-A Animal
    }
    

    }

    Superclass (Animal):

    public class Animal
    {
    public void displayName(String name)
    {
    System.out.println("My name is " + name);
    }
    }

    Subclass (Cat):

    public class Cat extends Animal
    {
    public void displayName(String name)
    {
    System.out.println("My name is " + name + " and I like to drink milk.");
    }
    }

    Subclass (Dog):

    public class Dog extends Animal
    {
    public void displayName(String name)
    {
    System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
    }
    }

    L 1 Reply Last reply
    0
    • J jude martin 99

      I am a beginner in Java Programming and I just started programming as a hobby. I am in need of help in my Polymorphism program. How do I create an application that has an array of some size, say 5. The array should be defined of the superclass type. Then create 5 objects where each class is represented at least once. Store the objects created into the array elements. Finally, have the application go to each element of the array and call the display method. I should find that the display method automatically called is the one defined in the object stored in the element. I already have a superclass which is animal and two subclasses dog and cat and a polymorphism application mainclass Polymorphism Application: MainClass

      public class MainClass {

      public static void main(String \[\] args)
      {
      	Animal a = new Animal();
      	a.displayName("Generic Animal");
      
      	Cat c = new Cat();
      	c.displayName("Fluffy");
      
      	Animal dog = new Dog();
      	dog.displayName("Scooby-Doo"); // Polymorphic method call -- Dog IS-A Animal
      
      	Animal cat = new Cat();
      	cat.displayName("Mittens");   // Polymorphic method call -- Cat IS-A Animal
      }
      

      }

      Superclass (Animal):

      public class Animal
      {
      public void displayName(String name)
      {
      System.out.println("My name is " + name);
      }
      }

      Subclass (Cat):

      public class Cat extends Animal
      {
      public void displayName(String name)
      {
      System.out.println("My name is " + name + " and I like to drink milk.");
      }
      }

      Subclass (Dog):

      public class Dog extends Animal
      {
      public void displayName(String name)
      {
      System.out.println("My name is " + name + " and I like to eat Scooby Snacks!");
      }
      }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You need to add constructors to your subclasses that take the animal's name and likes, then you can use the generic methods for any animal even if you do not know its class. Something like:

      import java.io.*;
      import java.util.Scanner;

      public class Test {

      public static void main(String \[\] args) {
      
          Animal\[\] zoo = new Animal\[3\];
          Animal a = new Animal("Generic Animal");
          a.displayName();
          
          Cat c = new Cat("Fluffy");
          c.displayName();
          
          zoo\[0\] = new Dog("Scooby-Doo");  // a dog
          
          zoo\[1\] = new Cat("Mittens");  // a cat
          for (int i = 0; i < 2; i++)
          	zoo\[i\].displayName();  // polymorphic calls
      }
      

      }

      // Superclass Animal

      public class Animal {

      protected String name;
      protected String likes;
      
      public Animal(String newname, String what) {
          name = newname;
          likes = what;
      }
      
      public Animal(String newname) {
          this(newname, "everything");
      }
      
      public void displayName() {
          System.out.println("My name is " + name + " and I like " + likes);
      }
      

      }

      // Subclass Cat
      public class Cat extends Animal {

      public Cat(String newname) {
          super(newname, "to drink milk");
      }
      

      }

      // Superclass Dog
      public class Dog extends Animal {

      public Dog(String newname) {
          super(newname, "to eat bones");
      }
      

      }

      As you can see you can extended it by adding more common functionality to the superclass, but this should give you some ideas to work with.

      Use the best guess

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups