Chapter 3 Inheritance and Polymorphism
Chapter 3 Inheritance and Polymorphism
Inheritance
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 examples
Superclass: Subclasses
Student: GraduateStudent, UndergraduateStudent
Shape: Circle, Triangle, Rectangle
Loan: CarLoan, HomeImprovementLoan, MortgageLoan
Employee: Faculty, Staff
BankAccount: CheckingAccount, SavingsAccount
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.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}
}
...cont.
üThe method must have the same name as in the parent class
üThe method must have the same parameter as in the parent class.
üThere must be an IS-A relationship (inheritance).
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 {
THE JAVA LANGUAGE
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 wish to access the superclass version of an
overridden function, you can do so by using
super. 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.
...cont
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);
}
}
OutPut: i and j: 1 2
k: 3
...cont.
// Methods with differing type signatures are overloaded – 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.
...cont.
class B extends A {
int k;
B(int a, int b, int c) {
super(a, b);
k = c;THE JAVA LANGUAGE
}
// overload show()
void show(String msg) {
System.out.println(msg + k);
}
}
...cont.
class Override {
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 type signature different from the one in A, which takes no
parameters. Therefore, no overriding (or name hiding) takes place.
Why Overridden Methods?
It allow Java to support run-time polymorphism.
it’s another way that Java implements the “one interface,
multiple methods” aspect of polymorphism.
Used correctly, the superclass provides all elements that a
subclass can use directly.
This allows the subclass the flexibility to define its own
methods, yet still enforces a consistent interface.
Dynamic, run-time polymorphism is one of the most powerful
mechanisms that object_x0002_oriented design brings to bear on
code reuse and robustness. The ability of existing code
libraries to call methods on instances of new classes without
recompiling while maintaining a clean abstract interface is a
profoundly powerful tool.
Overloading Methods
In Java it is possible to define two or more methods within
the same class that share the same name, as long as their
parameter declarations are different. When this is the case,
the methods are said to be overloaded, and the process is
referred to as method overloading.
Java uses the type and/or number of arguments as its guide
to determine which version of the overloaded method to
actually call.Thus, overloaded methods must differ in the
type and/or number of their parameters.
Here is a simple example that illustrates method overloading:
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
// Overload test for one integer parameter.
void test(int a) {
System.out.println("a: " + a);
}
// Overload test for two integer parameters.
void test(int a, int b) {
System.out.println("a and b: " + a + " " + b);
}
...cont
// overload test for a double parameter
double test(double a) {
System.out.println("double a: " + a);
return a*a;
}
}
class Overload {
public static void main(String args[]) {
OverloadDemo ob = new OverloadDemo();
double result;
// call all versions of test()
}
...cont.
ob.test();
ob.test(10);
ob.test(10, 20);
result = ob.test(123.25);
System.out.println("Result of ob.test(123.25): " + result);
}
}
This program generates the following output:
No parameters
a: 10
a and b: 10 20
double a: 123.25
Result of ob.test(123.25): 15190.5625
As you can see, test( ) is overloaded four times. The
first version takes no parameters, the second
takes one integer parameter, the third takes two integer
parameters, and the fourth takes one double parameter.