0% found this document useful (0 votes)
14 views6 pages

Oop L8

Uploaded by

scarfock56
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)
14 views6 pages

Oop L8

Uploaded by

scarfock56
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/ 6

2021 OOP-LECTURE

8
An Introduction to Polymorphism in Java:
Method Overriding
In a class hierarchy, when a method in a subclass has the same return type and signature as a
method in its superclass, then the method in the subclass is said to override the method in the
superclass. When an overridden method is called from within a subclass, it will always refer to
the version of that method defined by the subclass. The version of the method defined by the
superclass will be hidden. Consider the following:
// Method overriding.
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("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() {
System.out.println("k: " + k);
}
}
class Override {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show(); // this calls show() in B
}
}

The output produced by this program is shown here:


k: 3
When show( ) is invoked on an object of type B, the version of show( ) defined within B is used.
That is, the version of show( ) inside B overrides the version declared in A.
If you want to access the superclass version of an overridden method, you can do so by using
super.

1
2021 OOP-LECTURE
8
For example, in this version of B, the superclass version of show( ) is invoked within the
subclass’ version. This allows all instance variables to be displayed.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;
}
void show() {
super.show(); // this calls A's show()
System.out.println("k: " + k);
}
}
If you substitute this version of show( ) into the previous program, you will see the following
output:
i and j: 1 2
k: 3
Here, super.show( ) calls the superclass version of show( ).
Because signatures differ, this show( ) simply overloads show( ) in superclass A.

Method overriding occurs only when the return types and signatures of the two methods are
identical. If they are not, then the two methods are simply overloaded. For example, consider
this modified version of the preceding example:
/* Methods with differing signatures are
overloaded and not overridden. */
class A {
int i, j;
A(int a, int b) {
i = a;
j = b;
}
// display i and j
void show() {
System.out.println("i and j: " + i + " " + j);
}
}
// Create a subclass by extending class A.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c; }
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
2
2021 OOP-LECTURE
8
class Overload {
public static void main(String args[]) {
B subOb = new B(1, 2, 3);
subOb.show("This is k: "); // this calls show() in B
subOb.show(); // this calls show() in A
}
}
The output produced by this program is shown here:
This is k: 3
i and j: 1 2
The version of show( ) in B takes a string parameter. This makes its signature different from the
one in A, which takes no parameters. Therefore, no overriding (or name hiding) takes place.

Using Abstract Classes


Sometimes you will want to create a superclass that defines only a generalized form that will
be shared by all of its subclasses, leaving it to each subclass to fill in the details. Such a class
determines the nature of the methods that the subclasses must implement but does not, itself,
provide an implementation of one or more of these methods.
An abstract method is created by specifying the abstract type modifier. An abstract method
contains no body and is, therefore, not implemented by the superclass. Thus, a subclass must
override it—it cannot simply use the version defined in the superclass. To declare an abstract
method, use this general form:
abstract type name(parameter-list);
As you can see, no method body is present. The abstract modifier can be used only on normal
methods. It cannot be applied to static methods or to constructors.
A class that contains one or more abstract methods must also be declared as abstract by
preceding its class declaration with the abstract specifier. Since an abstract class does not
define a complete implementation, there can be no objects of an abstract class. Thus,
attempting to create an object of an abstract class by using new will result in a compile-time
error.
When a subclass inherits an abstract class, it must implement all of the abstract methods in the
superclass. If it doesn’t, then the subclass must also be specified as abstract. Thus, the abstract
attribute is inherited until such time as a complete implementation is achieved. Using an
abstract class, you can improve the TwoDShape class. Since there is no meaningful concept of
area for an undefined two-dimensional figure, the following version of the preceding program
declares area( ) as abstract inside TwoDShape, and TwoDShape as abstract. This, of course,
means that all classes derived from TwoDShape must override area( ).
// Create an abstract class.
abstract class TwoDShape {
private double width;
private double height;
private String name;
3
2021 OOP-LECTURE
8
// A default constructor.
TwoDShape() {
width = height = 0.0;
name = "null";
}
// Parameterized constructor.
TwoDShape(double w, double h, String n) {
width = w;
height = h;
name = n;
}
// Construct object with equal width and height.
TwoDShape(double x, String n) {
width = height = x;
name = n;
}
// Construct an object from an object.
TwoDShape(TwoDShape ob) {
width = ob.width;
height = ob.height;
name = ob.name;
}
// Accessor methods for width and height.
double getWidth() { return width; }
double getHeight() { return height; }
void setWidth(double w) { width = w; }
void setHeight(double h) { height = h; }
String getName() { return name; }
void showDim() {
System.out.println("Width and height are " +
width + " and " + height);
}
// Now, area() is abstract.
abstract double area();
}
// A subclass of TwoDShape for triangles.
class Triangle extends TwoDShape {
private String style;
// A default constructor.
Triangle() {
super();
style = "null";
}
// Constructor for Triangle.
Triangle(String s, double w, double h) {
super(w, h, "triangle");
style = s;
}
// Construct an isosceles triangle.
4
2021 OOP-LECTURE
8
Triangle(double x) {
super(x, "triangle"); // call superclass constructor
style = "isosceles";
}
// Construct an object from an object.
Triangle(Triangle ob) {
super(ob); // pass object to TwoDShape constructor
style = ob.style;
}
double area() {
return getWidth() * getHeight() / 2;
}
void showStyle() {
System.out.println("Triangle is " + style);
}
}
// A subclass of TwoDShape for rectangles.
class Rectangle extends TwoDShape {
// A default constructor.
Rectangle() {
super();
}
// Constructor for Rectangle.
Rectangle(double w, double h) {
super(w, h, "rectangle"); // call superclass constructor
}
// Construct a square.
Rectangle(double x) {
super(x, "rectangle"); // call superclass constructor
}
// Construct an object from an object.
Rectangle(Rectangle ob) {
super(ob); // pass object to TwoDShape constructor
}
boolean isSquare() {
if(getWidth() == getHeight()) return true;
return false;
}
double area() {
return getWidth() * getHeight();
}
}
class AbsShape {
public static void main(String args[]) {
TwoDShape shapes[] = new TwoDShape[4];
shapes[0] = new Triangle("right", 8.0, 12.0);
shapes[1] = new Rectangle(10);
shapes[2] = new Rectangle(10, 4);
shapes[3] = new Triangle(7.0);
5
2021 OOP-LECTURE
8
for(int i=0; i < shapes.length; i++) {
System.out.println("object is " +
shapes[i].getName());
System.out.println("Area is " + shapes[i].area());
System.out.println();
}
}
}
As the program illustrates, all subclasses of TwoDShape must override area( ).
To prove this to yourself, try creating a subclass that does not override area( ). You will receive a
compile-time error. Of course, it is still possible to create an object reference of type
TwoDShape, which the program does. However, it is no longer possible to declare objects of
type TwoDShape. Because of this, in main( ) the shapes array has been shortened to 4, and a
generic TwoDShape object is no longer created.
One last point: notice that TwoDShape still includes the showDim( ) and getName( )
methods and that these are not modified by abstract. It is perfectly acceptable—indeed, quite
common—for an abstract class to contain concrete methods which a subclass is free to use as
is. Only those methods declared as abstract need be overridden by subclasses.

You might also like