inheritance
Declaring one class or type as a specialised version of another, so it receives the parent's members and can be used wherever the parent is expected.
In more detail
It lets a new type reuse and override behaviour from an existing one. Deep hierarchies are hard to change, so many designs prefer composition and small interfaces.
Examples
C#: members marked virtual or abstract can be replaced with override; the course seals classes by default.Java: a class extends one parent class and may implement many interfaces; sealed types restrict who may extend them; records cannot extend a class.static class StorageFailure extends RuntimeException { }class Puppy(Dog): makes Puppy a subclass of Dog; prefer composition for has-a relationships.class Puppy(Dog):
passRelated terms
- composition — Building a component out of smaller components that the consumer assembles, instead of configuring one big component with flags.
- abstract — The C# modifier for a class that cannot be created directly, or a member with no body that every derived class must supply.
- virtual — The C# modifier for a member that has a working body a derived class may replace with override.
- override — The C# modifier that declares a replacement for an inherited virtual or abstract member, or for methods such as Equals and ToString.
- sealed — The C# modifier that stops any other class from inheriting from a class.
- class — A blueprint in code that bundles data and the functions that work on it, from which objects are made.
- interface (Java) — A Java type that lists operations without fixing how they are carried out, so different classes can provide their own implementations.
- sealed type — A class or interface that lists exactly which types may extend or implement it, so the compiler knows the complete family.
- method overriding — Defining a method in a subclass with the same name as one in its parent, so the subclass's version is used instead.
- super() — A built-in that lets a method call the version of itself in the parent class, most often super().__init__(...) in an initialiser.
- composition — Building a class by giving it other objects as attributes, a has-a relationship, instead of inheriting from them.
- method resolution order — The order in which Python searches a class and its parents for a method or attribute.
- extends — A keyword with three jobs in TypeScript: a class inheriting from another, a limit on a generic type parameter, and a tsconfig.json inheriting a base file.
- prototype chain — The linked list of objects JavaScript searches when looking up a property, which is how inheritance works under the hood.
- mixin — A reusable piece of shape or behaviour that is combined into other types, instead of inherited from a single parent class.
Taught in
- The C# Language: Types, Variables, and Flow Control — Modern .NET Core, C# & Enterprise Microservices
- Classes, Interfaces, and Object-Oriented C# — Modern .NET Core, C# & Enterprise Microservices
- Records And Data Classes — Modern Java: Virtual Threads & High-Throughput Services
- Sealed Types And Exhaustiveness — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Classes, Records And Initialisation Order — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Overloading And Varargs — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Scope, var And Immutable Objects — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Inheritance, Overriding And Object's Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sealed Types, Casts And Pattern Matching — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Interfaces: Default, Static And Private Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Enums With Fields, Constructors And Methods — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Object-Oriented Java — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Checked, Unchecked, Catch And Finally — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Try-With-Resources And Suppressed Exceptions — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: Exceptions, Arrays And Collections — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Serialization: What Survives The Trip — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Locales, Resource Bundles And Formatting — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Sprint: I/O And Localization — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Final Review And Exam Day — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Any Type As A Condition — Hands-on Python: Complete Video Course & Book
- Raising, And Writing Your Own Exception — Hands-on Python: Complete Video Course & Book
- Inheritance And Composition — Hands-on Python: Complete Video Course & Book
- Union Types, Intersections And Property Conflicts — Production TypeScript
- The Exception Family Tree — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Sixty Five Minutes, Forty Items: How PCAP Is Run — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Import Variants, dir() And sys.path — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Handlers, args, raise And assert — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exception Classes Of Your Own — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Exceptions Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Classes, Objects And Two Stacks — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Class Variables, Instance Variables And __dict__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Methods And The self Parameter — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Introspection: hasattr, __name__, __module__ And __bases__ — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Inheritance, super() And Polymorphism — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Multiple Inheritance, The MRO And Diamonds — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Object-Oriented Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Closures: Functions That Remember — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- read, readline, errno And bytearray — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Twelve Traps, Mostly Objects — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
References
- Inheritance (C# fundamentals)
- Java Tutorials: What Is Inheritance?
- dev.java: Inheritance
- Java Language Specification, chapter 8: Classes
- Python tutorial: inheritance
- TypeScript Handbook: Classes
- MDN: Classes