Inheritance by Aamir jamil
OBJECTIVE
Study
inheritance and using
super to call superclass constructors.
THEORY
Inheritance is one of the cornerstones of object-oriented programming
because it
allows
the creation of hierarchical classifications. using inheritance, you can create
a
general
class that defines traits common to a set of related items. this class can then
be
inherited by other, more specific classes, each adding those things that are
unique to it. in the terminology of java, a class that is inherited is called a
superclass. the class that does the inheriting is called a subclass. therefore,
a subclass is a specialized version of a superclass. it inherits all of the
instance variables and methods defined by the superclass and adds its own,
unique elements.
Inheritance Basics
To
inherit a class, you simply incorporate the definition of one class into
another by using the extends keyword. To see how, let’s begin with a
short example. The following program creates a superclass called A and a
subclass called B. Notice how the keyword extends is used to
create a subclass of A.
// A simple example of inheritance by aamir jamil
// Create a
superclass.
class A
{
int
i, j;
void
showij()
{
System.out.println("i
and j: " + i + " " + j);
}
}
// Create a
subclass by extending class A.
class B extends A
{
int
k;
void
showk()
{
System.out.println("k: " + k);
}
void
sum()
{
System.out.println("i+j+k: " +
(i+j+k));
}
}
class
SimpleInheritance
{
public
static void main(String args[])
{
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println("Contents
of superOb: ");
superOb.showij();
System.out.println();
/* The subclass has
access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println("Contents
of subOb: ");
subOb.showij();
subOb.showk();
System.out.println();
System.out.println("Sum
of i, j and k in subOb:");
subOb.sum();
}
}
Output:
Using
super
super has two general forms. The
first calls the superclass’ constructor. The second
is used to access a member of
the superclass that has been hidden by a member of a
subclass.
Using
super to Call Superclass Constructors
A subclass can call a
constructor method defined by its superclass by use of the
following form of super:
super(parameter-list);
Here, parameter-list specifies
any parameters needed by the constructor in the superclass. super( ) must
always be the first statement executed inside a subclass’ constructor.
// by aamir jamil
class Box {
private double
width;
private double
height;
private double
depth;
// construct clone
of an object
Box(Box ob) { //
pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used
when all dimensions specified
Box(double w,
double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and
return volume
double volume() {
return width *
height * depth;
}
}
// BoxWeight now
fully implements all constructors.
class BoxWeight
extends Box {
double weight;
// weight of box
// construct
clone of an object
BoxWeight(BoxWeight
ob) { // pass object to constructor
super(ob);
weight =
ob.weight;
}
// constructor
when all parameters are specified
BoxWeight(double
w, double h, double d, double m) {
super(w, h, d);
// call superclass constructor
weight = m;
}
}
class DemoSuper {
public static void
main(String args[]) {
BoxWeight mybox1 =
new BoxWeight(10, 20, 15, 34.3);
BoxWeight myclone =
new BoxWeight(mybox1);
double vol;
vol =
mybox1.volume();
System.out.println("Volume
of mybox1 is " + vol);
System.out.println("Weight
of mybox1 is " + mybox1.weight);
System.out.println();
vol = myclone.volume();
System.out.println("Volume
of myclone is " + vol);
System.out.println("Weight
of myclone is " + myclone.weight);
System.out.println();
}
}
Output:
No comments:
Post a Comment