Problem solve Get help with specific problems with your technologies, process and projects.

What is inheritance in Java?

What is inheritance in Java?

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.

Dig Deeper on Topics Archive

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.