Inheritance Chapter 11 in Gaddis
Is a relationships in ‘real’ life Exist when one object is a specialized version of another one –Examples An english setter is-a dog A jet is-a motorized plane A rose is-a flower A card game is-a game The specialized object has all of the characteristics of the general object plus additional characteristics that make it special.
Is-a relationships in OO Programming Are created using inheritance Inheritance is used to create is-a relationships among classes. Java does not allow what is called multiple inheritance.
Inheritance terminology General Superclass Base class Specific Subclass Derived class
Super class elements inherited/not inherited by subclass Inherited –Public fields –Public methods Accessors Mutators Not inherited –Constructors –Private fields –Private methods
Important re: constructors Superclass constructors are not inherited. Superclass constructors create superclass objects In an inheritance relationship, the superclass constructor always executes before the subclass constructor. It happens automatically when the superclass has a no-arg constructor or a default constructor.
Important re: constructors What if superclass has –Programmer-defined 1 argument constructor –Programmer-defined 2 argument constructor –Programmer-defined multi- argument constructor –All of the above Subclass uses the keyword super to call the desired superclass constructor
Constructor Guidelines The super statement can only appear in the subclass’s constructor The super statement must be the first statement in the subclass’s constructor IF the subclass constructor does not explicitly call the superclass’s constructor, Java will automatically call the superclasses no-arg or default constructor just before the subclass constructor executes. –Equivalent to super();
Two more important points If a superclass does not have a no-arg constructor but does have a programmer-defined constructor, the subclass must call it or a subclass compile error will occur. Subclass header statement must indicate what its superclass is using the reserved word extends
Important When a subclass extends a superclass, the public members of the superclass become public members of the subclass.
Important Inheritance is a one-way relationship. Subclasses inherit from superclasses. Superclasses do not inherit from subclasses.
Terminology review Overriding vs Overloading –Overloaded methods have the same name as one or more methods but a different parameter list. –Overridden methods have the same signature
Overriding and Overloading Both can take place in an inheritance relationship. Overloaded methods can occur: –Within the same class –A subclass can overload a method in a superclass Overridden methods can only occur in the inheritance relationship –A subclass can not override another method in the same class –A subclass can override a method in its superclass.
How to prevent overriding A method in the superclass with the modifier final can not be overridden.
More on Inheritance AFTER the exam