Download presentation
Presentation is loading. Please wait.
Published byCalvin Weaver Modified over 9 years ago
1
AP Computer Science A – Healdsburg High School 1 Inheritance - What is inheritance? - “Is-a” vs. “Has-a” relationship - Programming example “CircleBug”
2
AP Computer Science A – Healdsburg High School 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
AP Computer Science A – Healdsburg High School 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
AP Computer Science A – Healdsburg High School 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
AP Computer Science A – Healdsburg High School 5 Example: Inheritance Diagram Mammal Dog Border Collie Cat Base Class Subclasses of Mammal
6
AP Computer Science A – Healdsburg High School 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
AP Computer Science A – Healdsburg High School 7 Important Concept in OOP!!! “Is-a” vs. “Has-a” Inheritance Instance variables (private data) of a class
8
AP Computer Science A – Healdsburg High School 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 ? Bug CircleBug Inheritance Diagram
9
AP Computer Science A – Healdsburg High School 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. BankAccount SavingsAccountCheckingAccount Inheritance Diagram
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.