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

Spud

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.

Hovercraft Full Of Eels

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

How does the subclass object access the private variable of super class,as in java no except the class itself can access the private variable?

From Dev

Can I pass object from subclass to abstract class?

From Dev

Can I pass object from subclass to abstract class?

From Dev

toString() defined on private members of abstract class,then why printing subclass object prints along with private members of abstract class?

From Dev

Can you declare an attribute private within an abstract class?

From Dev

How can I access injected Grails beans in an abstract class method?

From Dev

How could I access a private object from another class?

From Dev

How could I access a private object from another class?

From Java

Why can I change the private attribute in the class?

From Dev

How can I add an instance attribute in subclass?

From Dev

How can I access public member of private package class?

From Dev

How can I access public member of private package class?

From Dev

How can I access an object's private members?

From Dev

How can i access value of other Mappble class object.(Alamofire Object Mapper)

From Dev

How can I pass value from one subclass to another subclass in a main class C#?

From Dev

How can I access a value in an object in javascript?

From Dev

How can I create object of class whose constructor is defined as private

From Dev

How to solve private access in [subclass]

From Dev

How to mock an object attribute, I can't access?

From Dev

ModelAttribute in abstract class with value from subclass

From Dev

how to access derived class object from abstract class in boost python

From Dev

How i can design these with abstract class

From Dev

Can I call abstract base class's public assignment operator from another class except subclass?

From Dev

How can I access an object defined in another class?

From Dev

How to access specific attribute value in node object?

From Dev

How do i access attribute [field] from subclass and create it in constructor of a subclass?

From Dev

Can't access private variable from own class via subclass instance

From Dev

Android:How can I access a value from a method of a class?

Related Related

  1. 1

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

  2. 2

    How does the subclass object access the private variable of super class,as in java no except the class itself can access the private variable?

  3. 3

    Can I pass object from subclass to abstract class?

  4. 4

    Can I pass object from subclass to abstract class?

  5. 5

    toString() defined on private members of abstract class,then why printing subclass object prints along with private members of abstract class?

  6. 6

    Can you declare an attribute private within an abstract class?

  7. 7

    How can I access injected Grails beans in an abstract class method?

  8. 8

    How could I access a private object from another class?

  9. 9

    How could I access a private object from another class?

  10. 10

    Why can I change the private attribute in the class?

  11. 11

    How can I add an instance attribute in subclass?

  12. 12

    How can I access public member of private package class?

  13. 13

    How can I access public member of private package class?

  14. 14

    How can I access an object's private members?

  15. 15

    How can i access value of other Mappble class object.(Alamofire Object Mapper)

  16. 16

    How can I pass value from one subclass to another subclass in a main class C#?

  17. 17

    How can I access a value in an object in javascript?

  18. 18

    How can I create object of class whose constructor is defined as private

  19. 19

    How to solve private access in [subclass]

  20. 20

    How to mock an object attribute, I can't access?

  21. 21

    ModelAttribute in abstract class with value from subclass

  22. 22

    how to access derived class object from abstract class in boost python

  23. 23

    How i can design these with abstract class

  24. 24

    Can I call abstract base class's public assignment operator from another class except subclass?

  25. 25

    How can I access an object defined in another class?

  26. 26

    How to access specific attribute value in node object?

  27. 27

    How do i access attribute [field] from subclass and create it in constructor of a subclass?

  28. 28

    Can't access private variable from own class via subclass instance

  29. 29

    Android:How can I access a value from a method of a class?

HotTag

Archive