Results 1 to 6 of 6
- 12-22-2007, 05:32 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 4
- Rep Power
- 0
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
//The subclassJava 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 main class, i dont want this to work because i would prefer a setName() methodJava Code:public class Bird extends Animal { public Bird(String nm) { name = nm; } public void speak() { System.out.println("Tweet tweet chirp"); } }
this is just a practice and is not part of an important project. if anyone can help i would be very greatfull. thankyouJava Code:public static void main(String[] args) { Bird abird=new Bird(""); abird.name="Dodo";//<======Stop this abird.setName("Dodo")//<====Leaving only this }
- 12-23-2007, 05:02 AM #2
Java Code:private String name;
- 12-23-2007, 05:32 AM #3
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
it would have to beJava Code:name = 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.Java Code:setName(nm);
- 12-23-2007, 06:35 AM #4
Try this
Another way you could do it is like thisJava 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"); } }
Java Code:abstract class Animal { private String name; Animal(String name) { this.name = name; } ... class Bird extends Animal { public Bird(String nm) { super(nm); }
- 12-23-2007, 07:50 AM #5
Member
- Join Date
- Dec 2007
- Posts
- 12
- Rep Power
- 0
the original post should work; the variable is protected; that does exactly what you want
- 12-23-2007, 02:46 PM #6
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.
Similar Threads
-
Error! "filename" is not abstract and does not override abstract method...
By hasani6leap in forum New To JavaReplies: 6Last Post: 10-27-2008, 12:25 AM -
Hiding parameters from URL
By Saurabh321 in forum New To JavaReplies: 0Last Post: 02-05-2008, 12:43 PM -
hiding inherited methods
By java_fun2007 in forum New To JavaReplies: 1Last Post: 01-05-2008, 02:16 PM -
Hiding the frame’s title bar
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 08:41 AM -
Top menu fields hiding
By youayshu in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-13-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks