Title: Inheritance
1Inheritance
- Inheritance is the primary concept separating
object-oriented programming from other
programming paradigms.
2Inheritance
- Inheritance defines a relationship among classes.
- A class C2 may inherit from (or extend, or derive
from) another class C1. - C1 parent (super-class)
- C2 child (sub-class)
- We say that C1 is the parent (super-class) of
C2. - Or, C2 is the child (sub-class) of C1.
3Inheritance (cont.)
- Using the inheritance mechanism, a sub-class will
inherit functionalities of its super-class. - Of course, a sub-class may also define its own
functionalities. - So, a sub-class will have
- its own fields the fields inherited from its
super-class - its own methods the methods inherited from its
super-class. - All of the members of a super-class will be
inherited by its sub-class, but only certain
members will be directly accessible by their
names in that sub-class. - public and protected members of a class are
accessible by name from the sub-classes of that
class. - private members are not accessible by name from
sub-classes. - package members are accessible by name from
sub-classes in the same package.
4extends keyword
- extends clause specifies the super-class of a
class. - ClassModifiers class SubClassName extends
SuperClassName - ClassMemberDeclarations
-
- class C2 extends C1 ...
- sub-class super-class
- (child) (parent)
- C2 is derived from C1.
5Accessibility Example (1)
- package fruit
- public class Fruit
- public int weight
- int color
- protected String name
- private String scientificName
-
- package fruit
- public class Apple extends Fruit
- public String toString()
- return weight , color , name ,
scientificName -
-
- This code wont compile. Why?
6Accessibility Example (2)
- package fruit
- public class Fruit
- public int weight
- int color
- protected String name
- private String scientificName
-
- package fruit.apple
- public class Apple extends Fruit
- public String toString()
- return weight , color , name
-
-
- This code wont compile. Why?
7Accessibility Example (3)
- package fruit
- public class Fruit
- public int weight
- int color
- protected String name
- private String scientificName
-
- package fruit.apple
- public class Apple extends Fruit
- public String toString()
- return weight , name
-
-
- Will this code compile? Why?
8Another Inheritance Example
- Assume the following class definitions
- public class Fruit
- private String name
- public String getName()
- return name
-
-
- public class Apple extends Fruit
-
- And the following usage
- Apple a new Apple()
- System.out.println(a.getName()) // Apple
inherits Fruits // getName method
9Object class
- If we do not use an extends clause in a class
declaration, the super-class of that declared
class is the class Object. - In Java, every class (except the Object class)
has a super-class. - class C1 ...
- is equivalent to
- class C1 extends Object ...
- This means that Object class will be on the top
of the class hierarchy. In other words, the class
hierarchy in Java is a tree, and its root node is
the Object class.
10Constructors of Sub-Classes
- The initialization of the fields of a sub-class
consists of two phases - The initialization of the inherited fields
- The initialization of the fields that are
declared in that sub-class. - One of the constructors of the super-class must
be invoked to initialize the fields inherited
from the super-class. - One of the constructors of the super-class must
be invoked explicitly Otherwise the no-argument
version of the constructor of the super-class
will be automatically invoked. - This invocation must be the first statement of
the constructor of the sub-class. (Or one of the
constructors of the sub-class must invoke another
version of the constructor of that sub-class). - How do we call super-class constructors?
reserved word super
11Constructors of Sub-Classes (Example)
- class C1
- private int x
- public C1() x 0
- public C1(int x) this.x x
-
- class C2 extends C1
- private int y
- public C2(int x, int y)
- super(x) the constructor of the super-class
is explicitly invoked - this.y y
-
- public C2(int y)
- this(1,y) Another version of the constructor
of this class is invoked -
- public C2()
- y 2 No-argument version of the constructor
of the super-class - is implicitly invoked. super()
12A Parcel Class
- public class Parcel
- protected double weight
- protected double volume
- protected boolean isAirFreight
- public Parcel(double w, double v, boolean b)
- weight w volume v isAirFreight b
-
-
- // shippingCost method uses weight, volume, and
isAirFreight to // calculate cost of shipping
this parcel - public double shippingCost() ...
13A RectangularParcel Class
- public class RectangularParcel extends Parcel
- private double parcelLength, parcelWidth,
parcelHeight - private final int weightPerCubicMeter 677
- public RectangularParcel(double len, double w,
double h, - boolean b)
- super(0, 0, b)
- parcelLength len
- parcelWidth w
- parcelHeight h
- volume parcelLength parcelWidth
parcelHeight - weight volume weightPerCubicMeter
-
- ...
Call superclass constructor with weight 0,
volume 0, and boolean value b that was passed
in the constructor call for RectangularParcel
set weight and volume to 0 since we can now
calculate them instead of asking for them in the
constructor.
14A CylindricalParcel Class
- public class CylindricalParcel extends Parcel
- private double parcelRadius, parcelLength
- public CylindricalParcel(double len, double r)
- super(0, 0, false)
- parcelLength len
- parcelRadius r
- volume parcelLength Math.PI
Math.pow(parcelRadius, 2) - weight volume 600
-
- ...
15UML Notation for Inheritance
16Constructors (cont.)
- What happens when no constructor is defined for a
class? - ? No-argument constructor will be provided
implicitly for that class - class C2 extends C1
- // no constructor is defined
- public C2() // this constructor is
- super() // automatically provided by
Java. -
-
- If super-class does not have no-argument version
of the constructor, a compilation error occurs. - If another constructor of the class is present,
no-argument version of the constructor will not
be provided automatically!
17Order of Initialization Step
- The fields of the super-class are initialized
using default values. - One of the constructors of the super-class is
executed. - The fields of the extended class (sub-class) are
initialized using the default values. - One of the constructors of the extended class
(sub-class) is executed.
18Order of Initialization Step (cont.)
- class C1
- private int x 1 // executed first
- public C1()
- x 2 // executed second
-
-
- class C2 extends C1
- private int y 3 // executed third
- public C2()
- super()
- y 4 // executed fourth
-
19Class Hierarchies
- Multiple classes can be derived from a single
parent. - Inheritance relations develop into a class
hierarchy. - C1
- C2 C3 C4
- C5 C6 C7
- Parent classes should carry most general
properties. - Child classes should carry most specific
properties.
Notice All subclasses (except Object) have
exactly one parent. There is no multiple
inheritance in Java.
20Object Class
- class C1 ...
- class C2 extends C1 ...
- class C3 extends C1 ...
- class C4 extends C2 ...
- class C5 extends C2 ...
- class C6 ...
- class C7 extends C6 ... Object
- C1 C6
- C2 C3 C7
- C4 C5