473,498 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

polymorphism

4 New Member
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.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();
}
}
Aug 3 '07 #1
4 1591
JoeMac3313
16 New Member
=========== Code =============
Expand|Select|Wrap|Line Numbers
  1.  
  2. // polymorphism
  3.  
  4. abstract class player // class is abstrct
  5. {
  6.       String name;
  7.  
  8.      player(String nm)
  9.      {
  10.          name=nm;
  11.      }
  12.  
  13.       //public String getName() // regular method
  14.       // {
  15.               // return(name);
  16.       // }
  17.  
  18.       abstract void play();
  19. }
  20.  
  21. class cricket_player extends player
  22. {
  23.         cricket_player(String var)
  24.         {
  25.                  super(nm);
  26.         }
  27.  
  28.     void play()
  29.     {
  30.         System.out.println("play cricket:");
  31.         return name;
  32.     }
  33. }
  34.  
  35. class hockey_player extends player
  36. {
  37.     hockey_player(String nm,String var)
  38.     {
  39.         super(nm);
  40.     }
  41.  
  42.     void play()
  43.     {
  44.         System.out.println("play hockey:");
  45.         return name;
  46.     }
  47. }
  48.  
  49. class football_player extends player
  50. {
  51.     football_player(String nm,String var)
  52.     {
  53.         super(nm);
  54.     }
  55.  
  56.     void play()
  57.     {
  58.         System.out.println("play football:");
  59.         return name;
  60.     }
  61. }
  62.  
  63. public class mcs24IIp39
  64. {
  65.     public static void main(String args[])
  66.     {
  67.         player ref; //set up var for an player1
  68.  
  69.         cricket_player aCplayer=new cricket_player("scahin"); // makes specific objects
  70.         hockey_player aHplayer=new hockey_player("dhanaraj");
  71.         football_player aFplayer= new football_player("bhutia");
  72.  
  73.         // now reference each as an animal
  74.         ref=aCplayer;
  75.         ref.play();
  76.  
  77.         ref=aHplayer;
  78.         ref.play();
  79.  
  80.         ref=aFplayer;
  81.         ref.play();
  82.     }
  83. }
  84.  
Reminder to put code tags around code
Aug 3 '07 #2
JosAH
11,448 Recognized Expert MVP
play() is a void method, i.e. it can't return anything.

kind regards,

Jos
Aug 3 '07 #3
praveen2gupta
201 New Member
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.
Aug 3 '07 #4
JosAH
11,448 Recognized Expert MVP
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
Aug 3 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

37
2786
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
18
12550
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
2
3364
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
11
2942
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
17
3827
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
6993
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7162
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6881
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1411
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.