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 last published in September 2002

Dig Deeper on Java Web Services

PRO+

Content

Find more PRO+ content and other member only offers, here.

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.

Join the conversation

1 comment

Send me notifications when other members comment.

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

Please create a username to comment.

Hi,

The concept of child and parent class in Compile - time inheritance is very much clear and understandable.

Please help out with an example of source code for determining the implementation of Run - time inheritance.

Reply awaited.

Thanks in advance
Ashish@S
Cancel

-ADS BY GOOGLE

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close