21-inheritance - CSE143 Lecture21 Inheritance,static

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

View Full Document Right Arrow Icon
CSE 143 Lecture 21 Inheritance,  static slides created by Alyssa Harding http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Inheritance We’ve seen how the mechanics of inheritance work We seen some things about extending classes,  super calls, and inherited methods Now we’re going to see how we can program with  inheritance to make our lives easier
Background image of page 2
3 Example:  StutterList We want a class that has all the functionality  of  ArrayList  but adds everything twice For instance, the following code StutterList<String> s = new StutterList<String>(); s.add(“hello”); System.out.println(s); outputs [“hello”,”hello”]
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Example:  StutterList How would we do this? We could write an entirely new class by  copying and pasting the  ArrayList<E> code But that’s redundant We could change the  ArrayList<E>  code to  include our new functionality But this is invasive change It would ruin any code that depended on  the original functionality
Background image of page 4
5 Example:  StutterList We want  additive , not  invasive , change! Instead, we just want to add onto the  ArrayList<E> We want to extend its functionality using inheritance: public class StutterList<E> extends ArrayList<E> { }
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 Example:  StutterList Now we  override  the old  add  method to include  our stutter functionality: public class StutterList<E> extends ArrayList<E> { public boolean add(E value) { super.add(value); super.add(value); return true; } } Instead of worrying about the details, we can use  the super class’s  add  method 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

21-inheritance - CSE143 Lecture21 Inheritance,static

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

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