1 / 15

Inheritance

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

Download Presentation

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. Inheritance Chapter 11 in Gaddis

  2. 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-aflower • 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.

  3. 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.

  4. General Superclass Base class Specific Subclass Derived class Inheritance terminology

  5. Super class elements inherited/not inherited by subclass • Inherited • Public fields • Public methods • Accessors • Mutators • Not inherited • Constructors • Private fields • Private methods

  6. 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.

  7. 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

  8. 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();

  9. 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

  10. Important • When a subclass extends a superclass, the public members of the superclass become public members of the subclass.

  11. Important • Inheritance is a one-way relationship. • Subclasses inherit from superclasses. • Superclasses do not inherit from subclasses.

  12. 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

  13. 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.

  14. How to prevent overriding • A method in the superclass with the modifier finalcan not be overridden.

  15. More on Inheritance • AFTER the exam

More Related