Spud
Spud

Reputation: 371

How can I access the value of a private attribute in an abstract class in a subclass object?

I am taking a class in Java and this question relates to one of the exercises I am to complete. I am trying to print out the contents of an array of objects created from 2 subclasses of an abstract superclass. I am able to create the objects and store them in an array, but when I print out the contents of the array I'm only able to get the last instance of the "age" and "weight" attributes of the superclass. As you can see they are private attributes. Is there a way to access the value of those attributes when the object is created? I've done a fair bit of reading and I'm confused as to whether I can do it and if I can, then how? My code:

public abstract class Parent {
    private static int age;
    private static double weight;
    public Animal(int age, double weight) {
        this.age = age;
        this.weight = weight;
        }
    public static int getAge() {
        return age;
        }
    public static double getWeight() {
        return weight;
        }
    }

public class Child1 extends Parent {
    private String name, owner, petInfo;
    protected int age;
    protected double weight;
    public Child1(int age, double weight, String name, String owner) {
        super(age, weight);
        this.name = name;
        this.owner = owner;
        }
    public String toString() {
        petInfo = "Pet's name: " + this.getName() + "\nPet's age: " + getAge() + " years\nPet's weight: " + getWeight() + " kilos\nOwner's name: " + this.getOwner();
        return petInfo;
        }
    }

public class Child2 extends Parent {
    public String wildInfo;
    public Child2(int age, double weight) {
        super(age, weight);
        }
    public String toString() {
        wildInfo = "The wild animal's age: " + getAge() + "\nThe wild animal's weight: " + getWeight();
        return wildInfo;
        }
    }

public class Console {
    public static void main(String[] args) {
        Parent ref[] = new Parent[5];
        for(i = 0; i < 5; i++) {
        //user input here
        Child1 pet = new Child1(age, weight, name, owner);
        ref[i] = pet;
        //more user input
        Child2 wild = new Child2(age, weight);
        ref[i] = wild;
        }
        //print contents of array
        for(Parent item : ref)
            System.out.println("\n" +item.toString()+ "\n");

My understanding is that I can only access the attributes of the superclass through the methods. When I use the getAge() and getWeight() methods in the toString() I am not getting the values entered for each object, only the last value the attributes had. Any help would be greatly appreciated. Cheers.

Upvotes: 2

Views: 1999

Answers (2)

Raghu
Raghu

Reputation: 1393

There were errors in the above program.

Corrected the static methods in Parent Class into non-static methods.

Sample Code:

import java.util.Scanner;

    abstract class Parent {
        private int age;
        private double weight;
        public Parent(int age, double weight) {
            this.age = age;
            this.weight = weight;
            }
        public int getAge() {
            return age;
            }
        public double getWeight() {
            return weight;
            }
    }

    class Child1 extends Parent {
        private String name, owner, petInfo;
        public Child1(int age, double weight, String name, String owner) {
            super(age, weight);
            this.name = name;
            this.owner = owner;
            }
        public String toString() {
            petInfo = "Pet's name: " + this.getName() + "\nPet's age: " + getAge() + " years\nPet's weight: " + getWeight() + " kilos\nOwner's name: " + this.getOwner();
            return petInfo;
            }
        public String getName() {
            return name;
        }
        public String getOwner() {
            return owner;
        }
    }

    class Child2 extends Parent {
        public String wildInfo;
        public Child2(int age, double weight) {
            super(age, weight);
            }
        public String toString() {
            wildInfo = "The wild animal's age: " + getAge() + "\nThe wild animal's weight: " + getWeight();
            return wildInfo;
            }
        }

    public class Console {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            Parent ref[] = new Parent[2];
            //int weight=10;
            //int age=5;
            //String name="parrot";
            //String owner="rajesh";
            for(int i = 0; i < 2; i++) {
            System.out.println("Enter the name");
            String name=in.next();
            System.out.println("Enter the age ");
            int age=in.nextInt();
            System.out.println("Enter the weight");
            double weight=in.nextDouble();
            System.out.println("Enter the owner");
            String owner=in.next();
            //user input here
            Child1 pet = new Child1(age, weight, name, owner);
            ref[i] = pet;
            //more user input
            if (i==1)
            {
                break;
            }
            System.out.println("Enter the name");
            name=in.next();
            System.out.println("Enter the age ");
            age=in.nextInt();
            System.out.println("Enter the weight");
            weight=in.nextDouble();
            System.out.println("Enter the owener");
            owner=in.next();
            Child2 wild = new Child2(age, weight);
            ref[++i] = wild;
            }
            //print contents of array
            for(Parent item : ref)
                System.out.println("\n" +item.toString()+ "\n");
        }
    }

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Don't use static variables for age and weight:

private static int age;
private static double weight;

The values of static variables are the same for all objects of this type since these variables are be class variables, not instance variables. These guys should be instance or non-static fields which will make them unique for each instance of this class (or instances of child classes).

Then in your Child class, get rid of these shadowing variables, since they will shadow the similarly named field in the Parent class:

public class Child1 extends Parent {
    private String name, owner, petInfo;
    protected int age;            // ***** get rid of, since it shadows
    protected double weight;      // ***** get rid of, since it shadows

Instead, wherever you use these, use the getters and setters in the Child class.

Upvotes: 1

Related Questions