Ask the Expert

What is inheritance in Java?

What is inheritance in Java?

    Requires Free Membership to View

Inheritance describes the ability of one class or object to possess characteristics and functionality of another class or object. All object-oriented languages should, and typically do, provide some kind of inheritance capabilities.

An object-oriented language can support inheritance as "run-time" inheritance or "compile-time" inheritance or both. Run-time inheritance refers to the ability of an instance of a class to "inherit" the capabilities of an instance of another class dynamically at run-time. Compile-time inheritance refers to the ability of a "child class" or "sub-class" to inherit some or all of the characteristics of another class during compile-time. When discussing inheritance with the Java programming language, one is generally referring to compile-time inheritance.

Some object-oriented languages support a mechanism known as "multiple inheritance." Multiple inheritance refers to the ability of one class to inherit the characteristics and/or functionality of more than one parent or "super" class. Java supports only single inheritance, i.e. a class can only inherit from one parent class. However, Java does support a powerful feature embodied in the ability of one class to support multiple interfaces. This refers to the ability of one class to be viewed as more than one interface depending upon how it is referenced by other classes.

In order for a Java class to inherit from another class, it must declare the parent class as part of its definition as follows:

public class MySubClass extends MyParentClass

In this example, MySubClass declares that it inherits or "extends" MyParentClass. MyParentClass can control what characteristics of functionality it allows child classes to inherit using the "scope modifiers": private, protected and public.

This was first published in September 2002

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: