Object Orientation

Moderators: None (Apply to moderate this forum)
Number of threads: 46
Number of posts: 78

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Re: Simple question Posted by ytvsoftware on 16 Jan 2004 at 2:25 AM
: Hi there,
:
: I have a simple question involving member inheritance in Java; I was wondering what the best way of tackling this sort of design issue is. I will describe an example.
:
: There are two types of Gun, a simple one, used by Soldiers:
:
: class Gun {}
:
: and an advanced one, SilencedGun which only Commanders can use. SilencedGun is a subclass of Gun:
:
: class SilencedGun extends Gun { }
:
: Now, a Soldier is defined as:
: class Soldier {
: ???? Gun gun; // see later
:
: public void setGun(Gun g) {
: gun = g;
: }
: public Gun getGun() {
: return gun;
: }
: }
:
: class Commander extends Soldier {
: ...
: }
:
: My question is this: How do we best code a Commander class, such that only SilencedGuns can be possessed by a commander, and never simply a Gun? If we leave the Commander class empty, then any client can call commander.setGun(new Gun()), which we don't want to happen. What access specifier should gun in the parent class have?
:
: Should I overwrite gun in Commander to be of type SilencedGun, and then add new get/set of type SilencedGun? Will that stop clients calling the superclass methods?
:
: What is the best solution to this sort of issue?
:
: Thanks for suggestions.
: GS
:

Thread Tree
c4scroller Simple question on 21 Aug 2003 at 2:49 PM
ytvsoftware Re: Simple question on 16 Jan 2004 at 2:25 AM
ytvsoftware Re: Simple question on 16 Jan 2004 at 3:06 AM
isa6699 This post has been deleted. on 7 May 2009 at 11:49 PM
mobman80 Re: Simple question on 22 Aug 2009 at 12:34 PM



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.