SlideShare una empresa de Scribd logo
1 de 25
Descargar para leer sin conexión
What Is Subclassing?




Subclassing is a process in which a new class is derived from an old one. It
is a distinct concept from
      (subtype) polymorphism,
      generalization/specialization and
      composition.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   2 / 12
Subclassing = Subtyping




     Subtypes are not required to share any code with their supertypes.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   3 / 12
Subclassing = Subtyping




     Subtypes are not required to share any code with their supertypes.
     On the other hand, subtypes are required to stick to the contract of
     their supertype (the Liskov substition principle).




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   3 / 12
Subclassing = Subtyping




     Subtypes are not required to share any code with their supertypes.
     On the other hand, subtypes are required to stick to the contract of
     their supertype (the Liskov substition principle).
Subtyping only concerns behavior, subclassing only concerns code.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   3 / 12
The Fallacy of Current Industrial Programming Languages




In Java (and many other languages)
      subclassing implies subtyping, in other words,
      every subclass has to stick to the contract of its parent class.
That severly limits possible code reuse!




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   4 / 12
Example




void DFS(Node n) {
  addToStack( n);
  while ( stackNotEmpty()) {
    n = removeFromStack();
    processNode( n);
    for ( Node c: n.children())
      addToStack( c);
  }
}




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   5 / 12
Example




                                                                  Obviously, addToStack
void DFS(Node n) {
                                                                  inserts a node at the
  addToStack( n);
                                                                  beginning of a list and
  while ( stackNotEmpty()) {
                                                                  removeFromStack removes
    n = removeFromStack();
                                                                  a node from the beginning
    processNode( n);
                                                                  of that list.
    for ( Node c: n.children())
      addToStack( c);
  }
}




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance     October 19, 2010   5 / 12
Example




                                                                  Obviously, addToStack
void DFS(Node n) {
                                                                  inserts a node at the
  addToStack( n);
                                                                  beginning of a list and
  while ( stackNotEmpty()) {
                                                                  removeFromStack removes
    n = removeFromStack();
                                                                  a node from the beginning
    processNode( n);
                                                                  of that list.
    for ( Node c: n.children())
      addToStack( c);                                             Is it correct to override
  }                                                               addToStack to insert a
}                                                                 node at the end of the list?




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance       October 19, 2010   5 / 12
Example (II)




     Not in Java—the new subtype would break the contract of the
     supertype.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   6 / 12
Example (II)




     Not in Java—the new subtype would break the contract of the
     supertype.
     However, it is perfectly legitimate in languages where subclassing
     does not imply subtyping—the new class simply isn’t type-compatible
     with the old one.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   6 / 12
Subclassing = Specialization




     Subclassing does not imply specialization—subclass does not have to
     be a specialized version of superclass.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   7 / 12
Subclassing = Specialization




     Subclassing does not imply specialization—subclass does not have to
     be a specialized version of superclass.
     Specialization does not imply subclassing—a specialized version of
     object does not have to share code with the base case.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   7 / 12
Subclassing = Specialization




     Subclassing does not imply specialization—subclass does not have to
     be a specialized version of superclass.
     Specialization does not imply subclassing—a specialized version of
     object does not have to share code with the base case.
     In fact, subtyping and specialization are distinct notions as well.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   7 / 12
Subclassing = Composition




     Subclassing and composition both facilitate code reuse.
     Is there anything that can be achieved by subclassing and not
     achieved by composition or vice versa?




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   8 / 12
Poor Man’s Inheritance



class Foo {                                         class Qux {
  void bar() {...}                                    Foo foo = new Foo();
  int baz() {...}                                     void bar() {
}                                                       foo.bar();
                                                      }

                                                        int baz() {
                                                          return foo.baz();
                                                        }
                                                    }




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   9 / 12
Poor Man’s Inheritance



class Foo {                                         class Qux {
  void bar() {...}                                    Foo foo = new Foo();
  int baz() {...}                                     void bar() {
}                                                       foo.bar();
                                                      }

                                                        int baz() {
                                                          return foo.baz();
                                                        }
                                                    }

Emulation of inheritance using composition is far from perfect—method
overriding can be emulated only in the simplest cases.

  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   9 / 12
The Essence of Inheritance




So what exactly is inheritance?




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   10 / 12
The Essence of Inheritance




So what exactly is inheritance?
      Inheritance is a mechanism facilitating code reuse.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   10 / 12
The Essence of Inheritance




So what exactly is inheritance?
      Inheritance is a mechanism facilitating code reuse.
      It can be thought about as the C-style include with overriding.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   10 / 12
The Essence of Inheritance




So what exactly is inheritance?
      Inheritance is a mechanism facilitating code reuse.
      It can be thought about as the C-style include with overriding.
      It enables reuse even in ways the original code creator never imagined.




  Michal P´se (CTU in Prague)
          ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   10 / 12
Implementation of Inheritance




     C++: virtual methods table.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   11 / 12
Implementation of Inheritance




     C++: virtual methods table.
     In Smalltalk: method dictionary.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   11 / 12
Implementation of Inheritance




     C++: virtual methods table.
     In Smalltalk: method dictionary.
     In systems with runtime: inline cache.




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   11 / 12
See




Taivalsaari, A. On the notion of inheritance. ACM Computing Surveys
28, 3 (Sep. 1996), 438–479.
http://doi.acm.org/10.1145/243439.243441




 Michal P´se (CTU in Prague)
         ıˇ                    Object Programming Lect. 4: Inheritance   October 19, 2010   12 / 12

Más contenido relacionado

Similar a Inheritance

