Docsity

Programming Languages - Inheritance - Notes | ECS 140A, Papers of Programming Languages

Toggle sidebar
bg1
Introduction
•What is inheritance?
•How does inheritance allow one to extend programseasily?
•What are different kinds of inheritance?
1. Interface inheritance
2. Implementation inheritance
How do the two differ?
•What is dynamic binding?
•How does Java support inheritance?
•How does C++ support inheritance?
•How are classes/subclasses and dynamic binding
implemented?
ECS140A, Winter’06 Inheritance, slide 1 c
Raju Pandey
Inheritance
•A mechanism for supporting extensibility of programs:
support for specifying general concepts (through class
mechanism) as well support for specializing concepts.
Various terms used for inheritance: concatenation,
sub-classing,derivation, and prefixing.
•Allow new object definitions in terms of existing ones.
R=X⊕∆X
R: newly defined class.
X: set of proper ties inherited from an existing object or class.
∆X: new properties
⊕: how to combine Xand ∆X.
•What can ∆Rcontain? (i) Additional state information (ii)
Additional methods (iii) Redefinition of methods and states
(iv) Deletion of methods and states.
•Terms used for Rand X:
–Xis R’s parent or immediate ancestor or Ris X’s child,
immediate descendant
–Xis R’s superclass or Ris X’s subclass
•Languages differ in how R,⊕, and ∆Xare defined.
•Model parent-child dependency relationship between
superclass and subclass an inheritance graph; Nodes of
graph: Class, Edges: class-subclass relationship.
•Single inheritance: When inheritance graph is a tree
•Multiple Inheritance: When a class inherits from more than
one class.
ECS140A, Winter’06 Inheritance, slide 2 c
Raju Pandey
Inheritance ⇐⇒ Extensibility
•Class Rextends class Xto create a new class.
inherits from
XR
•Inheritance used to create a new class:
X⊕=
∆XR
•Questions: (i) What is X? (ii) What is ∆X(iii) What is ’+’?
•X = Interface + Implementation.
•So
??
SISB
CICB=
⊕
•Inheritance model(⊕) in a language specifies :
– What will be the resulting interface?
– What about implementation?
•It defines a set of rules which determine what the resulting
class will be when
– when interfaces in Xand ∆Xmatch,
– when a method is implemented again,
– when same variable is declared,
– more than one class are extended (multiple inheritance)
•It determines
– how objects of ∆Xare classified with respect to X.
– what methods are legally allowed, and
– what methods will be executed on an object.
ECS140A, Winter’06 Inheritance, slide 3 c
Raju Pandey
Inheritance
•Two views of inheritance:
1. High level mechanism for representing conceptual
specialization
Used for specification inheritance or interface inheritance;
Known as sub-typing
Represents sub-typing and is-a relationship
is-a: Assume that S is a subclass of superclass C; Every
object of class S is an object of class C. Note the subset
relationship between objects of class S and class C.
S
C
Subtype principle: An object of a subtype may be used
anywhere its super-type can be used.
2. Low level mechanism for sharing data and behavior.
Known as implementation inheritance,representation
inheritance, or sub-classing..
Represents sub-classing relationship
•Use of inheritance for specialization: essential, whereas use
of inheritance for implementation accidental.
•Some languages (such as POOL-I and typed SmallTalk)
separate inheritance from sub-typing.
In languages such as C++, it occurs within a single
framework.
ECS140A, Winter’06 Inheritance, slide 4 c
Raju Pandey
pf3
pf4
pf5

Partial preview of the text

Download Programming Languages - Inheritance - Notes | ECS 140A and more Papers Programming Languages in PDF only on Docsity!

Introduction

• What is inheritance?

• How does inheritance allow one to extend programs easily?

• What are different kinds of inheritance?

  1. Interface inheritance
  2. Implementation inheritance How do the two differ?

• What is dynamic binding?

• How does Java support inheritance?

