What keyword is used to access the base class method from the child class?
A beginner's library for learning about essential Java programming concepts, syntax, APIs, and packages. Show
Use Java's extends keyword to derive a child class from a parent class, invoke parent class constructors and methods, override methods, and moreMarco Verch (CC BY 2.0) Table of Contents Show More Java supports class reuse through inheritance and composition. This two-part tutorial teaches you how to use inheritance in your Java programs. In Part 1 you'll learn how to use the 4 keyword to derive a child class from a parent class, invoke parent class constructors and methods, and override methods. In Part 2 you'll tour 5, which is Java's superclass from which every other class inherits.To complete your learning about inheritance, be sure to check out my Java tip explaining when to use composition vs inheritance. You'll learn why composition is an important complement to inheritance, and how to use it to guard against issues with encapsulation in your Java programs. download Get the code Download the source code for example applications in this tutorial. Created by Jeff Friesen for JavaWorld. Java inheritance: Two examplesInheritance 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 FriesenFigure 1. A pair of inheritance hierarchies are rooted in the common vehicle category This example 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 FriesenFigure 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. The extends keywordJava supports class extension via the 4 keyword. When present, 4 specifies a parent-child relationship between two classes. Below I use 4 to establish a relationship between classes 9 and 0, and then between 1 and 2:Listing 1. The class 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;
}
} |