SUBCLASSES AND SUBTYPES
Muhammad Adil Raja
Roaming Researchers, Inc.
cbna
April 17, 2015
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 1 / 13
OUTLINE I
1 INTRODUCTION
2 SUBCLASS, SUBTYPE AND SUBSTITUTION
3 SUMMARY
4 REFERENCES
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 2 / 13
Introduction
INTRODUCTION
In this chapter we will explore the relationships between the two
concepts of subclass and subtype.
To assert that one class is a subclass of another is to simply say it
was built using inheritance.
It is a statement concerning how it was constructed.
To assert that one class is a subtype of another is to say that it
preserves the purpose of the original.
It is a statement concerning meaning.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 3 / 13
Subclass, Subtype and Substitution
SUBCLASS, SUBTYPE AND SUBSTITUTION
MESSAGE SYNTAX
The distinction between subtype and subclass is important
because of their relationship to substitution.
Recall the argument that asserted a child class has the same
behavior as the parent, and thus a variable declared as the parent
class should in fact be allowed to hold a value generated from a
child class.
But does this argument always hold true?
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 4 / 13
Subclass, Subtype and Substitution
WHAT IS A TYPE?
What do we mean when we use the term type in describing a
programming language?
A set of values. (The type int, for example, describes
-2147483648 to 2147483647)
A set of operations. (We can do arithmetic on ints, not on
booleans).
A set of properties. (If we divide 8 by 5 we are not surprized when
the result is 1, and not 1.6).
What about when we consider classes (or interfaces) as a system
for defining types?
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 5 / 13
Subclass, Subtype and Substitution
THE PROBLEM OF DEFINING TYPES
THE PROBLEM OF DEFINING TYPES
Consider how we might define a Stack ADT:
interface Stack {
public void push ( Object value ) ;
public Object top ( ) ;
public void pop ( ) ;
}
Notice how the interface itself says nothing about the LIFO property,
which is the key defining feature of a stack. Is the following a stack?
class NonStack implements Stack {
public void push ( Object value ) { v = value ; }
public Object top ( ) { return v ; }
public void pop ( ) { v = null ; }
private Object v = null ;
}
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 6 / 13
Subclass, Subtype and Substitution
THE DEFINITION OF SUBTYPE
So now we can better understand the concept of a subtype. A
subtype preserves the meaning (purpose, or intent) of the parent.
Problem, meaning is extremely difficult to define.
Think about how to define the LIFO characteristics of the stack.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 7 / 13
Subclass, Subtype and Substitution
SUBCLASSES ARE NOT NECESSARILY SUBTYPES
It is easy to create a subclass that is not a subtype – think of
NonStack.
It is also possible to create subtypes that are not subclasses.
But class Dictionary will also support the same interface, and
perform similar actions – but Dictionary is not a subclass of Array.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 8 / 13
Subclass, Subtype and Substitution
THE SUBSTITUTION PARADOX
There is a curious paradox that lies at the heart of most strongly
typed object-oriented programming languages.
Substitution is permitted, based on subclasses.
That is, a variable declared as the parent type is allowed to hold a
value derived from a child type.
Yet from a semantic point of view, substitution only makes sense if
the expression value is a subtype of the target variable.
If substitution only makes sense for subtypes and not for all
subclasses, why do programming languages based the validity of
assignment on subclasses?
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 9 / 13
Subclass, Subtype and Substitution
THE UNDECIDABILITY OF SUBTYPE RELATIONSHIP
It is trivial to determine if one class is a subclass of another.
It is extremely difficult to define meaning (think of the Stack ADT),
and even if you can it is almost always impossible to determine if
one class preserves the meaning of another.
One of the classic corollaries of the halting problem is that there is
no procedure that can determine, in general, if two programs have
equivalent behavior.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 10 / 13
Subclass, Subtype and Substitution
IS THIS A PROBLEM?
What does it take to create a subclass that is not a subtype?
The new class must override at least one method from the parent.
It must preserve the type signatures
But it must violate some important property of the parent.
Is this common? Not likely. But it shows you where to look for
problem areas.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 11 / 13
Summary
SUMMARY
To say a class is a subclass of another simply asserts that is was
built using inheritance.
By itself, the subclass relationship says nothing about the
behavior of the child in relation to the parent.
The term subtype is used to describe a class that matches the
behavior of another class.
It is easy to build subclasses that are not subtypes.
It is possible (although not as easy) to build subtypes that are not
subclasses.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 12 / 13
References
REFERENCES I
Images and content for developing these slides have been taken
from the follwoing book with the permission of the author.
An Introduction to Object Oriented Programming, Timothy Budd.
This presentation is developed using Beamer:
CambridgeUS, orchid.
Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 13 / 13

