Hi,

I was having problems with the pet store java code.
The class pet has private attributes, but i cannot use the extender class.
Can help me?

import java.util.*;

public class Pet
{
    // Private fields
    private String species; // Species name
    private int ageInWeeks; // Pet's age in weeks

    // Constructor method
    public Pet(String species, int ageInWeeks)
    {
        this.species = species;
        this.ageInWeeks = ageInWeeks;
    }

    // Accessor (getter) methods
    public String getSpecies()
    {
        return species;
    }

    public int getAgeInWeeks()
    {
        return ageInWeeks;
    }

    // Mutator (setter) methods
    public void setSpecies (String species)
    {
        this.species = species;
    }

    public void setAgeInWeeks (int ageInWeeks)
    {
        this.ageInWeeks = ageInWeeks;
    }
}

class Chihuahua extends Pet
{
    Chihuahua()
    {
        species = "Chihuahua";
        ageInWeeks = 4;
    }
}

class Poodle extends Pet
{
    Poodle()
    {
        species = "Poodle";
        ageInWeeks = 5;
    }
}

class Dashshund extends Pet
{
    Dashshund()
    {
        species = "Dashshund";
        ageInWeeks = 3;
    }
}

public class PetStore 
{
    public static void main(String[] args) 
    {
        Scanner scan = new Scanner (System.in);
        int numSelect;

        Chihuahua chihuahua = new Chihuahua();
        Poodle poodle = new Poodle();
        Dashshund dashshund = new Dashshund();

        do
        {
            do
            {
                System.out.println("\nPick your pet:"
                + "\n1. Chihuahua"
                + "\n2. Poodle"
                + "\n3. Dashshund"
                + "\n4. Exit");
                System.out.print("Select (1 - 4): ");
                numSelect = scan.nextInt(); 
            } while (numSelect < 1 || numSelect > 4);

            switch (numSelect)
            {
                case 1:
                    pricing (numSelect, chihuahua, poodle, dashshund);
                    break;

                case 2:
                    pricing (numSelect, chihuahua, poodle, dashshund);
                    break;
                case 3:
                    pricing (numSelect, chihuahua, poodle, dashshund);
                    break;
            }    
        } while (numSelect != 4);

    }
    public static void pricing (int numSelect, Chihuahua chihuahua, Poodle poodle, Dashshund dashshund)
    {
        String speciesName;
        int SpeciesAgeInWeeks;
        double price;

        if (numSelect == 1) // Chihuahua
        {
            speciesName = chihuahua.getSpecies();
            SpeciesAgeInWeeks = chihuahua.getAgeInWeeks();
            price = 250 * SpeciesAgeInWeeks;

            System.out.println("Species name: " + speciesName);
            System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
            System.out.println("Price per every week old: 250");
            System.out.println("Pet price: " + price);
        }
        else if (numSelect == 2) // Poodle
        {
            speciesName = poodle.getSpecies();
            SpeciesAgeInWeeks = poodle.getAgeInWeeks();
            price = 300 * SpeciesAgeInWeeks;

            System.out.println("Species name: " + speciesName);
            System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
            System.out.println("Price per every week old: 300");
            System.out.println("Pet price: " + price);

        }
        else if (numSelect == 3) // Dashshund
        {
            speciesName = dashshund.getSpecies();
            SpeciesAgeInWeeks = dashshund.getAgeInWeeks();
            price = 200 * SpeciesAgeInWeeks;

            System.out.println("Species name: " + speciesName);
            System.out.println("Age in weeks: " + SpeciesAgeInWeeks);
            System.out.println("Price per every week old: 200");
            System.out.println("Pet price: " + price);

        }
    }

}

What do you mean "cannot use"? What exactly is your question?

There is an error of the private attributes in the extender

and what do you know about private?
Private members can ONLY be accessed from within the class they're defined in. You're trying to access them from another class.
Either redefine them as protected OR use the getter and setter methods to access them.

Which of the two is the prefered solution depends on context and personal preference, in this case I'd go with protected data members.

"There is an error" is a useless answer. What error exactly, in what code exactly?

Anyway... jwenting is right about private variables, but I totally disagree with him about how to fix it.
Making them protected means that your subclasses can use or modify those variables however and whenever they like. (It also exposes those variables to any other class in the same package, which is far too wide an accessibility.)
This is the 'fragile base class' anti-pattern, where the base class his subsequent internal modifications, and you get bugs because the modifications are incompatible with what the subclasses do with the variables.
The only safe approach is for subclasses to use ONLY the public members of the base class, in this case that's the accessor methods.
In real life there will be times when this creates problems, and protected variables may be a less-bad solution, but always start with the public members.
(The earliest drafts of the Java language considerd a "private protected" modifier that exposed members to subclasses but nowhere else. I think it was a terrible mistake that they didn't keep it in.)

hehe, I'm so used to writing things in tightly controlled environments that are performance critical that I'm pretty well used to using protected data members in abstract base classes (usually in JPA @MappedSuperclass).

Well yes. That makes total sense when you are designing and maintaining the while hierarchy. That's why I think Java needed a visibility for members to subclasses only.
IMHO the fragile base class problem only bites your bum when one person does the base class and other people come along later and create their own subclasses.

Yes, I'd never use it in public APIs.
For those I'd make as much final or otherwise unoverridable as I can get away with to prevent tinkering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.