Results 1 to 6 of 6
- 12-22-2007, 06: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
Java 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(); }
Java Code:public class Bird extends Animal { public Bird(String nm) { name = nm; } public void speak() { System.out.println("Tweet tweet chirp"); } }
Java 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, 06:02 AM #2Java Code:
private String name;
- 12-23-2007, 06: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
Java Code:name = nm;
Java Code:setName(nm);
- 12-23-2007, 07:35 AM #4
Try this
Java 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, 08: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, 03: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, 01:25 AM -
Hiding parameters from URL
By Saurabh321 in forum New To JavaReplies: 0Last Post: 02-05-2008, 01:43 PM -
hiding inherited methods
By java_fun2007 in forum New To JavaReplies: 1Last Post: 01-05-2008, 03:16 PM -
Hiding the frame’s title bar
By Java Tip in forum Java TipReplies: 0Last Post: 12-21-2007, 09:41 AM -
Top menu fields hiding
By youayshu in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 11-13-2007, 08:10 AM
Bookmarks