Basics of Java Programming
Unit-IV
22PLC25C
[Link]
Inheritance Basics
• To inherit a class, you simply incorporate the definition of one class into another by
using the extends keyword.
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
[Link]("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
[Link]("k: " + k);
}
void sum() {
[Link]("i+j+k: " + (i+j+k));
}
}
[Link]
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;
[Link]("Contents of superOb: ");
[Link]();
[Link]();
/* The subclass has access to all public members of
its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
[Link]("Contents of subOb: ");
[Link]();
[Link]();
[Link]();
[Link]("Sum of i, j and k in subOb:");
[Link]();
}
}
[Link]
The output from this program is shown here:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
[Link]
Member Access and Inheritance
• Although a subclass includes all of the
members of its superclass, it cannot access
those members of the superclass that have
been declared as private.
[Link]
/* In a class hierarchy, private members remain private to
their class.
This program contains an error and will not compile.
*/
// Create a superclass.
class A {
int i; // public by default
private int j; // private to A
void setij(int x, int y) {
i = x;
j = y;
}
}
[Link]
// A's j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
[Link](10, 12);
[Link]();
[Link]("Total is " + [Link]);
}
}
[Link]
// This program uses inheritance to extend Box.
class Box {
double width;
double height;
double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = [Link];
height = [Link];
depth = [Link];
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
[Link]
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
} [Link]
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.
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box();
double vol;
vol = [Link]();
[Link]("Volume of weightbox is " + vol);
[Link]("Weight of weightbox is " +
[Link]);
[Link]();
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = [Link](); // OK, volume() defined in Box
[Link]("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox
does not define a weight member. */
// [Link]("Weight of plainbox is " + [Link]);
}
}
[Link]
Using super
• Whenever a subclass needs to refer to its
immediate superclass, it can do so by use of
the keyword 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.
[Link]
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);
[Link]
// BoxWeight now uses super to initialize its Box
attributes.
class BoxWeight extends Box {
double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double
m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}
[Link]
A Second Use for super
• The second form of super acts somewhat like
this, except that it always refers to the
superclass of the subclass in which it is used.
[Link]
• Here, member can be either a method or an
instance variable.
[Link]
// Using super to overcome name hiding.
class A {
int i;
}
// Create a subclass by extending class A.
class B extends A {
int i; // this i hides the i in A
B(int a, int b) {
super.i = a; // i in A
i = b; // i in B
}
void show() {
[Link]("i in superclass: " + super.i);
[Link]("i in subclass: " + i);
}
}
class UseSuper {
public static void main(String args[]) {
B subOb = new B(1, 2);
[Link]();
}
}
[Link]
Creating a Multilevel Hierarchy
class Shipment extends BoxWeight {
double cost;
// construct clone of an object
Shipment(Shipment ob) { // pass object to constructor
super(ob);
cost = [Link];
}
// constructor when all parameters are specified
Shipment(double w, double h, double d,
double m, double c) {
super(w, h, d, m); // call superclass constructor
cost = c;
}
// default constructor
Shipment() {
super();
cost = -1;
} [Link]
// constructor used when cube is created
Shipment(double len, double m, double c) {
super(len, m);
cost = c;
}
}
class DemoShipment {
public static void main(String args[]) {
Shipment shipment1 = new Shipment(10, 20, 15, 10,
3.41);
double vol;
vol = [Link]();
}
} [Link]
When Constructors Are Called
// Demonstrate when constructors are called.
// Create a super class.
class A {
A() {
[Link]("Inside A's constructor.");
}
}
// Create a subclass by extending class A.
class B extends A {
B() {
[Link]("Inside B's constructor.");
}
}
// Create another subclass by extending B.
class C extends B {
C() {
[Link]("Inside C's constructor.");
}
}
class CallingCons {
public static void main(String args[]) {
C c = new C(); [Link]
}
o/p
The output from this program is shown here:
Inside A’s constructor
Inside B’s constructor
Inside C’s constructor
[Link]
Method Overriding
• In a class hierarchy, when a method in a
subclass has the same name and type
signature as a method in its superclass, then
the method in the subclass is said to override
the method in the superclass.
[Link]
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
[Link]("i and j: " + i + " " + j);
}
}
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
// display k – this overrides show() in A
void show() {
[Link]("k: " + k);
}
} [Link]
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
[Link](); // this calls show() in B
}
}
[Link]
• Dynamic Method Dispatch
• Dynamic method dispatch is the mechanism
by which a call to an overridden method is
resolved at run time, rather than compile
time. Dynamic method dispatch is important
because this is how Java implements run-time
polymorphism.
[Link]
// Dynamic Method Dispatch
class A {
void callme() {
[Link]("Inside A's callme method");
}
}
class B extends A {
// override callme()
void callme() {
[Link]("Inside B's callme method");
}
}
class C extends A {
// override callme()
void callme() {
[Link]("Inside C's callme method");
}
}
[Link]
class Dispatch {
public static void main(String args[]) {
A a = new A(); // object of type A
B b = new B(); // object of type B
C c = new C(); // object of type C
A r; // obtain a reference of type A
r = a; // r refers to an A object
[Link](); // calls A's version of callme
r = b; // r refers to a B object
[Link](); // calls B's version of callme
r = c; // r refers to a C object
[Link](); // calls C's version of callme
}
}
[Link]
o/p
Inside A’s callme method
Inside B’s callme method
Inside C’s callme method
[Link]
Using Abstract Classes
• There are situations in which you will want to
define a superclass that declares the structure of
a given abstraction without providing a complete
implementation of every method.
• sometimes you will want to create a superclass
that only defines a generalized form that will be
shared by all of its subclasses, leaving it to each
subclass to fill in the details.
• To declare an abstract method, use this general
form:
abstract type name(parameter-list);
[Link]
// A Simple demonstration of abstract.
abstract class A {
abstract void callme();
// concrete methods are still allowed in abstract classes
void callmetoo() {
[Link]("This is a concrete method.");
}
}
class B extends A {
void callme() {
[Link]("B's implementation of callme.");
}
}
class AbstractDemo {
public static void main(String args[]) {
B b = new B();
[Link]();
[Link]();
}
} [Link]
Using final with Inheritance
Using final to Prevent Overriding
• To disallow a method from being overridden,
specify final as a modifier at the start of its
declaration.
• Methods declared as final cannot be
overridden.
[Link]
class A {
final void meth() {
[Link]("This is a final method.");
}
}
class B extends A {
void meth() { // ERROR! Can't override
[Link]("Illegal!");
}
}
[Link]
Using final to Prevent Inheritance
• Sometimes you will want to prevent a class from being
inherited. To do this, precede the class declaration with
final.
• Declaring a class as final implicitly declares all of its
methods as final, too.
final class A {
// ...
}
// The following class is illegal.
class B extends A { // ERROR! Can't subclass A
// ...
}
[Link]
The Object Class
• There is one special class, Object, defined by
Java. All other classes are subclasses of Object.
That is, Object is a superclass of all other
classes.
[Link]
[Link]