Inheritance - What is inheritance? - - PowerPoint PPT Presentation

About This Presentation
Title:

Inheritance - What is inheritance? -

Description:

... 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 ... – PowerPoint PPT presentation

Number of Views:218
Avg rating:3.0/5.0
Slides: 10
Provided by: RM26
Learn more at: http://www.oocities.org
Category:

less

Transcript and Presenter's Notes

Title: Inheritance - What is inheritance? -


1
Inheritance- What is inheritance?- Is-a vs.
Has-a relationship- Programming
exampleCircleBug
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
Write a Comment
User Comments (0)
About PowerShow.com