0% found this document useful (0 votes)
29 views

Java Inheritence

The document discusses inheritance in object-oriented programming. It uses bicycles as an example where subclasses like mountain bikes, road bikes, and tandem bikes inherit characteristics from the bicycle superclass. The syntax for creating a subclass uses the extends keyword followed by the superclass name. Subclasses inherit all fields and methods from the superclass.

Uploaded by

Suryaraju01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java Inheritence

The document discusses inheritance in object-oriented programming. It uses bicycles as an example where subclasses like mountain bikes, road bikes, and tandem bikes inherit characteristics from the bicycle superclass. The syntax for creating a subclass uses the extends keyword followed by the superclass name. Subclasses inherit all fields and methods from the superclass.

Uploaded by

Suryaraju01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

What Is Inheritance?

Different kinds of objects often have a certain amount in common with each other. Mountain bikes, road
bikes, and tandem bikes, for example, all share the characteristics of bicycles (current speed, current
pedal cadence, current gear). Yet each also defines additional features that make them different: tandem
bicycles have two seats and two sets of handlebars; road bikes have drop handlebars; some mountain
bikes have an additional chain ring, giving them a lower gear ratio.
Object-oriented programming allows classes to inherit commonly used state and behavior from other
classes. In this example, Bicycle now becomes the superclass of MountainBike,RoadBike,
and TandemBike. In the Java programming language, each class is allowed to have one direct
superclass, and each superclass has the potential for an unlimited number ofsubclasses:

A hierarchy of bicycle classes.


The syntax for creating a subclass is simple. At the beginning of your class declaration, use
the extends keyword, followed by the name of the class to inherit from:
class MountainBike extends Bicycle {
// new fields and methods defining
// a mountain bike would go here
}
This gives MountainBike all the same fields and methods as Bicycle, yet allows its code to focus
exclusively on the features that make it unique. This makes code for your subclasses easy to read.

However, you must take care to properly document the state and behavior that each superclass defines,
since that code will not appear in the source file of each subclass.
Previous Trail Next

You might also like