You want to create a class that inherits from an existing class.
Write a subclass using the
There are cases when a new class is a more specific version of an existing class. The new class may feature much of the same behavior as the existing class. Rather than rewriting all the common functionality you can define the new class so it inherits all the functionality of the existing class. In relation to one another, the new class is then called a subclass and the existing class is called a superclass.
You can define inheritance between classes in the subclass declaration
extends keyword, as
A subclass can reference any
protected properties and methods of the
private properties and
methods are not accessible outside the class, not even to a
Inheritance is a powerful technique; however, as with anything else, it is important that you use inheritance correctly. Before writing a subclass you need to determine whether or not the new class actually has a subclass relationship with the existing class. There are two basic types of relationships that classes can have: inheritance and composition. You can usually quickly determine the correct relationship between classes by asking whether it’s an “is a” relationship or a “has a” relationship:
“Is a” relationships are often inheritance relationships. As an example, consider an application that ...