140 likes | 329 Views
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.
E N D
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. • 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)
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
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) }
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
Why Inheritance? • Code re-use • By extending an exisiting class, we can inherit its methods as well as add new capabilities
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
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.
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.
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.
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
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!
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.
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.