5. Inheritance. Objectives

5. Inheritance Objectives By the end of this session, you should be able to : • read and understand source code for derived classes • derive a new cla...
Author: Gary Patterson
4 downloads 0 Views 264KB Size
5. Inheritance Objectives By the end of this session, you should be able to : • read and understand source code for derived classes • derive a new class • define a set of classes that behave polymorphically • take advantage of Java/C#’s run-time typing 1 CSA1012 - Joseph Cordina(C)

A Bit of Terminology • The interface of a class is the set of methods and instances visible to the classes outside. • The interface provides a means of access to the class and defines the properties of such a class. • The implementation is the way the interface is implemented. • The interface is more a design problem while the implementation is a coding problem 2 CSA1012 - Joseph Cordina(C)

Introduction • Inheritance is characteristic of object-oriented languages • Whenever we identify the “is a” relationship between two classes, it is appropriate to derive the more specific class from the more general class: – the more general class is the superclass, or base class or parent class. – the more specific class is the subclass, or derived class or child class – we “abstract” the common features and embody them in a parent class

3 CSA1012 - Joseph Cordina(C)

Introduction (cont.) • The derived class: – inherits all of the parent’s members and implementation – optionally, adds additional members – optionally, modifies the behaviour of some methods

• Use of inheritance produces a hierarchy of classes

4 CSA1012 - Joseph Cordina(C)

Example of Inheritance • Suppose we see a need for two types of text objects: – CompactText (emphasizes storage efficiency rather than functionality) – WordProcText (used for word processing; contains font information)

• We note that they will have many common features, such as a getlength() method giving the length of the text 5 CSA1012 - Joseph Cordina(C)

Example of Inheritance (cont.) • We start by defining a base class TextObject, which includes a getlength() method • We then derive the two subclasses: – both inherit the getlength() interface – either might inherit the implementation – or, override the implementation with its own

• We can derive other subclasses later

6 CSA1012 - Joseph Cordina(C)

Derivation Syntax • In Java/C#, a derived class inherits from a base class by “extending” the base class: public class TextObject { private Integer length; public Integer getLength() { return length; } }

7 CSA1012 - Joseph Cordina(C)

Derivation Syntax (cont.) [Java] public class CompactText extends TextObject { … } public class WordProcText extend TextObject { … } [C#] public class CompactText:TextObject { … } public class WordProcText:TextObject { … }

• A Java/C# class can derive from one base class only • All classes automatically derive from the grand parent class java.lang.Object/System.Object 8 CSA1012 - Joseph Cordina(C)

Effects of Inheritance • Any public member of the base class is also a public member of derived classes: WordProcText wpt = new WordProcText (); Integer len = wpt.getLength ();

• Each instance of a derived class contains all the instance variables of its base class, plus any additional ones of the derived class

9 CSA1012 - Joseph Cordina(C)

Effects of Inheritance (cont.) • Any instance of a derived class is an instance of its base class: WordProcText wpt=new WordProcText(); CompactText ct = new CompactText(); TextObject tobj = wpt; // OK tobj = ct; // OK ct = tobj; // compile-time error ct = (CompactText) tobj; // OK see // later

10 CSA1012 - Joseph Cordina(C)

Protected Access • The following access specifier applies to derived classes: protected – package and subclass access public class A { protected Integer x; }

11 CSA1012 - Joseph Cordina(C)

Protected Access (cont.) • Member x is accessible to: – any class that derives from class A – in the class that hosts it

12 CSA1012 - Joseph Cordina(C)

Accessibility Summary Package2

Package1

Class A Class C

extends

Class B

Class E

Class D

Access to class A’s members Accessible by classes Access specifier public A,B,C,D,E protected A,B,D default A,B,C[Java] private A 13 CSA1012 - Joseph Cordina(C)

Overriding Methods • Suppose class CompactString provides its own implementation of method getlength(), rather than inheriting class TextObject’s • This is overriding a method: – the derived class replaces the original implementation with its own “new and improved” versions – it implements the method in a way that is unique to the derived class 14 CSA1012 - Joseph Cordina(C)

Overriding Methods (cont.) • Syntax example: [Java] public class TextObject { public Integer getLength () { // TextObject’s implementation } } public class CompactText extends TextObject { public Integer getLength () { // CompactText’s implementation } } [C#] public class TextObject { public virtual Integer getLength () { // TextObject’s implementation } } public class CompactText:TextObject { public override Integer getLength () { // CompactText’s implementation } } • virtual marks the first definition, showing a method is overridable. override marks a method as it overriding a parent’s method. 15 CSA1012 - Joseph Cordina(C)

Dynamic Method Dispatching • Consider the following: TextObject tobj=new CompactText(); Integer len = tobj.getLength ();

- the apparent type of the target object is TextObject, but - the actual type of the target object is CompactText

16 CSA1012 - Joseph Cordina(C)

Dynamic Method Dispatching (cont.) • Java/C# selects the method to dispatch based on the actual type, not the apparent type, at runtime • This is achieved since every object stores inside it the original type it was defined with • This is another advantage of using an interpreter at runtime

17 CSA1012 - Joseph Cordina(C)

Polymorphism • Java/C#’s dynamic method dispatching supports polymorphism – ability of a program to treat objects of different related types as if they were of the same type • Classes related by inheritance support a common interface and respond to similar method calls: – the action taken by the target appears to the calling code to be the same – even though the different types can respond in completely different ways – This is because an object has an identity of its own and responds accordingly

18 CSA1012 - Joseph Cordina(C)

Polymorphism (cont.) • User code can invoke the “same” function for objects of different related types • At run time, the program will invoke the function appropriate to each object’s type • Each object can “do its own thing” based on its actual class • For polymorphism to work, the method must always be defined in the base class, otherwise we cannot over-ride it.

19 CSA1012 - Joseph Cordina(C)

Polymorphism • Example: invoking the getLength() method on each element of an array of TextObject. TextObject[] tarr = { new EncodedText(),new WordProcText(), new CompactText(),new TextObject() }; Integer totalLen = new Integer(0); for (Integer i = new Integer(0); i.intValue()