Lec06_Inheritance_2per

Lec06_Inheritance_2per - 1 EECS EECS EECS EECS EECS EECS...

Info iconThis preview shows pages 1–4. 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 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 EECS285 Lecture 06 Inheritance Van der Linden Ch. 8 (first half) EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 2 Andrew M. Morgan Inheritance Inheritance is one of the basic properties of an object-oriented language Java allows inheritance (since it is considered OO) Inheritance allows one or more classes to obtain properties and functionality that is defined in another class New properties and/or functionality can be added to this inherited functionality A "base class" is a high-level class that contains attributes that all inherited classes have A "derived class" is a lower-level class which gets some of its attributes from the base class, and adds others Java uses keyword "extends" to indicate inheritance Subclass "extends" the generic nature of the base to a more specialized subclass 2 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 3 Andrew M. Morgan "Is-A" Relationship Use of inheritance results in "is-a" relationships A derived class "is-a" parent class, just a specific type Example: A base class might be named "ModeOfTransportation" A derived class might be named "Airplane" It is true that an Airplane"is-a" ModeOfTransportation All subclasses of ModeOfTransportation (Airplane, Car, Truck, Bicycle) have common attributes and functionality, specified in ModeOfTransportation Subclasses can have attributes and functionality specific to that subclass, that don't apply to other subclasses Example: The Airplane class might have an altitude attribute, that a Car would not (hopefully) EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 4 Andrew M. Morgan "Is-A" vs "Has-A" Relationship An attribute of a class constitutes a "has-a" relationship The Person class might have an attribute "name" The Person "has-a" name Design carefully maintaining the correct relationship types It would not make sense for a ModeOfTransportation to "have-a" Airplane Similarly it would not make sense to say a name "is-a" Person 3 EECS EECS EECS EECS EECS EECS EECS EECS 285 285 285 285 285 285 285 285 5 Andrew M. Morgan Inheritance, Motivation public class RotatablePartClass { MaterialEnum material = MaterialEnum.STEEL; String name = "Unknown"; double rotAngle = 0; void setMaterial(final MaterialEnum inMat) { material = inMat; } void setName(final String inName) { name = inName; } void setRotAngle(final double inAng) { rotAngle = inAng; } } public class RemoveablePartClass { MaterialEnum material = MaterialEnum.STEEL; String name = "Unknown"; boolean isRemoved = false; void setMaterial(final MaterialEnum inMat) { material = inMat; } void setName(final String inName) { name = inName; } void togglePart() { isRemoved = !isRemoved; } } All part types share some common information that could be grouped together Create an inheritance tree, with base class containing this...
View Full Document

Page1 / 10

Lec06_Inheritance_2per - 1 EECS EECS EECS EECS EECS EECS...

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

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