• Sat. Oct 26th, 2024

Inheritance in Java, Part 1: The extends keyword

Byadmin

Jun 4, 2024


Java supports class reuse through inheritance and composition. This two-part tutorial teaches you how to use inheritance in your Java programs.What you’ll learn in this Java tutorialThe first half of this introduction to Java inheritance teaches you how to use the extends keyword to derive a child class from a parent class, invoke parent class constructors and methods, and override methods:
What is inheritance in Java?
Single inheritance and multiple inheritance
How to use the extends keyword in Java
Understanding Java class hierarchy
When to use method overriding vs. method overloading
download

Download the source code for example applications in this tutorial. Created by Jeff Friesen.
What is inheritance in Java?Inheritance is a programming construct that software developers use to establish is-a relationships between categories. Inheritance enables us to derive more specific categories from more generic ones. The more specific category is a kind of the more generic category. For example, a checking account is a kind of account in which you can make deposits and withdrawals. Similarly, a truck is a kind of vehicle used for hauling large items.Inheritance can descend through multiple levels, leading to ever-more-specific categories. As an example, Figure 1 shows car and truck inheriting from vehicle; station wagon inheriting from car; and garbage truck inheriting from truck. Arrows point from more specific “child” categories (lower down) to less specific “parent” categories (higher up). Jeff Friesen

Figure 1. A pair of inheritance hierarchies are rooted in the common vehicle category

Single inheritance and multiple inheritanceThe example in Figure 1 illustrates single inheritance in which a child category inherits state and behaviors from one immediate parent category. In contrast, multiple inheritance enables a child category to inherit state and behaviors from two or more immediate parent categories. The hierarchy in Figure 2 illustrates multiple inheritance. Jeff Friesen

Figure 2. Hovercraft multiply inherits from land vehicle and water vehicle categories

Categories are described by classes. Java supports single inheritance through class extension, in which one class directly inherits accessible fields and methods from another class by extending that class. Java doesn’t support multiple inheritance through class extension, however. When viewing an inheritance hierarchy, you can easily detect multiple inheritance by the presence of a diamond pattern. Figure 2 shows this pattern in the context of vehicle, land vehicle, water vehicle, and hovercraft.How to use the extends keyword in JavaJava supports class extension via the extends keyword. When present, extends specifies a parent-child relationship between two classes. Below I use extends to establish a relationship between classes Vehicle and Car, and then between Account and SavingsAccount: Listing 1. The ‘extends’ keyword specifies a parent-child relationshipclass Vehicle
{
// member declarations
}
class Car extends Vehicle
{
// inherit accessible members from Vehicle
// provide own member declarations
}
class Account
{
// member declarations
}
class SavingsAccount extends Account
{
// inherit accessible members from Account
// provide own member declarations
}The extends keyword is specified after the class name and before another class name. The class name before extends identifies the child and the class name after extends identifies the parent. It’s impossible to specify multiple class names after extends because Java doesn’t support class-based multiple inheritance.These examples codify is-a relationships: Car is a specialized Vehicle and SavingsAccount is a specialized Account. Vehicle and Account are known as base classes, parent classes, or superclasses. Car and SavingsAccount are known as derived classes, child classes, or subclasses.Child classes inherit accessible fields and methods from their parent classes and other ancestors. They never inherit constructors, however. Instead, child classes declare their own constructors. Furthermore, they can declare their own fields and methods to differentiate them from their parents. Consider Listing 2.Listing 2. An Account parent classclass Account
{
private String name;

private long amount;

Account(String name, long amount)
{
this.name = name;
setAmount(amount);
}

void deposit(long amount)
{
this.amount += amount;
}

String getName()
{
return name;
}

long getAmount()
{
return amount;
}

void setAmount(long amount)
{
this.amount = amount;
}
}Listing 2 describes a generic bank account class that has a name and an initial amount, which are both set in the constructor. Also, it lets users make deposits. (You can make withdrawals by depositing negative amounts of money but we’ll ignore this possibility.) Note that the account name must be set when an account is created. Listing 3 presents a SavingsAccount child class that extends its Account parent class.Listing 3. A SavingsAccount child class extends its Account parent classclass SavingsAccount extends Account
{
SavingsAccount(long amount)
{
super(“savings”, amount);
}
}The SavingsAccount class is trivial because it doesn’t need to declare additional fields or methods. It does, however, declare a constructor that initializes the fields in its Account superclass. Initialization happens when Account’s constructor is called via Java’s super keyword, followed by a parenthesized argument list.Listing 4 further extends Account with a CheckingAccount class.Listing 4. A CheckingAccount child class extends its Account parent classclass CheckingAccount extends Account
{
CheckingAccount(long amount)
{
super(“checking”, amount);
}

void withdraw(long amount)
{
setAmount(getAmount() – amount);
}
}CheckingAccount is a little more substantial than SavingsAccount because it declares a withdraw() method. Notice this method’s calls to setAmount() and getAmount(), which CheckingAccount inherits from Account. You cannot directly access the amount field in Account because this field is declared private (see Listing 2). Understanding Java class hierarchyI’ve created an AccountDemo application class that lets you try out the Account class hierarchy. First, take a look at AccountDemo’s source code.Listing 5. AccountDemo demonstrates the account class hierarchyclass AccountDemo
{
public static void main(String[] args)
{
SavingsAccount sa = new SavingsAccount(10000);
System.out.println(“account name: ” + sa.getName());
System.out.println(“initial amount: ” + sa.getAmount());
sa.deposit(5000);
System.out.println(“new amount after deposit: ” + sa.getAmount());

CheckingAccount ca = new CheckingAccount(20000);
System.out.println(“account name: ” + ca.getName());
System.out.println(“initial amount: ” + ca.getAmount());
ca.deposit(6000);
System.out.println(“new amount after deposit: ” + ca.getAmount());
ca.withdraw(3000);
System.out.println(“new amount after withdrawal: ” + ca.getAmount());
}
}The main() method in Listing 5 first demonstrates SavingsAccount, then CheckingAccount. Assuming Account.java, SavingsAccount.java, CheckingAccount.java, and AccountDemo.java source files are in the same directory, execute either of the following commands to compile all of these source files:javac AccountDemo.java
javac *.javaExecute the following command to run the application:java AccountDemoYou should observe the following output:account name: savings
initial amount: 10000
new amount after deposit: 15000
account name: checking
initial amount: 20000
new amount after deposit: 26000
new amount after withdrawal: 23000Method overriding vs. method overloadingA subclass can override (replace) an inherited method so that the subclass’s version of the method is called instead. An overriding method must specify the same name, parameter list, and return type as the method being overridden. To demonstrate, I’ve declared a print() method in the Vehicle class, shown in Listing 6.



Source link