In the preceding lessons, you have seen inheritance discussed several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.

You are watching: A protected member of a class may be directly accessed by:

Definitions:A class that is derived from another class is referred to as a subclass (also a obtained class, extended class, or child class). The class from i beg your pardon the subclass is acquired is referred to as a superclass (also a base class or a parent class).Excepting Object, which has actually no superclass, every class has one and also only one straight superclass (single inheritance). In the lack of any kind of other explicit superclass, every course is implicitly a subclass of Object.Classes have the right to be acquired from classes the are derived from classes the are obtained from classes, and so on, and also ultimately obtained from the topmost class, Object. Together a course is said to be descended from all the class in the inheritance chain stretching earlier to Object.

The idea that inheritance is straightforward but powerful: as soon as you want to produce a new class and also there is already a course that contains some that the code that girlfriend want, you can derive your brand-new class indigenous the existing class. In doing this, you have the right to reuse the fields and also methods that the existing course without having to write (and debug!) castle yourself.

A subclass inherits every the members (fields, methods, and also nested classes) native its superclass. Constructors space not members, so they space not inherited by subclasses, however the constructor the the superclass deserve to be invoked from the subclass.

The Java Platform course Hierarchy

The thing class, identified in the java.lang package, defines and also implements habits common to all classes—including the ones the you write. In the Java platform, plenty of classes derive directly from Object, other classes derive from some of those classes, and also so on, creating a hierarchy of classes.


All class in the Java Platform space Descendants that Object

At the top of the hierarchy, thing is the most basic of every classes. Classes close to the bottom of the hierarchy provide more dedicated behavior.

An instance of Inheritance

Here is the sample code for a possible implementation that a Bicycle class that was presented in the Classes and also Objects lesson:

public class Bicycle // the Bicycle class has three fields publicly int cadence; windy int gear; public int speed; // the Bicycle course has one constructor publicly Bicycle(int startCadence, int startSpeed, int startGear) equipment = startGear; cadence = startCadence; speed = startSpeed; // the Bicycle class has four methods public void setCadence(int newValue) cadence = newValue; windy void setGear(int newValue) gear = newValue; public void applyBrake(int decrement) speed -= decrement; windy void speedUp(int increment) rate += increment;
A course declaration for a MountainBike course that is a subclass that Bicycle can look favor this:

public course MountainBike expand Bicycle // the MountainBike subclass adds one field publicly int seatHeight; // the MountainBike subclass has actually one constructor publicly MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) super(startCadence, startSpeed, startGear); seatHeight = startHeight; // the MountainBike subclass adds one method publicly void setHeight(int newValue) seatHeight = newValue;
MountainBike inherits every the fields and methods that Bicycle and adds the field seatHeight and a technique to collection it. Except for the constructor, that is together if you had written a new MountainBike class entirely from scratch, with 4 fields and five methods. However, you didn't need to do all the work. This would be especially an useful if the approaches in the Bicycle course were complicated and had taken substantial time to debug.

What You deserve to Do in a Subclass

A subclass inherits every one of the public and also protected members the its parent, no issue what package the subclass is in. If the subclass is in the same package together its parent, it also inherits the package-private members that the parent. You have the right to use the inherited members together is, change them, hide them, or complement them with new members:

The inherited fields can be supplied directly, just like any kind of other fields.You can declare a ar in the subclass through the very same name together the one in the superclass, for this reason hiding the (not recommended).You deserve to declare brand-new fields in the subclass that space not in the superclass.The inherited methods have the right to be used directly as castle are.You can write a new instance technique in the subclass that has the same signature together the one in the superclass, thus overriding it.You have the right to write a new static an approach in the subclass that has the exact same signature as the one in the superclass, hence hiding it.You have the right to declare new methods in the subclass that are not in the superclass.You deserve to write a subclass constructor that invokes the constructor of the superclass, one of two people implicitly or by making use of the keyword super.

The adhering to sections in this lessonwill broaden on these topics.

Private Members in a Superclass

A subclass does not inherit the personal members that its parental class. However, if the superclass has public or safeguarded methods because that accessing its exclusive fields, these can additionally be used by the subclass.

A nested course has accessibility to every the personal members the its enclosing class—both fields and also methods. Therefore, a public or protected nested course inherited through a subclass has indirect accessibility to every one of the personal members that the superclass.

Casting Objects

We have seen that an object is that the data type of the class from i m sorry it was instantiated. Because that example, if we write

then myBike is of form MountainBike.

MountainBike is descended from Bicycle and also Object. Therefore, a MountainBike is a Bicycle and is likewise an Object, and it deserve to be used wherever bike or thing objects are called for.

The turning back is no necessarily true: a Bicycle may be a MountainBike, yet it isn't necessarily. Similarly, an object may it is in a bike or a MountainBike, yet it isn't necessarily.

Casting reflects the use of an item of one type in place of one more type, amongst the objects permitted by inheritance and also implementations. For example, if we write

then obj is both an item and a MountainBike (until such time together obj is assigned one more object the is no a MountainBike). This is dubbed implicit casting.

If, on the other hand, we write

we would obtain a compile-time error due to the fact that obj is not well-known to the compiler to be a MountainBike. However, we deserve to tell the compiler that us promise to assign a MountainBike come obj by explicit casting:

This cast inserts a runtime check that obj is assigned a MountainBike so that the compiler can safely assume the obj is a MountainBike. If obj is no a MountainBike in ~ runtime, an exception will be thrown.

See more: The Electric Resistance In A Length Of Wire Is Doubled When The Wire Is _________.

Note:You deserve to make a logical test as to the kind of a details object utilizing the instanceof operator. This deserve to save girlfriend from a runtime error fan to an not correct cast. Because that example:
Here the instanceof operator verifies that obj describes a MountainBike so the we have the right to make the cast with expertise that there will be no runtime exemption thrown.