Programming with inheritance Based on slides by Alyssa Harding

Slides:



Advertisements
Similar presentations
18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings  The LineList data type r Recursive methods to construct line drawings.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Week 8 Recap CSE 115 Spring Composite Revisited Once we create a composite object, it can itself refer to composites. Once we create a composite.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
AP Computer Science.  Not necessary but good programming practice in Java  When you override a super class method notation.
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Inheritance using Java
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Parameters… Classes Cont Mrs. C. Furman October 13, 2008.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Programming in Java CSCI-2220 Object Oriented Programming.
Creating Windows. How can we use Java to create programs that use windows (GUI applications)? How can we use Java to create programs that use windows.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Session 7 Introduction to Inheritance. Accumulator Example a simple calculator app classes needed: –AdderApp - contains main –AddingFrame - GUI –CloseableFrame.
Subclassing, pt. 2 Method overriding, virtual methods, abstract classes/methods COMP 401, Fall 2014 Lecture 9 9/16/2014.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
1 Review for Midterm 2 Aaron Bloomfield CS 101-E.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Polymorphism and access control. RHS – SOC 2 What is polymorphism? In general: the ability of some concept to take on different forms In Java: the ability.
Lecture 5:Interfaces and Abstract Classes
Object-Oriented Concepts
Building Java Programs
Lecture 12 Inheritance.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
University of Central Florida COP 3330 Object Oriented Programming
Polymorphism 2nd Lecture
Agenda Warmup AP Exam Review: Litvin A2
Lecture 9-2: Interacting with the Superclass (super);
University of Central Florida COP 3330 Object Oriented Programming
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Polymorphism 2nd Lecture
Lecture 22 Inheritance Richard Gesick.
Polymorphism and access control
slides created by Ethan Apter
Inherited Classes in Java
slides created by Alyssa Harding
slides created by Ethan Apter
slides created by Ethan Apter
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
Chapter 4 Constructors Section 4.4
slides created by Ethan Apter and Marty Stepp
Summary.
Parameters, Overloading Methods, and Random Garbage
CIS 110: Introduction to computer programming
Presentation transcript:

Programming with inheritance Based on slides by Alyssa Harding CSE 143 Lecture 25 Programming with inheritance Based on slides by Alyssa Harding

Inheritance Key feature of object-oriented programming Very powerful tool As with most powerful tools, have to learn the details of how to use them correctly. Before midterm we saw the mechanics of how inheritance works We remember some things about extending classes, super calls, and inherited methods, ...right? Today we will look at more examples and more details about how to use inheritance correctly

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(“why”); s.add(“hello”); s.add(“there”); System.out.println(s); outputs: [“why”, “why”, “hello”, ”hello”, “there, “there”]

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 of ArrayList<E>

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> { }

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); return true; } Instead of worrying about the details, we can use the super class’s add method

Example: TranslatePoint Or maybe we want a Point that keeps track of how many times it has been translated: public class TranslatePoint extends Point { private int count; } We need a field to keep track of our new state, but we rely on the Point class to keep track of the regular state

Example: TranslatePoint We then need to override the translate method to update our new state while still keeping the old functionality: public void translate(int x, int y) { count++; super.translate(x,y); } We need to make sure we call the super class’ method, otherwise we would have infinite recursion!

Example: TranslatePoint We can also add more functionality to the class: public int getTranslateCount() { return count; }

Example: TranslatePoint We still need to think about constructors. Constructors are not inherited like the rest of the methods. Even when we don’t specify one, Java automatically includes an empty, zero-argument constructor: public TranslatePoint() { // do nothing } But it doesn’t actually do nothing...

Example: TranslatePoint Java needs to construct a TranslatePoint, so it at least needs to construct a Point It automatically includes a call on the super class’ constructor: public TranslatePoint() { super(); } We can use the super() notation to call the super class’ constructor just like we use the this() notation to call this class’ constructor

Example: TranslatePoint But we want to be able to specify coordinates, so we will need to make a constructor The first thing we need to do is include a call on the super class’ constructor: public TranslatePoint(int x, int y) { super(x,y); count = 0; }

Example: TranslatePoint However, now that we have a constructor, Java won’t automatically give us a zero-argument constructor Since we still want one, we have to explicitly program it: public TranslatePoint() { this(0,0); }

Inheritance Recap Fields. What additional information do you need to keep track of? Constructors. If you want a constructor that takes parameters, you have to create one and call the super class’ constructor in it. Overridden methods. What methods affect the new state? These need to be overridden. Again, call the super method when you need to accomplish the old task. Added methods. What new behavior does your class need?

Graphics Another useful application: graphical user interfaces (GUIs) Think of all the different programs on your computer. You wouldn’t want to code each type of window, textbox, scrollbar individually! Java includes basic graphical classes that we can extend to add more functionality Here’s the API documentation for a frame: http://java.sun.com/javase/6/docs/api/java/awt/Frame.html

Graphics We can use this to customize our own type of frame: public class FunFrame extends Frame { public FunFrame() { setVisible(true); setSize(400, 400); setTitle("Such fun!"); setBackground(Color.PINK); }