简体   繁体   中英

How do I access subclass attributes passed down from superclass?

I have a subclass that extends from it's parent class and I'm having trouble understanding how I would access object attributes from the subclass that were inherited from the parent class.

In my example, I'll call the class Friend which inherits name and phone from Acquaintance but has it's own variables birthdate and address . Once that's created, I want to print the content of a Friend object which includes name , phone , birthdate , address .

Because I want my variables to all be private, I figured I would use getters, but I'm not sure how I would call the parent class in this case.

public class Acquaintance {

    private String name;
    private String phone;

    public Acquaintance(String name, String phone) {

        this.name = name;
        this.phone = phone;
    }

    public static String getName() {
        return this.name;
    }

    public String getPhone() {
        return this.phone;
    }

    public void printContact() {
        System.out.println("Name: " + this.name + ", Phone: " + this.phone);

    }

}

Subclass:

public class Friend extends Acquaintance {

    private String address;
    private String birthdate;

    public Friend(String name, String phone, String birthdate, String address) {
        super(name, phone);
        this.address = address;
        this.birthdate = birthdate;
    }

    public void printContact() {
        /* This is where I'm having trouble. 
        I don't know how to access the name, 
        and phone which are contained in the superclass.
        */


        System.out.println("Name: " + "Phone: " + "Birthdate: " + this.birthdate + "Address: " + this.address);
    }

}

Getters in your parent class Acquaintance such as getName are public so that they are visible to the sub class Friend in this case. So just merely use the getters like getName to access fields defined in the super class from within your subclass.

In your case first make the getName method an instance method by merely removing the static modifier. Then change the code like so,

public void printContact() {
    /*
     * This is where I'm having trouble. I don't know how to access the name, and
     * phone which are contained in the superclass.
     */
    getName();
}

Basically I'll call the class Friend which inherits name and phone from Acquaintance

Since name and phone are declared private , they are not inherited. A subclass only inherits protected and public fields and methods from its parent class. This means that you must call getName() in order to access this field. You can call it without an qualifier or you can call it with this.getName() . In a class getName() and this.getName() are the same thing.

A subclass inherits all public and protected members from it's super class. In your case, since getName() is public, you can normally call it in the subclasses of Acquaintance , like below:

public void printContact() {
    System.out.println("Name: " + this.getName() + "Phone: " + this.getPhone() + "Birthdate: " + this.birthdate + "Address: " + this.address);
}

Note that the use of the keyword this is optional. It merely "highlights" that it's the current object who is calling the method.

Also, when you want the children of a superclass to inherit it's members (variables, methods, etc) while still keeping them invisible to other classes, you can do as follows:

  • Create an exclusive package for the superclass and it's children;
  • If you want the members of the superclass to be accessible only by the children inside the package , declare them without an access modifier ( eg: int phone );
  • If you want the members of the superclass to be accessible by any of the children, declare them with the protected access modifier ( eg: protected int phone );.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM