• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheriting from an instantiated object  RSS feed

 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, new to the forum, be gentle with me. Hoping it will be the start of a long and fruitful relationship!!!


I have a Member class and a Player class. Players extends Members.
I know that player will have all the methods of Member but I was wondering how I could link a player object directly to a member object.


What im looking for is the p1.getName() to return John.
Can I do something within the Player class for it to inherit from a Member object
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Player object already is a Member object. So if you want to create a Player and set its name, you just need the Player object. Creating another Member object is not what you want to do, since then you have two unrelated Member objects. Here's some better code:


 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Paul thanks for the quick reply.
The member gets instantiated first and not all Members are Players.
Its only later does a member become a Player.
I could declare a Member object in the Player class and link it that way;

I was just hoping it could be done??
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could certainly do that. In fact having a reference to a Member from a Player object is in many ways superior to having Player extend Member. There's a design rule which says "Prefer composition over inheritance" and it looks like you just did that.
 
Ian Looney
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cheers Paul.
Ill go with that one so.
THanks for the help

 
Junilu Lacar
Sheriff
Posts: 11479
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ian Looney wrote:The member gets instantiated first and not all Members are Players.
Its only later does a member become a Player.
I could declare a Member object in the Player class and link it that way;
...
I was just hoping it could be done??

Just because it can be done syntactically doesn't mean that it's the right thing to do conceptually. What's the idea you're trying to represent in your program by "linking" Member and Player? Do players have essentially the same behaviors and attributes that a member has? When you say "A member becomes a player later on" the first thing that comes to my mind is an "isPlayer" attribute in the Member class. This attribute will be false initially, then some time later when certain conditions are met, the attribute is set to true. It could be as simple as that without having to resort to inheritance or composition.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!