0% found this document useful (0 votes)
2 views5 pages

Java Research 1

The document explains the concept of inheritance in object-oriented programming, highlighting that private class members are only accessible within their own class and not by subclasses. It provides an example of a Box class extended to include a weight attribute, illustrating how subclasses can inherit and extend the functionality of a superclass. Additionally, it emphasizes that a superclass reference can point to a subclass object, but access is limited to the superclass's defined members.

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Java Research 1

The document explains the concept of inheritance in object-oriented programming, highlighting that private class members are only accessible within their own class and not by subclasses. It provides an example of a Box class extended to include a weight attribute, illustrating how subclasses can inherit and extend the functionality of a superclass. Additionally, it emphasizes that a superclass reference can point to a subclass object, but access is limited to the superclass's defined members.

Uploaded by

Asim Dinda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

only accessible by other members of its own class.

Subclasses have no
access to it.

Remember A class member that has been declared as private will remain
private to its class. It is not accessible by any code outside its class,
including subclasses.

A More Practical Example


Let’s look at a more practical example that will help illustrate the power of
inheritance. Here, the final version of the Box class developed in the
preceding chapter will be extended to include a fourth component called
weight. Thus, the new class will contain a box’s width, height, depth, and
weight.
The output from this program is shown here:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3

Volume of mybox2 is 24.0


Weight of mybox2 is 0.076

BoxWeight inherits all of the characteristics of Box and adds to them


the weight component. It is not necessary for BoxWeight to re-create all
of the features found in Box. It can simply extend Box to meet its own
purposes.
A major advantage of inheritance is that once you have created a
superclass that defines the attributes common to a set of objects, it can be
used to create any number of more specific subclasses. Each subclass can
precisely tailor its own classification. For example, the following class
inherits Box and adds a color attribute:

Remember, once you have created a superclass that defines the general
aspects of an object, that superclass can be inherited to form specialized
classes. Each subclass simply adds its own unique attributes. This is the
essence of inheritance.

A Superclass Variable Can Reference a Subclass


Object
A reference variable of a superclass can be assigned a reference to any
subclass derived from that superclass. You will find this aspect of
inheritance quite useful in a variety of situations. For example, consider
the following:
Here, weightbox is a reference to BoxWeight objects, and plainbox is a
reference to Box objects. Since BoxWeight is a subclass of Box, it is
permissible to assign plainbox a reference to the weightbox object.
It is important to understand that it is the type of the reference variable
—not the type of the object that it refers to—that determines what
members can be accessed. That is, when a reference to a subclass object is
assigned to a superclass reference variable, you will have access only to
those parts of the object defined by the superclass. This is why plainbox
can’t access weight even when it refers to a BoxWeight object. If you
think about it, this makes sense, because the superclass has no knowledge
of what a subclass adds to it. This is why the last line of code in the
preceding fragment is commented out. It is not possible for a Box
reference to access the weight field, because Box does not define one.
Although the preceding may seem a bit esoteric, it has some important
practical applications—two of which are discussed later in this chapter.

You might also like