Similar a Inheritance (6)

Multiple Dispatch
Multiple DispatchMultiple Dispatch
Multiple Dispatch
 
Subtyping
SubtypingSubtyping
Subtyping
 
Sour Pickles
Sour PicklesSour Pickles
Sour Pickles
 
Stack
StackStack
Stack
 
Type Systems
Type SystemsType Systems
Type Systems
 
Stack
StackStack
Stack
 

Más de Michal Píše

Más de Michal Píše (7)

Prototype Languages
Prototype LanguagesPrototype Languages
Prototype Languages
 
Reflection and Metadata
Reflection and MetadataReflection and Metadata
Reflection and Metadata
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Garbage Collection
Garbage CollectionGarbage Collection
Garbage Collection
 
Flow Control
Flow ControlFlow Control
Flow Control
 
Reclassification
ReclassificationReclassification
Reclassification
 
Functional Concepts
Functional ConceptsFunctional Concepts
Functional Concepts
 

Último

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 

Último (20)

UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 

Inheritance

  • 1.
  • 2. What Is Subclassing? Subclassing is a process in which a new class is derived from an old one. It is a distinct concept from (subtype) polymorphism, generalization/specialization and composition. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 2 / 12
  • 3. Subclassing = Subtyping Subtypes are not required to share any code with their supertypes. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
  • 4. Subclassing = Subtyping Subtypes are not required to share any code with their supertypes. On the other hand, subtypes are required to stick to the contract of their supertype (the Liskov substition principle). Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
  • 5. Subclassing = Subtyping Subtypes are not required to share any code with their supertypes. On the other hand, subtypes are required to stick to the contract of their supertype (the Liskov substition principle). Subtyping only concerns behavior, subclassing only concerns code. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 3 / 12
  • 6. The Fallacy of Current Industrial Programming Languages In Java (and many other languages) subclassing implies subtyping, in other words, every subclass has to stick to the contract of its parent class. That severly limits possible code reuse! Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 4 / 12
  • 7. Example void DFS(Node n) { addToStack( n); while ( stackNotEmpty()) { n = removeFromStack(); processNode( n); for ( Node c: n.children()) addToStack( c); } } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
  • 8. Example Obviously, addToStack void DFS(Node n) { inserts a node at the addToStack( n); beginning of a list and while ( stackNotEmpty()) { removeFromStack removes n = removeFromStack(); a node from the beginning processNode( n); of that list. for ( Node c: n.children()) addToStack( c); } } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
  • 9. Example Obviously, addToStack void DFS(Node n) { inserts a node at the addToStack( n); beginning of a list and while ( stackNotEmpty()) { removeFromStack removes n = removeFromStack(); a node from the beginning processNode( n); of that list. for ( Node c: n.children()) addToStack( c); Is it correct to override } addToStack to insert a } node at the end of the list? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 5 / 12
  • 10. Example (II) Not in Java—the new subtype would break the contract of the supertype. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 6 / 12
  • 11. Example (II) Not in Java—the new subtype would break the contract of the supertype. However, it is perfectly legitimate in languages where subclassing does not imply subtyping—the new class simply isn’t type-compatible with the old one. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 6 / 12
  • 12. Subclassing = Specialization Subclassing does not imply specialization—subclass does not have to be a specialized version of superclass. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
  • 13. Subclassing = Specialization Subclassing does not imply specialization—subclass does not have to be a specialized version of superclass. Specialization does not imply subclassing—a specialized version of object does not have to share code with the base case. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
  • 14. Subclassing = Specialization Subclassing does not imply specialization—subclass does not have to be a specialized version of superclass. Specialization does not imply subclassing—a specialized version of object does not have to share code with the base case. In fact, subtyping and specialization are distinct notions as well. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 7 / 12
  • 15. Subclassing = Composition Subclassing and composition both facilitate code reuse. Is there anything that can be achieved by subclassing and not achieved by composition or vice versa? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 8 / 12
  • 16. Poor Man’s Inheritance class Foo { class Qux { void bar() {...} Foo foo = new Foo(); int baz() {...} void bar() { } foo.bar(); } int baz() { return foo.baz(); } } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 9 / 12
  • 17. Poor Man’s Inheritance class Foo { class Qux { void bar() {...} Foo foo = new Foo(); int baz() {...} void bar() { } foo.bar(); } int baz() { return foo.baz(); } } Emulation of inheritance using composition is far from perfect—method overriding can be emulated only in the simplest cases. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 9 / 12
  • 18. The Essence of Inheritance So what exactly is inheritance? Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
  • 19. The Essence of Inheritance So what exactly is inheritance? Inheritance is a mechanism facilitating code reuse. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
  • 20. The Essence of Inheritance So what exactly is inheritance? Inheritance is a mechanism facilitating code reuse. It can be thought about as the C-style include with overriding. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
  • 21. The Essence of Inheritance So what exactly is inheritance? Inheritance is a mechanism facilitating code reuse. It can be thought about as the C-style include with overriding. It enables reuse even in ways the original code creator never imagined. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 10 / 12
  • 22. Implementation of Inheritance C++: virtual methods table. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
  • 23. Implementation of Inheritance C++: virtual methods table. In Smalltalk: method dictionary. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
  • 24. Implementation of Inheritance C++: virtual methods table. In Smalltalk: method dictionary. In systems with runtime: inline cache. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 11 / 12
  • 25. See Taivalsaari, A. On the notion of inheritance. ACM Computing Surveys 28, 3 (Sep. 1996), 438–479. http://doi.acm.org/10.1145/243439.243441 Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 4: Inheritance October 19, 2010 12 / 12