• How does C++ support inheritance?

• How are classes/subclasses and dynamic binding

implemented?

ECS140A, Winter’06 Inheritance, slide 1 ©c Raju Pandey

Inheritance

• A mechanism for supporting extensibility of programs:

support for specifying general concepts (through class mechanism) as well support for specializing concepts. Various terms used for inheritance: concatenation , sub-classing , derivation , and prefixing.

• Allow new object definitions in terms of existing ones.

R = X ⊕ ∆X

R: newly defined class.

X: set of properties inherited from an existing object or class.

∆X: new properties

⊕: how to combine X and ∆X.

• What can ∆R contain? (i) Additional state information (ii)

Additional methods (iii) Redefinition of methods and states (iv) Deletion of methods and states.

• Terms used for R and X:

– X is R’s parent or immediate ancestor or R is X’s child ,

immediate descendant

– X is R’s superclass or R is X’s subclass

• Languages differ in how R, ⊕, and ∆X are defined.

• Model parent-child dependency relationship between

superclass and subclass an inheritance graph; Nodes of graph: Class, Edges: class-subclass relationship.

• Single inheritance : When inheritance graph is a tree

• Multiple Inheritance : When a class inherits from more than

one class.

ECS140A, Winter’06 Inheritance, slide 2 ©c Raju Pandey

Inheritance ⇐⇒ Extensibility

• Class R extends class X inherits from to create a new class.

X R

• Inheritance used to create a new class:

X ⊕ ∆ X = R

• Questions : (i) What is X? (ii) What is ∆X (iii) What is ’+’?

• X = Interface + Implementation.

• So

CI CB ⊕ SI SB =??

• Inheritance model (⊕) in a language specifies :

  • What will be the resulting interface?
  • What about implementation?

• It defines a set of rules which determine what the resulting

class will be when

– when interfaces in X and ∆X match,

  • when a method is implemented again,
  • when same variable is declared,
  • more than one class are extended (multiple inheritance)

• It determines

– how objects of ∆X are classified with respect to X.

  • what methods are legally allowed, and
  • what methods will be executed on an object.

Inheritance

• Two views of inheritance:

  1. High level mechanism for representing conceptual specialization Used for specification inheritance or interface inheritance ; Known as sub-typing Represents sub-typing and is-a relationship is-a : Assume that S is a subclass of superclass C; Every object of class S is an object of class C. Note the subset relationship between objects of class S and class C.

S

C

Subtype principle : An object of a subtype may be used anywhere its super-type can be used.

  1. Low level mechanism for sharing data and behavior. Known as implementation inheritance , representation inheritance , or sub-classing .. Represents sub-classing relationship

• Use of inheritance for specialization: essential, whereas use

of inheritance for implementation accidental.

• Some languages (such as POOL-I and typed SmallTalk)

separate inheritance from sub-typing. In languages such as C++, it occurs within a single framework.

How does the inherited interface look like?

  • Interface of X + ∆X: All methods of X and ∆X.
  • Example: A class Shape and its subclasses Box,

Ellipse. Interface of Shape has following methods:

Draw, Initialize, Offset, SetWidth

Interface of Box, Ellipse: Interface of Shape +

additional methods.

  • What does interface inheritance mean? Interface inheritance suggests type-subtype relationship: whatever operation that can be performed on an object of type can also be performed on an object of subtype.
  • Objects of subclasses can behave like objects of superclass: procedure f(ent: Shape); ... sh: Shape; bx: Box; ... f(bx);
  • Is the above parameter passing type-safe? Yes, because any

method that f will invoke on an object of Shape also exists

for objects of Box.

  • What about following method invocation? procedure f1(ent: Box); f1(sh); Not type safe because we are constructing a specialized object from a general object.

ECS140A, Winter’06 Inheritance, slide 5 ©c Raju Pandey

How does implementation inheritance look?

  • Inherit instance variables + Inherit implementation of methods.

