Chapter 4
Chapter 4
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
3
To inherit a class, we use the extends keyword.
class A {
int i, j;
void showij() {
System.out.println("i and j: " + i + " " + j);
}}
class B extends A {
int k;
void showk() {
System.out.println("k: " + k);
}
void sum() {
System.out.println("i+j+k: " + (i+j+k));
}} 4
You can only specify one super class for any
subclass that you create.
5
4.1.2 Member Access and Inheritance
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
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: super.member
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 ( ) {
System.out.println ( “ i in superclass : “ + super.i );
System.out.println ( “i in subclass :“ + i );
}}
class UseSuper {
public static void main ( String args[ ] ) {
B subOb = new B ( 1 , 2 );
SubOb.show( );
}} 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)
12
class A {
A( ) {
System.out.println ( “ Inside A’s Constructor . “ );
}}
// Create a subclass by extending class A
class B extends A {
B(){
System.out.println ( “ Inside B’s constructor. “);
}}
// Create another subclass by extending B
class C extends B {
C(){
System.out.println ( “ 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.
. 14
class A {
int i ,j;
A ( int a, int b) {
i= a;
j= b;
}
// display i and j
Void show ( ) {
Sytem.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:
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 ( ) {
super.show( ) ; // this calls A’s show( )
System.out.println ( “ k : “ + k) ;
}}
16
THE END OF
CHAPTER 4
THANK U!!
17