Results 1 to 6 of 6
  1. #1
    Ace_Of_John is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default 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();
    }
    //The subclass
    Java 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
    Java 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

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    private String name;

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Quote Originally Posted by hardwired View Post
    Java 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
    Java Code:
    name = nm;
    it would have to be
    Java 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.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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");
        }
    }
    Another way you could do it is like this
    Java Code:
    abstract class Animal
    {
        private String name;
    
        Animal(String name)
        {
            this.name = name;
        }
        ...
    
    class Bird extends Animal
    {
        public Bird(String nm)
        {
            super(nm);
        }

  5. #5
    spoon! is offline Member
    Join Date
    Dec 2007
    Posts
    12
    Rep Power
    0

    Default

    the original post should work; the variable is protected; that does exactly what you want

  6. #6
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    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.

Similar Threads

  1. Replies: 6
    Last Post: 10-27-2008, 01:25 AM
  2. Hiding parameters from URL
    By Saurabh321 in forum New To Java
    Replies: 0
    Last Post: 02-05-2008, 01:43 PM
  3. hiding inherited methods
    By java_fun2007 in forum New To Java
    Replies: 1
    Last Post: 01-05-2008, 03:16 PM
  4. Hiding the frameís title bar
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 09:41 AM
  5. Top menu fields hiding
    By youayshu in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-13-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •