11-inheritance-polymorphism

11-inheritance-polymorphism - CSE 114 Computer Science I...

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

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Inheritance, Polymorphism, and UML Yosemite National Park, California
Background image of page 1

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

View Full DocumentRight Arrow Icon
Containment • A class contains another class if it instantiates an object of that class – “HAS-A” LinkedList HAS-A ListNode CAR HAS-A Engine
Background image of page 2
Cats and Dogs • Cat – Is a mammal – Has fur – Common house pet – Has several breeds – Meows – Scratches the furniture – Catches mice • Dog – Is a mammal – Has fur – Common house pet – Has several breeds – Barks – Catches Frisbees – Chases squirrels – Can guard a house
Background image of page 3

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

View Full DocumentRight Arrow Icon
Cats and Dogs • Some classes share properties that are similar • For example, cats and dogs are both mammals and common house pets • It doesn’t make sense to duplicate shared properties unnecessarily • Instead, we can abstract out common characteristics
Background image of page 4
Cats and Dogs
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Inheritance • Inheritance A mechanism by which one class acquires (inherits) the properties (both data fields and methods) of another class. • Superclass The class being inherited from • Derived class The class that inherits • The derived class is specialized by adding properties specific to it
Background image of page 6
Inheritance • An object class that is derived from another class inherits its instance variables and methods. child class -----> parent class “IS-A” subclass class derived class base class • Any instance variable or method that is inherited by a subclass does not need to be redefined in the subclass.
Background image of page 7

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

View Full DocumentRight Arrow Icon
Why use Inheritance? • Customize classes (from the JDK or your own) • Benefits: – Don’t have to re-write code • Abstraction – the JDK has many classes to customize, especially for GUIs • Use methods and variables of fully tested classes • Code in super classes can be used by a limitless number of subclasses – Making changes to common properties is easier – just change the parent class
Background image of page 8
Inheritance Syntax public class ChildClass extends ParentClass { // instance variables for Child only // methods for Child only } ChildClass now contains all instance variables and methods defined above, as well as those defined inside ParentClass
Background image of page 9

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

View Full DocumentRight Arrow Icon
Sample Inheritance Code public class Mammal { boolean hasFur = true; public void makeSound(){} } public class Cat extends Mammal { boolean hasClaws; String furColor; public void makeSound() { System.out.println(“Meow!”); } }
Background image of page 10
Sample Inheritance Code II public class Persian extends Cat { String furColor = “White”; public void makeSound() { System.out.println(“Hisss!”); } } The Persian class inherits hasClaws from the Cat parent class. It overrides the makeSound() method, and hides the furColor field from Cat .
Background image of page 11

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

View Full DocumentRight Arrow Icon
How to organize classes using Inheritance • Determine what data you need to store. For example, for storing student and employee data: – Student data: name, age, GPA – Employee data: name, age, salary • Divide up your classes according to state – since Students and Employees store different data, use separate classes • Pool common data into a common parent class – Person data: name, age • Have Student and Employee classes customize Person
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2008 for the course CSE 114 taught by Professor Tashbook during the Spring '08 term at SUNY Stony Brook.

Page1 / 58

11-inheritance-polymorphism - CSE 114 Computer Science I...

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

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