CHAPTER 4 INHERITANCE
1
Contents:
Inheritance basics
Member access and inheritance
Using super keyword
Method overriding
2
4.1 Inheritance Basics
Allows the creation of hierarchical
classifications.
A class that is inherited is called a super class
The class that does the inheriting is called a
subclass
Subclass inherits all of the instance variables
and methods defined by the super class and
adds its own, unique elements.
3
To inherit a class, we use the extends keyword.
class A {
int i, j;
void showij() {
[Link]("i and j: " + i + " " + j);
}}
class B extends A {
int k;
void showk() {
[Link]("k: " + k);
}
void sum() {
[Link]("i+j+k: " + (i+j+k));
}} 4
You can only specify one super class for any
subclass that you create.
Java does not support the inheritance of
multiple super classes into a single subclass.
no class can be a super class of itself.
5
4.1.2 Member Access and Inheritance
Although a subclass includes all of the
members of its super class, it cannot access
those members of the super class that have
been declared as private.
6
class A {
int i;
private int j;
void setij(int x, int y) {
i = x;
j = y;
}
}
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
7
4.2 Using super Keyword
Super has two general forms.
calls the super class constructor
used to access a member of the super class
Using super to call super class Constructor
A subclass can call a constructor method
defined by its super class.
Its form is super ( parameter-list );
8
// 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 super class constructor
weight = m;
}}
9
BoxWeight no longer initializes these values itself.
It only needs to initialize the value unique to it:
weight.
The Second use of super
To call a super class method.
To call a super class instance variables.
Its general form is: [Link]
member can be either a method or an instance
variable.
is most applicable to situation in which member
names of a subclass hide members by the same
name in the super class.
10
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]( );
}} 11
Super can also be used to call methods that
are hidden by a subclass.
When Constructors are called
constructors are called in order of derivation,
from super class to subclass. (based on
hierarchy)
since super( ) must be the first statement
executed in a subclass’ constructor , this order
is the same whether or not super ( ) is used.
12
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 ( );
}}
The output from this program is shown here :
Inside A’s constructor.
13
Inside B’s constructor.
4.3 Method Overriding
when a method in a subclass has the same and type
signature as a method in its super class, then the
method in the subclass is said to override the
method in the super class.
When an overridden method is called from within a
subclass, it will always refer to the version of that
method defined by the subclass.
version of the method defined by the super class will
be hidden.
. 14
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 );
}}
class override {
public static void main ( String args[ ] ) {
B subob = new B ( 1, 2, 3);
[Link]( ) ; // this calls show( ) in B;
}}
The output produced by this program is shown here:
15
k:3
If you wish to access the super class version of an
overridden function, you can do so by using super.
class B extends A {
int k;
B ( int a, int b ,int c) {
super ( a,b);
k = c;
}
void show ( ) {
[Link]( ) ; // this calls A’s show( )
[Link] ( “ k : “ + k) ;
}}
16
THE END OF
CHAPTER 4
THANK U!!
17