1

Animal Base Class

public class Animal
{

protected String pig;
protected String dog;
protected String cat;

public void setPig(String pig_)
{
  pig=pig_;
}
public void setCat(String cat_)
{
  cat=cat_;
}
public void setDog(String dog_)
{
  dog=dog_;
}

}

AnimalAction Class

public class AnimalAction extends Animal
{
  public AnimalAction(String pig, String cat, String dog)
  {
       super.pig = pig;
       super.cat = cat;
       super.dog = dog;
  }

}

Would this be the correct way to set protected variables? Is using protected variables the correct way to do this? Is there a more professional OO way to do?

4
  • 1
    Why do you have setters but not use them? Commented Jun 11, 2012 at 14:55
  • 1
    Please capitalize your classes. It is the standard in Java. Commented Jun 11, 2012 at 14:56
  • @Poindexter using non-final setters in a constructor may have ugly effects if a subclass overrides them. The question should rather be - "why are your setters not final?" ;) Commented Jun 11, 2012 at 14:57
  • @kostja this is the type of discussion I'm looking for. I need someone to point me the correct approach to this Commented Jun 11, 2012 at 15:00

6 Answers 6

2

You can use private variables instead of protected. This will be more apt. You can use the constructor to set the value of the super class. Edited:

public class Animal{

    private String pig;
    private String dog;
    private String cat;

    public Animal(String pig,String dog,String cat){
       this.pig=pig;
       this.dog=dog;
       this.cat=cat;
    }

}


public class AnimalAction extends Animal
{ 
  public AnimalAction(String pig, String cat, String dog)
  {
       super(pig,dog,cat);
  } 

}
Sign up to request clarification or add additional context in comments.

Comments

0

You should be able to use this.pig etc, since you inherited the protected members. You could also actually call the public setPig(...) methods.

2 Comments

Thanks Thomas, does this overall approach look correct though?
It will work, but isn't a good way to do it. There are several more correct ways of doing it in the other answers here.
0

There is nothing wrong in using protected member variable and then inherit them in subclass .

But If a developer comes along and subclasses your class they may mess it up because they don't understand it fully. With private members, other than the public interface, they can't see the implementation specific details of how things are being done which gives you the flexibility of changing it later. By providing protected member variables you are just coupling tight between you subclass and superclass.

Comments

0

The less your member variables can be seen outside the class, the better. I would make the class variables private and make the getters public (or as required) & the setters protected.

Comments

0

There's no need to use the super prefix, or any other prefix, to access protected variables.

BTW - I disagree with Thomas on one point - do not call the setter methods of the superclass in your constructor. Using non-final setters in a constructor may have ugly effects if a subclass overrides them. Then they could be called on an incompletely constructed object. But you should consider making your setters final if you don't mean them to be overridden.

The principle of "design for inheritance or forbid it" is explained in the Effective Java book by Joshua Bloch.

Comments

0

Your example is quite confusing, but it would work. I'll give another example:

// use capitals for classes/interfaces/enums, lower case for methods/fields.

public class Animal 
{
protected String name;
protected int numberOfFeet;

public Animal(String name)
{
    this.name = name;
}

public void setNumberOfFeet(int numberOfFeet)
{
    this.numberOfFeet = numberOfFeet;
}
}

public class Dog extends Animal
{
    public Dog()
    {
        super("dog"); // call the constructor of the super class.

        // because Dog extends Animal and numberOfFeet is protected, numberOfFeet becomes part of "this" class.
        this.numberOfFeet = 4;
    }
}

//Now you can create instances of Animal like:
Animal bird = new Animal("bird");
bird.setNumberOfFeet(2);
//Or use Dog to create an animal "dog" with 4 feet.
Animal dog = new Dog();

//after an accident
dog.setNumberOfFeet(3);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.