Inheritance Inheritance is the primary concept separating objectoriented

Inheritance • Inheritance is the primary concept separating object-oriented programming from other programming paradigms. Fall 2002 COP 3330 Object Oriented Programming 1

Inheritance • Inheritance defines a relationship among classes. • A class C 2 may inherit from (or extend, or derive from) another class C 1 parent (super-class) C 2 child (sub-class) • We say that C 1 is the parent (super-class) of C 2. • Or, C 2 is the child (sub-class) of C 1. Fall 2002 COP 3330 Object Oriented Programming 2

Inheritance (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. Fall 2002 COP 3330 Object Oriented Programming 3

extends keyword • extends clause specifies the super-class of a class. Class. Modifiers class Sub. Class. Name extends Super. Class. Name { Class. Member. Declarations } class C 2 extends C 1 {. . . } sub-class (child) super-class (parent) • C 2 is derived from C 1. Fall 2002 COP 3330 Object Oriented Programming 4

Accessibility Example (1) package fruit; public class Fruit { public int weight; int color; protected String name; private String scientific. Name; } package fruit; public class Apple extends Fruit { public String to. String() { return weight + “, ” + color + “, ” + name + “, ” +scientific. Name; } } • This code won’t compile. Why? Fall 2002 COP 3330 Object Oriented Programming 5

Accessibility Example (2) package fruit; public class Fruit { public int weight; int color; protected String name; private String scientific. Name; } package fruit. apple; public class Apple extends Fruit { public String to. String() { return weight + “, ” + color + “, ” + name; } } • This code won’t compile. Why? Fall 2002 COP 3330 Object Oriented Programming 6

Accessibility Example (3) package fruit; public class Fruit { public int weight; int color; protected String name; private String scientific. Name; } package fruit. apple; public class Apple extends Fruit { public String to. String() { return weight + “, ” + name; } } • Will this code compile? Why? Fall 2002 COP 3330 Object Oriented Programming 7

Another Inheritance Example Assume the following class definitions: public class Fruit { private String name; public String get. Name() { return name; } } public class Apple extends Fruit { } And the following usage: Apple a = new Apple(); System. out. println(a. get. Name()); Fall 2002 // Apple inherits Fruit’s // get. Name method COP 3330 Object Oriented Programming 8

Object class • If we do not use an extends clause in a class declaration, the superclass of that declared class is the class Object. • In Java, every class (except the Object class) has a super-class C 1 {. . . } is equivalent to class C 1 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. Fall 2002 COP 3330 Object Oriented Programming 9

Constructors 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 noargument 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 subclass). – How do we call super-class constructors? reserved word super Fall 2002 COP 3330 Object Oriented Programming 10

Constructors of Sub-Classes (Example) class C 1 { private int x; public C 1() { x = 0; } public C 1(int x) { this. x = x; } } class C 2 extends C 1 { private int y; public C 2(int x, int y) { super(x); the constructor of the super-class is explicitly invoked this. y = y; } public C 2(int y) { this(1, y); Another version of the constructor of this class is invoked } public C 2() { y = 2; No-argument version of the constructor of the super-class } is implicitly invoked. super(); } Fall 2002 COP 3330 Object Oriented Programming 11

A Parcel Class public class Parcel { protected double weight; protected double volume; protected boolean is. Air. Freight; public Parcel(double w, double v, boolean b) { weight = w; volume = v; is. Air. Freight = b; } // shipping. Cost method uses weight, volume, and is. Air. Freight to // calculate cost of shipping this parcel public double shipping. Cost() {. . . } Fall 2002 COP 3330 Object Oriented Programming 12

A Rectangular. Parcel Class public class Rectangular. Parcel extends Parcel { private double parcel. Length, parcel. Width, parcel. Height; private final int weight. Per. Cubic. Meter = 677; public Rectangular. Parcel(double len, double w, double h, boolean b) { Call superclass constructor with weight = 0, volume = 0, and super(0, 0, b); boolean value b that was passed in the constructor call for parcel. Length = len; Rectangular. Parcel; set weight and volume to 0 since we can now parcel. Width = w; calculate them instead of asking for them in the constructor. parcel. Height = h; volume = parcel. Length * parcel. Width * parcel. Height; weight = volume * weight. Per. Cubic. Meter; }. . . } Fall 2002 COP 3330 Object Oriented Programming 13

A Cylindrical. Parcel Class public class Cylindrical. Parcel extends Parcel { private double parcel. Radius, parcel. Length; public Cylindrical. Parcel(double len, double r) { super(0, 0, false); parcel. Length = len; parcel. Radius = r; volume = parcel. Length * Math. PI * Math. pow(parcel. Radius, 2); weight = volume * 600; }. . . } Fall 2002 COP 3330 Object Oriented Programming 14

UML Notation for Inheritance Parcel Rectangular. Parcel Fall 2002 Cylindrical. Parcel COP 3330 Object Oriented Programming 15

Constructors (cont. ) • What happens when no constructor is defined for a class? No-argument constructor will be provided implicitly for that class C 2 extends C 1 { // no constructor is defined public C 2() { // 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! Fall 2002 COP 3330 Object Oriented Programming 16

Order of Initialization Step 1. The fields of the super-class are initialized using default values. 2. One of the constructors of the super-class is executed. 3. The fields of the extended class (sub-class) are initialized using the default values. 4. One of the constructors of the extended class (sub-class) is executed. Fall 2002 COP 3330 Object Oriented Programming 17

Order of Initialization Step (cont. ) class C 1 { private int x = 1; public C 1() { x = 2; } } class C 2 extends C 1 { private int y = 3; public C 2() { super(); y = 4 ; } } Fall 2002 // executed first // executed second // executed third // executed fourth COP 3330 Object Oriented Programming 18

Class Hierarchies • Multiple classes can be derived from a single parent. • Inheritance relations develop into a class hierarchy. C 1 C 2 C 5 C 3 C 6 C 4 Notice: All subclasses (except Object) have exactly one parent. There is no multiple inheritance in Java. C 7 • Parent classes should carry most general properties. • Child classes should carry most specific properties. Fall 2002 COP 3330 Object Oriented Programming 19

Object Class class class C 1 C 2 C 3 C 4 C 5 C 6 C 7 {. . . } extends {. . . } extends C 1 C 2 { { . . . } } Object C 6 {. . . } C 1 C 2 C 4 Fall 2002 C 6 C 3 C 7 C 5 COP 3330 Object Oriented Programming 20
- Slides: 20