Java Concepts‎ > ‎


Inheritance is a feature allowing new classes to be created from existing classes. The new class (subclass) inherits all fields and methods from the class (superclass) it extends. Therefore, inheritance helps to reuse the existing code -- only the more specialized (extended) fields and methods should be defined in the subclass.

For example, lets look at the two classes used in the Real Estate Business program. The superclass Property has 3 fields, 1 constructor, and 1 method named toString(), which overrides a corresponding toString()method of the Object class.
Note: The Object class is a superclass of all superclases and subclasses -- every class can be seen as a direct or indirect extension of the Object class.
Note: The private fields of the superclass can be accessed only by superclass methods.

public class Property
    // fields
    private String streetAddress;
    private double priceAsking;
    private int sqFootage;

    // constructor
    public Property(String streetAddress, double priceAsking, int sqFootage) {
        this.streetAddress = streetAddress;
        this.priceAsking = priceAsking;
        this.sqFootage = sqFootage;
    // method(s)
    public String toString() {
        return "Property{" + "streetAddress=" + streetAddress
                        + "priceAsking=" + priceAsking + "sqFootage=" + sqFootage + '}';


The second class is the subclass Commercial, which has 2 additional fields, 1 constructor, and a method toString(), that overrides the toString() methods of the Property class as well as of the Object class.
Note: If a subclass method has the same signature (the same name and same parameter types) as the superclass method, the overriding (subclass) method is executed but the original (superclass) method is ignored by compiler. Note: All superclass methods (if not overridden) are automatically inherited by the subclass.
Note: To access private fields of the superclass, we must use the super.method(), because only the superclass methods can access its fields. Subclass methods do not have access to the private fields of the superclass.
Note: The compiler processes the superclass constructor first, then executes the subclass constructor. Therefore, the superclass constructor must be called from the first line of the subclass constructor (using the super keyword).

public class Commercial extends Property
    // only extended fields are defined in the declarations section of the subclass
    private int stories;
    private String type;

    // constructor: all subclass fields are defined as parameters
    public Commercial(String addr, double price, int sqFt, int stories, String type)
        super(addr, price, sqFt);    // all other fields are sent to superclass constructor
        this.stories = stories;
        this.type = type;

    // method(s)
    public String toString() {
        return "Property (Commercial): \n"
            + String.format("%24s %-40s %n", "Address: ", super.getStreetAddress())
            + String.format("%24s %,#.2f %n", "Price: ", super.getPriceAsking())
            + String.format("%24s %d %n", "Sq.Ft.: ", super.getSqFootage())
            + String.format("%24s %d %n", "Stories: ", stories)
            + String.format("%24s %-40s %n", "Type: ", type);
Linas Vaicekavicius,
Oct 12, 2010, 4:19 AM