Inheritance

Why Trust Techopedia

What Does Inheritance Mean?

Inheritance is a mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes.

Advertisements

A class derived from another class is called a subclass, whereas the class from which a subclass is derived is called a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses.

Techopedia Explains Inheritance

Inheritance is the process wherein characteristics are inherited from ancestors. Similarly, in Java, a subclass inherits the characteristics (properties and methods) of its superclass (ancestor). For example, a vehicle is a superclass and a car is a subclass. The car (subclass) inherits all of the vehicle’s properties. The inheritance mechanism is very useful in code reuse. The following are some limitations of Java class inheritance: A subclass cannot inherit private members of its superclass. Constructor and initializer blocks cannot be inherited by a subclass. A subclass can have only one superclass.

The keyword “extends” is used to derive a subclass from the superclass, as illustrated by the following syntax: class Name_of_subclass extends Name_of superclass { //new fields and methods that would define the subclass go here } If you want to derive a subclass Rectangle from a superclass Shapes, you can do it as follows: class Rectangle extends Shapes { …. }

Advertisements

Related Terms

Margaret Rouse
Editor

Margaret jest nagradzaną technical writerką, nauczycielką i wykładowczynią. Jest znana z tego, że potrafi w prostych słowach pzybliżyć złożone pojęcia techniczne słuchaczom ze świata biznesu. Od dwudziestu lat jej definicje pojęć z dziedziny IT są publikowane przez Que w encyklopedii terminów technologicznych, a także cytowane w artykułach ukazujących się w New York Times, w magazynie Time, USA Today, ZDNet, a także w magazynach PC i Discovery. Margaret dołączyła do zespołu Techopedii w roku 2011. Margaret lubi pomagać znaleźć wspólny język specjalistom ze świata biznesu i IT. W swojej pracy, jak sama mówi, buduje mosty między tymi dwiema domenami, w ten…