Inheritance, and classes in general, let you express relationships between parts of your code. If you create a subclass called PostContactSystem and implement it, you don’t have to change anything in your User class, or any of the code involved in the sending of messages! Modelsįinally, one of the advantages of inheritance is about how you design systems. Not only that, let’s say you want to extend the functionality of your system by adding a new way of contacting your users by Post. If a User contains a preferred contact medium - of type ContactSystem - you know you can call Send() on it and that they’ll be sent that particular message in their chosen way. Let’s say you provide two options to contact your users - by text message or by email: The ContactSystem class and two of its subclasses, TextContactSystem and EmailContractSystem.Īs you can see, the ContactSystem class defines a behavior - Send(message) - and is subclassed byTextContactSystem and EmailContactSystem. Inheritance also allows for extensibility - i.e., the possibility of extending a program’s functionality without having to modify existing code. Subclasses of Employee would probably Work() differently! We’ll talk more about this concept - overriding - in Chapter 3. While you don’t have to rewrite subclass methods, you can. Imagine you had a dozen subclasses of Employee - that’s a lot of copy and pasted code without inheritance! If you wanted to make a change to Walk()’s functionality, you’d have to update every single subclass. When you make Employee a subclass of Person, you don’t have to re-add the name attribute or re-write the Walk() Method. ![]() The simplest reason is the same reason that you write functions - reusability. There are a few different interrelated reasons why we use inheritance in object-oriented programming: Reusability This kind of relationship comes up a lot in the real world - can you think of another example? Why Inheritance? What might happen if you try to put a Person object into a method that is supposed to use an Employee? A Person doesn’t have a work() method, so you’d get an error! More about this in Chapter 5. We’ll talk more about this - and types - in Chapter 5.Ī Person is not an Employee. ![]() You can use an Employee anywhere you could use a Person in your code. Notice that Walk() and Name are attributes of Person that are inherited by Employee, which also adds its own attributes: EmployeeID and Work(). The superclass Person is the parent of the subclass Employee. Employee is a subclass/child of Person, and Person is a superclass/parent of Employee. Here is an example of two classes: Person and Employee. Inheritance allows you to create a subclass (or child) of a class that contains the parent’s attributes and other attributes specific to the child. One of the most useful things about object-oriented programming is the concept of inheritance.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |