slides created by Alyssa Harding

Slides:



Advertisements
Similar presentations
Inheritance Part I. Lecture Objectives To learn about inheritance To understand how to inherit and override superclass methods To be able to invoke superclass.
Advertisements

GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
CS221 - Computer Science II Polymorphism 1 Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
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.
Chapter 4 Objects and Classes.
NSIT,Jetalpur CORE JAVA CONCEPTS SURABHI MISHRA (LCE)NSIT.
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.
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.
Lesson 27: Introduction to the Java GUI. // helloworldbutton.java import java.awt.*; import javax.swing.*; class HelloButton{ public static void main.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Inheritance (Part 4) Abstract Classes 1.  sometimes you will find that you want the API for a base class to have a method that the base class cannot.
28-Dec-04polymorhism.ppt1 Polymorphism. 28-Dec-04polymorhism.ppt2 signatures in any programming language, a signature is what distinguishes one function.
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.
Jens Dalsgaard Nielsen Jan Dimon Bendtsen Dept. of Electronic Systems Basic Programming INS-basis GF, PDP and HST.
Programming in Java CSCI-2220 Object Oriented Programming.
 GUI – Graphic User Interface  Up to now in the programs we have written all output has been sent to the standard output device i.e.: the DOS console.
Static Attributes and Inheritance  static attributes behave the same as non-static attributes in inheritance  public and protected static attributes.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CIS Intro to JAVA Lecture Notes Set 8 9-June-05.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
Review – Primitive Data What is primitive data? What are 3 examples of primitive data types? How is a piece of primitive data different from an object?
Introducing, the JFrame Gives us a work area beside System.out.println.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Topic: Classes and Objects
Building Java Programs
Lecture 14 Throwing Custom Exceptions
Lecture 12 Inheritance.
Chapter 3: Using Methods, Classes, and Objects
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Week 8 Lecture -3 Inheritance and Polymorphism
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
Polymorphism 11-Nov-18.
Polymorphism and access control
slides created by Ethan Apter
Building Java Programs
Inherited Classes in Java
Building Java Programs
slides created by Ethan Apter
CS2011 Introduction to Programming I Objects and Classes
slides created by Ethan Apter
Core Concepts.
slides created by Alyssa Harding
CSE 143 Lecture 5 References and Linked Nodes
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Programming with inheritance Based on slides by Alyssa Harding
CSE 143 Lecture 2 ArrayIntList, binary search
slides created by Ethan Apter
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
slides created by Ethan Apter and Marty Stepp
Binary Search Trees Based on slides by Marty Stepp & Alyssa Harding
Visibilities and Static-ness
Parameters, Overloading Methods, and Random Garbage
Presentation transcript:

slides created by Alyssa Harding CSE 143 Lecture 21 Inheritance, static slides created by Alyssa Harding http://www.cs.washington.edu/143/

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

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”]

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

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 We want a Point that keeps track of how many times it has been translated: import java.awt.*; 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 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); }

Graphics We can also override other methods in the class: public void paint(Graphics g) { System.out.println("in paint"); } This will be called whenever the frame needs to be redrawn on the screen Play around with more methods!

What does static mean? Why static in public static void main? You can think of static as meaning that there is only one copy per class. A static method is also known as a class method. A non-static method is also known as an instance method.

What does static mean? public class Counter { public static int classCounter; public int instanceCounter; public Counter() { classCounter++; instanceCounter++; } public static void staticIncrement() { classCounter++; //instanceCounter++; // illegal! Which instanceCounter would we increment? } public void instanceIncrement() { classCounter++; instanceCounter++; } public void print(String msg) { System.out.println(msg); System.out.println("class = " + classCounter); System.out.println("instance = " + instanceCounter); System.out.println(); } public static void main(String[] args) { Counter a = new Counter(); a.print("a created"); Counter b = new Counter(); b.print("b created"); Counter.staticIncrement(); //Counter.instanceIncrement(); // illegal! Which instanceIncrement() would we call? a.print("after static increment"); a.instanceIncrement(); a.print("a instance increment"); b.print("b print"); } }