Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-22-2007, 06:32 PM
Member
 
Join Date: Dec 2007
Posts: 4
Ace_Of_John is on a distinguished road
abstract extended and hiding variable??
im trying to fugure out how to make a variable of an abstract class available for the subclass but not available externaly.
// The abstract
Code:
public abstract class Animal { protected String name;//<---This is the variable public String getName() { return (name); } public void setName(String nm) { name=nm; } public abstract void speak(); }
//The subclass
Code:
public class Bird extends Animal { public Bird(String nm) { name = nm; } public void speak() { System.out.println("Tweet tweet chirp"); } }
//The main class, i dont want this to work because i would prefer a setName() method
Code:
public static void main(String[] args) { Bird abird=new Bird(""); abird.name="Dodo";//<======Stop this abird.setName("Dodo")//<====Leaving only this }
this is just a practice and is not part of an important project. if anyone can help i would be very greatfull. thankyou
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-23-2007, 06:02 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Code:
private String name;
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-23-2007, 06:32 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by hardwired View Post
Code:
private String name;

I saw this hardwired, but then name would not be available within class Bird. To access it from Bird, then when calling the constructor instead of
Code:
name = nm;
it would have to be
Code:
setName(nm);
as in the Main method, technically not making the variable available to the subclass. I don't think that's what is desired - I believe s/he wants to use the constructor as is. It's possible in a non-abstract class while using a protected variable.. but I'm not sure how to do it abstract-wise.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-23-2007, 07:35 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,189
hardwired is on a distinguished road
Try this
Code:
public class ClassTest { public static void main(String[] args) { Bird abird=new Bird(""); System.out.println(abird); // abird.name="Dodo"; // compile error: name has private access in Animal abird.setName("Dodo"); System.out.println(abird); } } abstract class Animal { private String name; public String getName() { return (name); } public void setName(String nm) { name = nm; } public abstract void speak(); public String toString() { return getClass().getName() + "[name: " + name + "]"; } } class Bird extends Animal { public Bird(String nm) { super.setName(nm); } public void speak() { System.out.println("Tweet tweet chirp"); } }
Another way you could do it is like this
Code:
abstract class Animal { private String name; Animal(String name) { this.name = name; } ... class Bird extends Animal { public Bird(String nm) { super(nm); }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-23-2007, 08:50 AM
Member
 
Join Date: Dec 2007
Posts: 12
spoon! is on a distinguished road
the original post should work; the variable is protected; that does exactly what you want
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 12-23-2007, 03:46 PM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 840
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Quote:
Originally Posted by spoon! View Post
the original post should work; the variable is protected; that does exactly what you want
Yes, it works - as was implied in the original post. But the OP is asking for a customization on the abstract class's variable so it can't be accessed externally(as in main()). It appears you are misreading the OP's post.. but I'll leave that for her or him to decide officially since its his/her thread.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hiding parameters from URL Saurabh321 New To Java 0 02-05-2008 01:43 PM
Error! "filename" is not abstract and does not override abstract method... hasani6leap New To Java 4 01-06-2008 05:59 PM
hiding inherited methods java_fun2007 New To Java 1 01-05-2008 03:16 PM
Hiding the frame’s title bar Java Tip Java Tips 0 12-21-2007 09:41 AM
Top menu fields hiding youayshu JavaServer Pages (JSP) and JSTL 0 11-13-2007 08:10 AM


All times are GMT +3. The time now is 02:55 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org