Subtyping vs inheritance
Madhavan Mukund
[Link]
Programming Concepts using Java
Week 3
Subclasses, subtyping and inheritance
Class hierarchy provides both subtyping and inheritance
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 2/4
Subclasses, subtyping and inheritance
Class hierarchy provides both subtyping and inheritance
Subtyping
Capabilities of the subtype are a superset of the main type
If B is a subtype of A, wherever we require an object of type A, we can use an object of
type B
Employee e = new Manager(...); is legal
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 2/4
Subclasses, subtyping and inheritance
Class hierarchy provides both subtyping and inheritance
Subtyping
Capabilities of the subtype are a superset of the main type
If B is a subtype of A, wherever we require an object of type A, we can use an object of
type B
Employee e = new Manager(...); is legal
Inheritance
Subtype can reuse code of the main type
B inherits from A if some functions for B are written in terms of functions of A
[Link]() uses [Link]()
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 2/4
Subtyping vs inheritance
Recall the following example
queue, with methods insert-rear, delete-front
stack, with methods insert-front, delete-front
deque, with methods insert-front, delete-front, insert-rear, delete-rear
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 3/4
Subtyping vs inheritance
Recall the following example
queue, with methods insert-rear, delete-front
stack, with methods insert-front, delete-front
deque, with methods insert-front, delete-front, insert-rear, delete-rear
What are the subtype and inheritance relationships between these classes?
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 3/4
Subtyping vs inheritance
Recall the following example
queue, with methods insert-rear, delete-front
stack, with methods insert-front, delete-front
deque, with methods insert-front, delete-front, insert-rear, delete-rear
What are the subtype and inheritance relationships between these classes?
Subtyping
deque has more functionality than queue or stack
deque is a subtype of both these types
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 3/4
Subtyping vs inheritance
Recall the following example
queue, with methods insert-rear, delete-front
stack, with methods insert-front, delete-front
deque, with methods insert-front, delete-front, insert-rear, delete-rear
What are the subtype and inheritance relationships between these classes?
Subtyping
deque has more functionality than queue or stack
deque is a subtype of both these types
Inheritance
Can suppress two functions in a deque and use it as a queue or stack
Both queue and stack inherit from deque
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 3/4
Subclasses, subtyping and inheritance
Class hierarchy represents both subtyping and inheritance
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 4/4
Subclasses, subtyping and inheritance
Class hierarchy represents both subtyping and inheritance
Subtyping
Compatibility of interfaces.
B is a subtype of A if every function that can be invoked on an object of type A can
also be invoked on an object of type B.
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 4/4
Subclasses, subtyping and inheritance
Class hierarchy represents both subtyping and inheritance
Subtyping
Compatibility of interfaces.
B is a subtype of A if every function that can be invoked on an object of type A can
also be invoked on an object of type B.
Inheritance
Reuse of implementations.
B inherits from A if some functions for B are written in terms of functions of A.
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 4/4
Subclasses, subtyping and inheritance
Class hierarchy represents both subtyping and inheritance
Subtyping
Compatibility of interfaces.
B is a subtype of A if every function that can be invoked on an object of type A can
also be invoked on an object of type B.
Inheritance
Reuse of implementations.
B inherits from A if some functions for B are written in terms of functions of A.
Using one idea (hierarchy of classes) to implement both concepts blurs the
distinction between the two
Madhavan Mukund Subtyping vs inheritance Programming Concepts using Java 4/4