Lets say we have:
1) Superclass containing a string parameter: companyName 2) Subclasses containing string parameters: firstName, lastName
If we have subClassA, subClassB, subClassC, subClassD, etc. Can these subclasses have same superclass, but with different strings companyName, or the companyName meaning/value will be the same for every subclass no matter what?
Every instance of the parent class can have a different value for companyName
whether it's of a subtype or not.
public class Parent {
private final String companyName;
public Parent(String name) {
this.companyName = name; // ignoring error-checking here
}
public String getCompanyName() {
return companyName;
}
}
public class Subsidiary extends Parent {
private final String subsidiaryName;
public Subsidiary(String parentName, String subsidiaryName) {
super(parentName);
this.subsidiaryName = subsidiaryName;
}
public String getSubsidiaryName() {
return subsidiaryName;
}
}
In some client code you can call:
Subsidiary subsidiary = new Subsidiary("Holdings, Inc.", "Puppet");
System.out.println(subsidiary.getCompanyName() + " owns "
+ subsidiary.getSubsidiaryName());
You see, the child-type object inherits the accessor method getCompanyName
that gives it access to the information in the parent section of the object.