blog

Home / DeveloperSection / Blogs / Inheritance in Java: What is Inheritance?

Inheritance in Java: What is Inheritance?

Mark Devid1464 16-May-2016

All of us have observed inheritance in nature. For example, a baby inherits the characteristics of her parents; a child plant inherits the characteristics of its parent plant, and so on. We attempt to bring this feature into our software by inheriting the characteristics of the existing classes and then extending their functionality. To identify the classes, which have some commonality and thus are probable candidates for creating inheritance hierarchies, object-oriented analysis techniques provide some rules.

Let me illustrate inheritance with a concrete real-life example. Suppose we are asked to write some assets management software. Now, what are the classifications of assets a person may possess? A person might have a bank account, a few investments in securities, and a home in which to live. There may be many more types of assets, but we will restrict our discussion to the few mentioned here. We would like to represent these assets in our software, so let’s start creating classes to represent them. The first thing that comes to mind is an Asset class, which is a common term for all the assets previously mentioned. We represent the Asset class as shown here:


The Asset Class:

Asset

-id: int              

-type: string

+printDescription() : void




Note that this figure uses Unified Modeling Language (UML) notationto represent an Asset class. UML defines a widely accepted notation for creating artifacts of object-oriented design.

Each asset has a unique identifier represented by the id field in the class definition. Similarly, each asset has a certain type, such as bank account, security, real estate, and so on. We represent this type using the type field.

The prefix in the definition of each field indicates its visibility within our program code. 

·         The hyphen (-) prefix indicates the private visibility

·         The plus sign (+) prefix indicates a public visibility, and so on.

The variables declared with private visibility are accessible only to the methods defined in the class and are not visible to the code defined in other classes. The Asset class declares a method called printDescription().

The method returns a void and its name is prefixed with a + sign, indicating

that the method is publicly accessible. 

Next, we want to add classes to represent our real-world assets. Let’s define a class to represent a bank account. As mentioned earlier, a bank account is a type of asset and would have an id and type associated with it. Each account is held with a particular bank; this information will be captured in an attribute that represents the bank name. Let’s call this field bankName.

Each bank account will also contain a balance at any given time. We capture this by creating a field called balance. Thus, our BankAccount class will look like this:

BankAccount

-id: int

-type: string

-bankName: string

-balance: float

+printDescription() : void

 

We can easily see that the BankAccount and Asset classes have certain characteristics in common. Because the id and type attributes (fields) are common to both classes, we will create a hierarchy such that the BankAccount class inherits these fields from the Asset class. This is represented in the UML notation shown here:

Inheritance in Java: What is Inheritance?

We call this operation of extending the class functionality subclassing. We say that the BankAccount is a “subclass” of the Asset class. Alternatively, we say that BankAccount “extends” the Asset class. Another way of putting it is that BankAccount is “derived from” the Asset class. 

The class from which a subclass is created is called a superclass. Thus, Asset is a superclass of BankAccount. Alternatively, we say that Asset is a “base class” of BankAccount. 

In Java, sub-classing is achieved with the help of the extends keyword. UML notation has been used thus far to explain class inheritance. Now, you will see the class declarations for these classes

Super Class: Asset
class Asset
{
            private int id;
            private String type;
            public int getId() {
                        return id;
            }
            public void setId(int id) {
                        this.id = id;
            }
            public String getType() {
                        return type;
            }
            public void setType(String type) {
                        this.type = type;
            }
            public void printDescription() {
                        System.out.println("Asset ID: " + id);
                        System.out.println("Asset type: " + type);
            }
}

 

Sub-class: BankAccount
class BankAccount extends Asset {
            private String bankName;
            private int accountNumber;
            private float balance;
            public int getAccountNumber() {
                        return accountNumber;
            }
            public void setAccountNumber(int accountNumber) {
                        this.accountNumber = accountNumber;
            }
            public float getBalance() {
                        return balance;
            }
            public void setBalance(float balance) {
                        this.balance = balance;
            }
            public String getBankName() {
                        return bankName;
            }
            public void setBankName(String bankName) {
                        this.bankName = bankName;
            }
            public void printDescription() {
                        super.printDescription();
                        System.out.println("Name: " + bankName);
                        System.out.printf("Account #: %d%n", accountNumber);
                        System.out.printf("Current balance: $%.02f%n", balance);             }
}

 

Note that the BankAccount class definition does not contain any of the fields defined in the Asset class. These characteristics (fields and methods) are automatically made available to an object of BankAccount type. Note that the id and type fields of the Asset class are declared private. As stated earlier, private fields are not visible to the code outside the class definition. Therefore, to access these private fields, we create getter/setter methods for each field. These methods are declared public and can be called from the code outside the current class definition.


Updated 14-Mar-2018

Leave Comment

Comments

Liked By