1 / 9

Inheritance

Inheritance - What is inheritance? - “ Is-a ” vs. “ Has-a ” relationship - Programming example “CircleBug”. Inheritance. A class ( child class) inherits the functionality of another class ( parent class), and then adds new functionality of its own. Java supports single inheritance.

zoltin
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- What is inheritance?- “Is-a” vs. “Has-a” relationship- Programming example“CircleBug”

  2. Inheritance A class (child class) • inherits the functionality of another class (parent class), and then • adds new functionality of its own. • Java supports single inheritance. • Inheritance is a requirement of all object-oriented systems.

  3. Why inheritance? • Inheritance is a technique for reuse. • If a new class has a lot in common with a class that already exists, you can reuse parts of the existing class in the new class. • The child class is defined by extending the parent class. • The new ''child'' class has all characteristics of its ''parent'' plus any it adds itself.

  4. Inheritance (an is-a relationship) • Inheritance is a parent-child relationship between classes. • The parent class is also called a base class or a superclass. • The child class is also called a derived class or subclass.

  5. Example: Inheritance Diagram Mammal Base Class Dog Cat Subclasses of Mammal Border Collie

  6. Example: In an OOP traffic simulation, you may have • the following classes: • Vehicle • Car • Truck • Sedan • Coupe • PickupTruck • SUV • Minivan • Motorcycle • Bicycle Make an inheritance diagram for the classes at the left.

  7. Important Concept in OOP!!! “Is-a” vs. “Has-a” Inheritance Instance variables (private data) of a class

  8. Example: The CircleBug A CircleBug will inherit most of the qualities of a Bug. The only difference is how this bug moves. A CircleBug acts identical to the BoxBug, except that in the act method, the turn method is called once instead of twice. How is the behavior different than a BoxBug? Should it be called a CircleBug? Inheritance Diagram Bug CircleBug

  9. Example: Create a checking account class. A checking account has all of the qualities of a BankAccount, but is assessed a fee of $2.00 if there are more than 3 transactions per month. Inheritance Diagram BankAccount SavingsAccount CheckingAccount

More Related