Subclasses and Subtypes

  • 1.
    SUBCLASSES AND SUBTYPES MuhammadAdil Raja Roaming Researchers, Inc. cbna April 17, 2015 Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 1 / 13
  • 2.
    OUTLINE I 1 INTRODUCTION 2SUBCLASS, SUBTYPE AND SUBSTITUTION 3 SUMMARY 4 REFERENCES Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 2 / 13
  • 3.
    Introduction INTRODUCTION In this chapterwe will explore the relationships between the two concepts of subclass and subtype. To assert that one class is a subclass of another is to simply say it was built using inheritance. It is a statement concerning how it was constructed. To assert that one class is a subtype of another is to say that it preserves the purpose of the original. It is a statement concerning meaning. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 3 / 13
  • 4.
    Subclass, Subtype andSubstitution SUBCLASS, SUBTYPE AND SUBSTITUTION MESSAGE SYNTAX The distinction between subtype and subclass is important because of their relationship to substitution. Recall the argument that asserted a child class has the same behavior as the parent, and thus a variable declared as the parent class should in fact be allowed to hold a value generated from a child class. But does this argument always hold true? Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 4 / 13
  • 5.
    Subclass, Subtype andSubstitution WHAT IS A TYPE? What do we mean when we use the term type in describing a programming language? A set of values. (The type int, for example, describes -2147483648 to 2147483647) A set of operations. (We can do arithmetic on ints, not on booleans). A set of properties. (If we divide 8 by 5 we are not surprized when the result is 1, and not 1.6). What about when we consider classes (or interfaces) as a system for defining types? Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 5 / 13
  • 6.
    Subclass, Subtype andSubstitution THE PROBLEM OF DEFINING TYPES THE PROBLEM OF DEFINING TYPES Consider how we might define a Stack ADT: interface Stack { public void push ( Object value ) ; public Object top ( ) ; public void pop ( ) ; } Notice how the interface itself says nothing about the LIFO property, which is the key defining feature of a stack. Is the following a stack? class NonStack implements Stack { public void push ( Object value ) { v = value ; } public Object top ( ) { return v ; } public void pop ( ) { v = null ; } private Object v = null ; } Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 6 / 13
  • 7.
    Subclass, Subtype andSubstitution THE DEFINITION OF SUBTYPE So now we can better understand the concept of a subtype. A subtype preserves the meaning (purpose, or intent) of the parent. Problem, meaning is extremely difficult to define. Think about how to define the LIFO characteristics of the stack. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 7 / 13
  • 8.
    Subclass, Subtype andSubstitution SUBCLASSES ARE NOT NECESSARILY SUBTYPES It is easy to create a subclass that is not a subtype – think of NonStack. It is also possible to create subtypes that are not subclasses. But class Dictionary will also support the same interface, and perform similar actions – but Dictionary is not a subclass of Array. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 8 / 13
  • 9.
    Subclass, Subtype andSubstitution THE SUBSTITUTION PARADOX There is a curious paradox that lies at the heart of most strongly typed object-oriented programming languages. Substitution is permitted, based on subclasses. That is, a variable declared as the parent type is allowed to hold a value derived from a child type. Yet from a semantic point of view, substitution only makes sense if the expression value is a subtype of the target variable. If substitution only makes sense for subtypes and not for all subclasses, why do programming languages based the validity of assignment on subclasses? Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 9 / 13
  • 10.
    Subclass, Subtype andSubstitution THE UNDECIDABILITY OF SUBTYPE RELATIONSHIP It is trivial to determine if one class is a subclass of another. It is extremely difficult to define meaning (think of the Stack ADT), and even if you can it is almost always impossible to determine if one class preserves the meaning of another. One of the classic corollaries of the halting problem is that there is no procedure that can determine, in general, if two programs have equivalent behavior. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 10 / 13
  • 11.
    Subclass, Subtype andSubstitution IS THIS A PROBLEM? What does it take to create a subclass that is not a subtype? The new class must override at least one method from the parent. It must preserve the type signatures But it must violate some important property of the parent. Is this common? Not likely. But it shows you where to look for problem areas. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 11 / 13
  • 12.
    Summary SUMMARY To say aclass is a subclass of another simply asserts that is was built using inheritance. By itself, the subclass relationship says nothing about the behavior of the child in relation to the parent. The term subtype is used to describe a class that matches the behavior of another class. It is easy to build subclasses that are not subtypes. It is possible (although not as easy) to build subtypes that are not subclasses. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 12 / 13
  • 13.
    References REFERENCES I Images andcontent for developing these slides have been taken from the follwoing book with the permission of the author. An Introduction to Object Oriented Programming, Timothy Budd. This presentation is developed using Beamer: CambridgeUS, orchid. Muhammad Adil Raja ( Roaming Researchers, Inc. cbna)Subclasses and Subtypes April 17, 2015 13 / 13