Lecture 8: Object Oriented Design: Inheritance
Textbook section
2.1-2.2
Object oriented design principles
- Abstraction
- give an interface of an abstract data type (ADT)
- a class implements the interface
- Encapsulation
- do not reveal the implementation details; just maintain the interface
- Modularity
- organize different components of a system into separate functional units
Inheritance
- hierarchical; uses “is-a” relationships
- we say that a subclass or child class extends a base class, parent class, or super class
- child class can augment by adding new fields and methods
- can also override an existing method
- use
super
keyword to refer to parent class - constructors are not inherited
Files used in class
Additional exercises
- Create another class,
AirlineCreditCard
, that inherits from theCreditCard
class. It should have the additional private fieldsairline
(String
type) andmiles
(double
type). It should have a constructor that calls theCreditCard
constructor using thesuper
keyword and sets the values of the two new fields. It should override thecharge
method to also add one mile per dollar spent to themiles
total. (Try to use thesuper
keyword again to call thecharge
method from the parent class.) Add agetMiles()
method and write some code inCreditCardDemo.java
to test that your new class is working.