Inheritance is extending the capabilities and functionality of a class | |||||||
Inheritance allows you to build a hierarchy of classes, with parents (called superclasses) and children (called subclasses) | |||||||
A superclass has one or more extensions - subclasses that are built upon the superclass | |||||||
All classes in Java are subclasses of java.lang.Object | |||||||
Once a superclass is defined, use the extends keyword to define a subclass. | |||||||
Example - the Box superclass -- public class Box | |||||||
The LockableBox subclass -- public class LockableBox extends Box | |||||||
LockableBox objects have the member variables and methodsfrom Box as well as it's own member variables and methods | |||||||
Use the super keyword to reference methods and members from the superclass -- | |||||||
public LockableBox (String password) | |||||||
Overriding Methods - | |||||||
Example - consider the setOpen method -
|
Download the LockableBox source code example - pay cloase attention to the definition of LockableBox.java and Box.java
|
Last Updated by DrB on Wednesday, February 18, 2009