• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inheriting from an instantiated object

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers Paul.
Ill go with that one so.
THanks for the help

 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic