J
a
v
a
I
C
S
2
0
1
University Of Ha’il 1
Chapter 1
Inheritance
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Introduction to Inheritance
Inheritance is one of the main techniques of object-
oriented programming (OOP)
Using this technique, further classes can be created
from existing ones; those classes are said to inherit
the methods and instance variables of the class they
inherited
The original class is called the base class
The new class is called a derived class
Advantage: Reusing existing code
7-2
Base Class
Derived Class
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Inheritance
Animal
Dog FoxCat
3
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Inheritance
Land Vehicle
Truck CarBus
4
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Inheritance
Land Vehicle
Car
Toyota
TruckBus
Yaris Corolla Camry
5
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
6
An Example Inheritance Hierarchy
Data and behavior associated with super-classes
are accessible to those subclasses
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
7
The is-a Rule
Always check generalizations to ensure
they obey the is-a rule
“A checking account is an account”
“A village is a municipality”
“A cat is an animal”
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 8
Derived Class (subclass)
A derived class, also called a subclass (or child class), is
defined by starting with another already defined class,
called a base class or superclass or parent class, and
adding (and/or changing) methods, instance variables,
and static variables
The derived class inherits:
all the public methods,
all the public and private instance variables, and
all the public and private static variables from the base class.
The derived class can add more instance variables,
static variables, and/or methods.
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 9
Derived Classes
The extends clause in a class declaration establishes an
inheritance relationship between two classes. It has the
following syntax:
class DerivedClass extends BaseClass
{
// body of the class
}
Base Class
Derived Class
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Parent and Child Classes
A base class is often called the parent class
A derived class is then called a child class
These relationships are often extended such that
a class that is a parent of a parent . . . of another
class is called an ancestor class
If class A is an ancestor of class B, then class B can be
called a descendent of class A
7-10
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 11
Inheritance and Variables
Variable Hiding
If a variable of a class has a same name (type maybe
different) as a superclass variable, in that case class
variable hides the superclass variable.
You can access a hidden variable by using super keyword
super.variable
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 12
Example (Inheritance and variables)
class C1 {
static int x;
}
class C2 extends C1 {
static String x;
}
class Cc {
public static void main(String[] args)
{
C1 p = new C1();
p.x = 55;
System.out.println("p.x=" +
p.x);
C2 q = new C2();
q.x = "This is a String";
System.out.println( "q.x=" +
q.x);
}
}
Output:
p.x=55
q.x=This is a String
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 13
Example (Inheritance and variables)
class M100 {
int x = 100;
}
class M200 extends M100 {
String x = " Welcome “ ;
void display()
{
System.out.println( "x=" + x);
System.out.println( "super.x="
+super.x);
}
}
class SuperKeyword {
public static void main(String[] args)
{
M200 m200 = new M200();
m200.display();
}
}
Output:
x= Welcome
super.x=100
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Example
Inherit1.java
Inherit2.java
University Of Ha’il 14
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Dr.Mwaffaq Abu Alhija University Of Ha’il 15
Homework
Write an application that illustrates how to access a hidden
variable. Class G declares a static variable x. Class H
extends G and declares an instance variable x. A display()
method in H displays both of these variables.
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Inheriting Methods
Override method:
Supply a different implementation of a method that
exists in the superclass
Must have same signature (same name and same
parameter types)
Inherit method:
Don't supply a new implementation of a method that
exists in superclass
Superclass method can be applied to the subclass
objects
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
Inheriting Methods (Cont’d)
Add method:
Supply a new method that doesn't exist in
the superclass
New method can be applied only to subclass
objects
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
18
Overloading vs Overriding
Comparison between overloading and overriding
Overriding deals with two
methods, one in a parent
class and one in a child class
with the same signature
Overriding lets you define a
similar operation in different
ways for different object types
Overloading deals with
multiple methods in the same
class with the same name but
different signatures.
Overloading lets you define a
similar operation in different
ways for different data
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 19
Method Overriding
The access permission of an overridden method can be
changed from private in the base class to public in the
derived class.
However, the access permission of an overridden
method can not be changed from public in the base
class to a more restricted access permission in the
derived class.
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 20
Method Overriding
Given the following method header in a base case:
private void doSomething()
The following method header is valid in a derived class:
public void doSomething()
However, the opposite is not valid
Given the following method header in a base case:
public void doSomething()
The following method header is not valid in a derived
class:
private void doSomething()
P
r
o
g
r
a
m
m
i
n
g
W
i
t
h
J
a
v
a
I
C
S
2
0
1
University Of Ha’il 21
Example (Method Overriding)
class A1
{
void hello()
{
System.out.println( "Hello from A1" );
}
}
class B1 extends A1
{
void hello()
{
System.out.println( "Hello from B1" );
}
}
class C1 extends B1
{
void hello()
{
System.out.println( "Hello from C1");
}
}
class MethodOverriding
{
public static void main(String[] arg)
{
C1 obj = new C1();
obj.hello();
A1 a = new C1() ;
a.hello();
}
}
Output:
Hello from C1
Hello from C1