Q

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.


This was first published in September 2002

Dig deeper on Java Web Services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

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:

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close