class18 Notes

class18 Notes - 1 1 IST 240 Thu (3/20) Chapter 8:...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 IST 240 Thu (3/20) Chapter 8: Inheritance Hwk #8: Ex 8.3, 8.4, 8.8 Due: Tue 3/25 11:59pm For Ex 8.8: you can make up whatever parent/child classes you want. One possibility is: Parent class: Car Child class: SportsCar Hwk #7: Ex 7.1, 7.3, 7.5, PP 7.1 Due: Sun 3/23 11:59pm Tue (3/25) Teamwork: Implementation Thu (3/27) Review Ch 5-8 Chapter 9: Polymorphism & Interfaces Tue (4/1) Exam #2: Ch 5-8 2 8.1 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing class is called the parent class, or superclass , or base class The derived class is called the child class or subclass As the name implies, the child inherits characteristics of the parent That is, the child class inherits the methods and data defined by the parent class 2 3 8.1 Inheritance Inheritance relationships are shown in a UML class diagram using a solid arrow with an unfilled triangular arrowhead pointing to the parent class Proper inheritance creates an is-a relationship, meaning the child is a more specific version of the parent 4 8.1 Inheritance A programmer can tailor a derived class as needed by adding new variables or methods, or by modifying the inherited ones Software reuse is a fundamental benefit of inheritance By using existing software components to create new ones, we capitalize on all the effort that went into the design, implementation, and testing of the existing software 3 5 Consider: Vehicle Hierarchy (in UML) A superclass can have multiple subclasses . Subclasses can be superclasses of other subclasses . A subclass can inherit directly from only one superclass . I have 1 superclass and 2 subclasses! 6 Motivation All vehicles will have similar methods: move() getLocation() setSpeed() isBroken() A lot of this code is common to all types of vehicles It would be a waste to have to write separate move() methods for each vehicle type What if you then have to change one you would then have to change dozens of methods We want a means to specify one move() method, and have each vehicle type inherit that code Then, if we have to change it, we only have to change one copy 4 7 Specifying Inheritance The syntax for defining a subclass is to use the extends keyword in the class header, as in accessModifier class SubclassName extends SuperclassName { // class definition } public class Truck extends Vehicle { // class definition } The superclass name specified after the extends keyword is called the direct superclass. Recall: public class DieGUI extends JFrame 8 8.1 The protected Modifier Visibility modifiers affect the way that class members can be used in a child class Why not private?...
View Full Document

Page1 / 16

class18 Notes - 1 1 IST 240 Thu (3/20) Chapter 8:...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online