Creating a Derived Class

Contact Us or call 1-877-932-8228
Creating a Derived Class

Creating a Derived Class

The syntax for extending a base class to create a new class is:

[modifiers] class DerivedClassName extends BaseClassName {
	(new or revised field and method definitions go here)

If you do not extend any class, Java assumes you are extending Object by default

Your new class can use the fields and methods contained in the original class (subject to the note coming up in a few pages about access keywords), add new data fields and methods, or replace fields or methods.

A derived class object may be stored in a base class reference variable without any special treatment. If you then want to store that object in a derived class reference again, you can force that with a typecast.

Java doesn't allow multiple inheritance, where one class inherits from two or more classes. Note that:

  • It does have a concept called an interface, which defines a set of method names.
  • A class may implement an interface, defining those methods in addition to whatever other methods are in the class.
  • This allows for several otherwise unrelated classes to have the same set of method names available, and to be treated as the same type of object for that limited set of methods.