Reputation: 505
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);
}
}
Upvotes: 1
Views: 7180
Reputation: 2097
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:
int phone
);protected
access modifier (e.g.: protected int phone
);.Upvotes: 0
Reputation: 83577
Basically I'll call the class
Friend
which inheritsname
andphone
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.
Upvotes: 0
Reputation: 21124
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();
}
Upvotes: 1