polymorphism

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hardik shah
    New Member
    • Jul 2007
    • 4

    #1

    polymorphism

    Hi,

    I am a Java Student, I am running following code but it is failed, I found ferror :




    =========== Code =============


    // polymorphism

    abstract class player // class is abstrct
    {
    String name;

    player(String nm)
    {
    name=nm;
    }

    //public String getName() // regular method
    // {
    // return(name);
    // }

    abstract void play();
    }

    class cricket_player extends player
    {
    cricket_player( String var)
    {
    super(nm);
    }

    void play()
    {
    System.out.prin tln("play cricket:");
    return name;
    }
    }

    class hockey_player extends player
    {
    hockey_player(S tring nm,String var)
    {
    super(nm);
    }

    void play()
    {
    System.out.prin tln("play hockey:");
    return name;
    }
    }

    class football_player extends player
    {
    football_player (String nm,String var)
    {
    super(nm);
    }

    void play()
    {
    System.out.prin tln("play football:");
    return name;
    }
    }

    public class mcs24IIp39
    {
    public static void main(String args[])
    {
    player ref; //set up var for an player1

    cricket_player aCplayer=new cricket_player( "scahin"); // makes specific objects
    hockey_player aHplayer=new hockey_player(" dhanaraj");
    football_player aFplayer= new football_player ("bhutia");

    // now reference each as an animal
    ref=aCplayer;
    ref.play();

    ref=aHplayer;
    ref.play();

    ref=aFplayer;
    ref.play();
    }
    }
  • JoeMac3313
    New Member
    • Jul 2007
    • 16

    #2
    =========== Code =============
    Code:
    // polymorphism
    
    abstract class player // class is abstrct
    {
          String name;
    
         player(String nm)
         {
             name=nm;
         }
    
          //public String getName() // regular method
          // {
                  // return(name);
          // }
    
          abstract void play();
    }
    
    class cricket_player extends player
    {
            cricket_player(String var)
            {
                     super(nm);
            }
    
    	void play()
    	{
    		System.out.println("play cricket:");
    		return name;
    	}
    }
    
    class hockey_player extends player
    {
    	hockey_player(String nm,String var)
    	{
    		super(nm);
    	}
    
    	void play()
    	{
    		System.out.println("play hockey:");
    		return name;
    	}
    }
    
    class football_player extends player
    {
    	football_player(String nm,String var)
    	{
    		super(nm);
    	}
    
    	void play()
    	{
    		System.out.println("play football:");
    		return name;
    	}
    }
    
    public class mcs24IIp39
    {
    	public static void main(String args[])
    	{
    		player ref; //set up var for an player1
    
    		cricket_player aCplayer=new cricket_player("scahin"); // makes specific objects
    		hockey_player aHplayer=new hockey_player("dhanaraj");
    		football_player aFplayer= new football_player("bhutia");
    
    		// now reference each as an animal
    		ref=aCplayer;
    		ref.play();
    
    		ref=aHplayer;
    		ref.play();
    
    		ref=aFplayer;
    		ref.play();
    	}
    }
    Reminder to put code tags around code

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      play() is a void method, i.e. it can't return anything.

      kind regards,

      Jos

      Comment

      • praveen2gupta
        New Member
        • May 2007
        • 200

        #4
        Hi
        There is some basic fundamental problem in the code. You wants to understand polymorphism. And you are clubbing it with abstract classes. To understand the Polymorphism there is no need of abstract clases. There classes are not linked with polmorphism. There is two Big problem in the code.

        1. You should not mix abstract with polymorphism( for understanding it ).
        2. Object of abstract class can't be created.(Object ref)

        Polymorphism allows you to pass parameters or signatures with the same name of method with different Input parameters or signatures.

        You can create more than one method with the same name but the signature should be different.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by praveen2gupta
          Polymorphism allows you to pass parameters or signatures with the same name of method with different Input parameters or signatures.
          I'd call that 'overloading'. Using an abstract class, extend it and implement the
          abstract method is a form of polymorphism. Note that all the classes could
          have implemented an interface instead which is another form of overloading.
          The two are much alike because in both cases a type is extended, either with
          implementation (extends a class) or whithout (implements an interface).

          kind regards,

          Jos

          Comment

          Working...