Example: Objects of Box contain all instance variables of

Shape. They inherit implementations of methods as well.

  • Inheritance provides support for re-usability: can reuse existing code by inheriting from superclass.
  • Conflicts between inheritance of interface and implementation. Example: Stack class is implemented using a class List: class List: Procedure Add(Element elm) returns List; Procedure Next() returns Element; ... Stack inherits from List: Procedure Push(); Procedure Pop();

If inherited naively, Stack also inherits the interface. What

does Next() mean for a Stack object?

Stack is-not-a List.

  • Need to distinguish between type-based inheritance, and implementation-based inheritance. Unfortunately many languages (e.g., SmallTalk) do not distinguish the two. What about C++?

ECS140A, Winter’06 Inheritance, slide 6 ©c Raju Pandey

Why inheritance?

  • Example: Assume Diagram built out of various shapes:

Box, Ellipse, Line, Text etc.

s.c cc s.o

  • A Diagram object contains a set of Box, Ellipse,

Line, Text objects.

procedure draw(Diagram d) begin for each Shape s in d do begin case s of BOX: draw_box(); ELLIPSE: draw_ellipse(); LINE: draw_line(); TEXT: draw_text(); ... end

  • Problems:
    • Keep track of types of objects
    • Extensibility: say add a Circle type of object. Need to

redefine Draw, redefine a new type, add the proper case

statement at different places. User is managing various types through constant definitions and case statements.

How can inheritance help?

  • Capture relationship among different types of objects

Box Ellipse Line Text

Shape

Circle

  • Diagram object: set of Shape objects procedure draw(Diagram d) begin foreach Shape s in d do begin draw(s); end end
  • Different draw called on the basis of Shape s’s class.
  • Extensibility: add a new class Brace. No change in

implementation of Draw

Circle

Shape

Box EllipseLine Text Brace

  • Programming at a higher level of abstraction

Support for interface inheritance in Java

  • Support multiple inheritance for interfaces interface W { ... } interface X extends W { ... } interface Y extends W { ... } class Z implements X, Y { ... } Z is a subtype of X and Y (subtype of more than one class.)
  • Name conflicts: - Same names but different signature: name overloading - Same signature: one method - Different return types: not allowed
  • Check the following types and subtypes assignments interface A { ... } interface B extends A { ... } interface C { ... } interface D extends extends A, C { ... } class E { ... } class F extends E implements D { ... } class G implements B { ... } ... { A a1; B b1; C c1; D d1; E e1; F f1; G g1; ... a1 = d1; a1 = e1; a1 = f1; a1 = b1; a1 = c1; a1 = g1; b1 = a1; b1 = c1; b1 = d1; b1 = e1; b1 = f1; b1 = g1; c1 = a1; c1 = b1; c1 = d1; c1 = e1; c1 = f1; c1 = g1; d1 = a1; d1 = b1; d1 = c1; d1 = e1; d1 = f1; d1 = g1; e1 = a1; e1 = b1; e1 = c1; e1 = d1; e1 = f1; e1 = g1; f1 = a1; f1 = b1; f1 = c1; f1 = d1; f1 = e1; f1 = g1; g1 = a1; g1 = b1; g1 = c1; g1 = d1; g1 = e1; g1 = f1; }

ECS140A, Winter’06 Inheritance, slide 13 ©c Raju Pandey

Support for inheritance in C++

  • Support for inheritance of both interface as well as implementation. In addition it allows one to specify if interface should be inherited.
  • Interface inheritance class Box: public Shape { ... };

Box inherits i) interface, ii) instance variables, and iii) default

implementation of methods. It can override the implementation of methods by re-implementing them.

public keyword used to specify that Box is a subtype of

Shape. An object of type Box can be used for an object of

Shape. Similarly, a pointer or reference of type Box is also

a reference or pointer of type Shape.

  • Implementation inheritance only: class Stack: private List { ... };

Interface of List become private in Stack; they cannot be

