What is inheritance in Java?

What is inheritance in Java?

What is inheritance in Java?

    Requires Free Membership to View

    When you register, you'll begin receiving targeted emails from my team of award-winning writers. Our goal is to keep you informed on recent service-oriented architecture (SOA) and SOA-related topics such as integration, governance, Web services, Cloud and more.

    Hannah Smalltree, Editorial Director

    By submitting your registration information to SearchSOA.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchSOA.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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