1 / 14

Lecture 10: Inheritance

Lecture 10: Inheritance. Subclasses and superclasses The inheritance chain Access control The Object cosmic superclass The super keyword Overriding methods Overriding vs overloading. Read Chapter 9. Chapter 9 is one of the most important chapters in the book.

gustav
Télécharger la présentation

Lecture 10: Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 10: Inheritance • Subclasses and superclasses • The inheritance chain • Access control • The Object cosmic superclass • The super keyword • Overriding methods • Overriding vs overloading

  2. Read Chapter 9 • Chapter 9 is one of the most important chapters in the book. • However, the key issue of dynamic binding is not explained very well, so please pay special attention to this topic in class (to be discussed Wednesday)

  3. Subclasses and Superclasses • Examples • Circle extends GeometricObject • Rectangle extends GeometricObject • GeometricObject extends Object • Example • Time extends Date • Date extends Object • Example • JOptionPane extends JComponent • JComponent extends Container • Container extends Component • Component extends Object • In each case, a new subclassextends the superclass • The subclass typically has additional data fields (instance variables) and/or additional methods

  4. class Time extends Date • Additional data fields (instance variables): • hour (int between 0 and 23) • minute (int between 0 and 59) • second (int between 0 and 59) • Additional method public boolean morning(){ return (hour < 12) }

  5. Sub or Super? • The Time class • extends the Date class • has additional data fields and methods • has more capabilities than the Date class • So why is it called sub, not super ? • The set of objects of class Time is a subset of the set of objects of class Date • The set of objects of class Date is a superset of the set of objects of class Time

  6. Why Inheritance? • Code re-use • By extending an exisiting class, we can inherit its methods as well as add new capabilities

  7. Subclass Methods • Subclass can inherit methods of the superclass. This happens by default. • Example: Time inherits dayOfWeek from Date • Subclass can override methods of the superclass by defining a method with the same signature (reminder: what is this?) • Example: Time should override Date’s toString • Subclass can provide additional methods not in the superclass • Example: Time provides morning

  8. Subclass Data Fields • Subclass can inherit data fields of the superclass. This happens by default, unless they are private, which is typically a good idea – then they are only accessed by methods of the superclass. • Example: year, month, day • Subclass can shadow data fields of the superclass. This happens when new data fields with the same name are used. Typically not advisable. • Subclass can define new data fields. • Example: hour, minute, second.

  9. Inheritance Chains • Suppose C extends B and B extends A • Then we say A is part of C’s inheritance chain, and that C is descended from A • We also say (somewhat confusingly) that C is a subclass of A • We can be more specific by saying that C is an immediate subclass of B and B is an immediate subclass of A • Likewise A is theimmediate superclass of B and B is theimmediate superclass of A • Notice that a class only has one immediate superclass, but it could have many immediate subclasses.

  10. Access Control • public: available to all methods in all classes • package (default): available to methods in classes that are in the same package • protected: available to methods in the same class and all of its subclasses • private: available only to methods in the same class. Usually the right choice for all data fields and perhaps for some methods.

  11. The Object Cosmic Superclass • Every class is descended from the Object class • Thus, every class can call or override the methods in the Object class (except any private ones) • Date overrides Object’s toString • Time overrides Date’s toString

  12. The Super Keyword • Similar usage to this • Used in constructor for the subclass to call the constructor for the (immediate) superclass. • Must be the first statement in the constructor. • If omitted, there is an understood constructor call super() • If there isn’t any “no-arg” constructor in the superclass, this will give an error. • Constructor chaining: constructor for C calls constructor for immediate superclass B which calls constructor for immediate superclass A, etc. • super.super.p() does not work!

  13. The Super Keyword, continued • Super can also be used to call methods from the superclass: for example, Time.toString might call Date.toString via super.toString() • If forget to write super, what happens? • If write Date.toString(), what happens? • Super can also be used to access data fields in the (immediate) superclass, as long as they are not private. But this is usually not necessary, unless the data field is shadowed.

  14. Overriding vs. Overloading • When we have several methods with different signatures, this is called overloading • When a method in one class has the same signature as the method in a superclass, this is called overriding • Class A might have 2 methods (maybe constructors) with the same name and different signatures, so the method is overloaded, but its subclass B might override only one of them.

More Related