L9 - CSE 114 Computer Science I Inheritance Yosemite...

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

View Full Document Right Arrow Icon
CSE 114 – Computer Science I Inheritance 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” – also called aggregation PairOfDice HAS-A Die
Background image of page 2
Inheritance • One class can be 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 3

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 4
And the real reason? • Inheritance is powerful • Why? • Code written today can call methods written 10 years from now – Huh? • That’s due to inheritance & polymorphism, more on this next lecture
Background image of page 5

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

View Full DocumentRight Arrow Icon
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 6
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 7

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

View Full DocumentRight Arrow Icon
Example: Parent Class: Person public class Person { private String name; private int age; // constructor public Person(String initName) { age = 0; // just born name = initName; } // accessor method public String getName() { return name; } // accessor method public int getAge() { return age; } // mutator method public void setAge(int newAge) { if (newAge < 0) age = 0; else age = newAge; } }
Background image of page 8
Example: Child (Sub-)Class: Student public class Student extends Person
Background image of page 9

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

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

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

Page1 / 24

L9 - CSE 114 Computer Science I Inheritance Yosemite...

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

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