accessed from outside. Stack is not a subtype of List.

An object of Stack cannot be used for an object of List.

ECS140A, Winter’06 Inheritance, slide 14 ©c Raju Pandey

Support for inheritance in Java

  • Definition: member functions that obey dynamic binding must

be declared virtual:

class Shape { ... public: virtual void Draw(Shape *) { .. } } class Ellipse: public Shape { public: virtual void Draw(Shape *); }

virtual keyword tells the compiler that Draw may vary

according to the type of object. (All subclasses don’t have to re-implement them).

  • Usage: Dynamically bind them through pointers and references only. (Why?) Shape *sh; Ellipse *el; sh->Draw(); // invokes Shape’s Draw sh = el; // sh points to an object el->Draw(); // invoke Ellipse’s Draw
  • Once a function is declared virtual, it remains so in all derived classes

Dynamic Binding

  • Signature of a virtual function must remain the same in the subclass. Why? Otherwise allows conversion of pointers to base class into subclasses. Class B { ... virtual void f(B ); }; class D: public B { ... virtual void f(D ); }; void g(B p1, B p2) { p1->f(p2); } void h() { g(new D, new B); }

Abstract Classes

  • Abstract class mechanism supports notion of general

concept such as Shape, of which only more concrete

variants, such as Box are used.

  • A class is abstract if it has at least one pure virtual function: class Shape { ... virtual void Draw() = 0; };
  • No object of abstract class is created; pointers or references to abstract class can be defined:

Method invocations of subclasses

  • Non-virtual functions are invoked according to the class of object.
  • Virtual functions cannot differ in return type. Base class definition: class base { virtual void vf1(); virtual void vf2(); virtual void vf3() void f(); }; Derived class definition: class derived: public base { void vf1(); // overrides base::vf void vf2(int);// different from base::vf char vf3(); // error: differs in return type void f(); // base::f() is not virtual. }; Usage of two classes: void g() { derived d; base *bp = &d; \ bp->vf1(); // call derived::vf1\ bp->vf2(); // call base::vf2\ bp->f(); // call base::f }

ECS140A, Winter’06 Inheritance, slide 17 ©c Raju Pandey

Special member functions and Inheritance

  • Constructors, Copy constructors, Destructor, and Assignment

operator (=) are not inherited.

Why not? Because they do not have corresponding definitions for subclasses.

  • Order of invocation for a class with base classes or member objects: - Constructor: Constructors for base and member objects are called before the constructor for the derived class. - Destructor: The body of the destructor is executed before the destructor for base classes and member objects.
  • Virtual:
    • A constructor may not be a virtual.
    • A destructor may be virtual.

Example: When deleting Diagram data structure,

destructor on corresponding objects should be called. A rule of thumb: declare a virtual destructor for every base class that has a virtual function.

ECS140A, Winter’06 Inheritance, slide 18 ©c Raju Pandey

Implementation mechanisms: Storage layout

  • Objects of derived classes are composed by concatenating the members of the base class and the derived class itself: class A { int a; void f(int); }; class B: public A { int b; void g(int); }; class C: public B { int c; void h(int); };
  • Implementation of objects of C may look like any of the following:

int a; int b; int c;

B

A

C

Most languages (including C++) do not guarantee the order in which storage is allocated for derived classes.

  • Accesses to various elements can be directly defined.

Implementation of virtual functions

  • Define a table, say vtbl, for a class. The table contains pointers to virtual functions of the class. Each object of a class contains pointer to the virtual function table of the class. class A { int a; virtual void f(int); virtual void g(int); virtual void h(char); }; class B: public A { int b; void g(int); }; class C: public B { int c; void h(int); } Assume the member functions are all public.

int a; int b; int c; vptr

vtbl: &A::f

&C::h

&B::g

  • call to virtual functions transformed C pc = new C; pc->g(2); ⇒ ((pc->vptr[1]))(pc, 2);