CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Similar documents
CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 FALL2017. Lecture 4: The class hierarchy; static components

Lecture 4: The class hierarchy; static components

CS/ENGRD 2110 SPRING Lecture 3: Fields, getters and setters, constructors, testing

CS/ENGRD 2110 FALL Lecture 3: Fields, getters and setters, constructors, testing

CS/ENGRD 2110 SPRING Lecture 5: Local vars; Inside-out rule; constructors

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

Programming Languages and Techniques (CIS120)

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

CS/ENGRD 2110 SPRING 2018

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Programming Languages and Techniques (CIS120)

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Instance Members and Static Members

Inheritance: Definition

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

Programming Languages and Techniques (CIS120)

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Example: Count of Points

Programming Languages and Techniques (CIS120)

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

CS/ENGRD 2110 SPRING Lecture 2: Objects and classes in Java

Java Fundamentals (II)

Java Object Oriented Design. CSC207 Fall 2014

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Inheritance and Interfaces

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Inheritance (Part 5) Odds and ends

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

CS/ENGRD 2110 FALL Lecture 5: Local vars; Inside-out rule; constructors

CSE 113 A. Announcements - Lab

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Introduction to Inheritance

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

Programming Languages and Techniques (CIS120)

Object Oriented Programming. Week 1 Part 3 Writing Java with Eclipse and JUnit

Inheritance and Polymorphism

Announcements for the Class

RECURSION (CONTINUED)

Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

Computer Science 210: Data Structures

Example: Count of Points

1.00 Lecture 13. Inheritance

INHERITANCE. Spring 2019

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

CSE wi Midterm Exam 2/8/18. Name UW ID #

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

ITI Introduction to Computing II

Inheritance (Part 2) Notes Chapter 6

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

Inheritance. Inheritance

ITI Introduction to Computing II

CS/ENGRD 2110 SPRING 2018

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

ITI Introduction to Computing II

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

More about inheritance

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java Magistère BFA

Inheritance (P1 2006/2007)

Inheritance (Extends) Overriding methods IS-A Vs. HAS-A Polymorphism. superclass. is-a. subclass

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Big software. code reuse: The practice of writing program code once and using it in many contexts.

CS 100J Prelim 2 Have a good break!!! 15 March 2007

Programming Languages and Techniques (CIS120)

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

The class Object. Lecture CS1122 Summer 2008

Inheritance and Interfaces

Accelerating Information Technology Innovation

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

(a) in assignment statements. (b) when invoking methods (with objects as parameters) (d) equality of references versus equality of objects

Exam Duration: 2hrs and 30min Software Design

C a; C b; C e; int c;

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

CSE wi Midterm Exam 2/8/18 Sample Solution

COMP 250 Fall inheritance Nov. 17, 2017

Abstract Classes and Interfaces

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

1.00 Lecture 8. Using An Existing Class, cont.

public static boolean isoutside(int min, int max, int value)

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Intro to Computer Science 2. Inheritance

The software crisis. code reuse: The practice of writing program code once and using it in many contexts.

Solutions to Quiz 1 (March 14, 2016)

CSE 143 Au04 Midterm 1 Page 1 of 9

Polymorphism. return a.doublevalue() + b.doublevalue();

Transcription:

1 CS/ENGRD 2110 SPRING 2019 Lecture 4: The class hierarchy; static components http://cs.cornell.edu/courses/cs2110

Announcements 2 We're pleased with how many people are already working on A1, as evidenced by Piazza activity! Please be sure to look at Piazza note @10 every day for any updates. Also search existing questions! Groups: Forming a group of two? Do it well before you submit at least one day before. Both members must act: one invites, the other accepts. Thereafter, only one member has to submit the files. If one of you submits before forming the group, the course staff will have to do extra work, and you ll receive a small penalty of 4 points. Reminder: groups must complete the assignment working together.

Big ideas so far 3 Java variables have types (L1) A type is a set of values and operations on them (int: +, -, *, /, %, etc.) Classes define new types (L2) Methods are the operations on objects of that class. Fields allow objects to store data (L3) A software engineering principle: give user access to functionality, not the implementation details

Review: Method specs should not mention fields 4 public class Time { private int hr; //in 0..23 private int min; //in 0..59 /** return hour of day*/ public int gethour() { return hr; Time@fa8 hr 9 min 5 sethour(int) Time gethour() getmin() tostring() Decide to change implemen -tation public class Time { // min, in 0..23*60+59 private int min; /** return hour of day*/ public int gethour() { return min / 60; Time@fa8 min 545 Time gethour() getmin() tostring() sethour(int) Specs of methods stay the same. Implementations, including fields, change!

Today s topics 5 Class Object Extends, is-a Method tostring(), object names, overriding Keyword this, shadowing Static components

Running example: Class W (for Worker) 6 /** Constructor: worker with last name n, SSN s, boss b (null if none). Prec: n not null, s in 0..999999999 with no leading zeros.*/ public W(String n, int s, W b) /** = worker's last name */ public String getlname() /** = last 4 SSN digits */ public String getssn() /** = worker's boss (null if none) */ public W getboss() /** Set boss to b */ public void setboss(w b) Contains other methods! W@af lname Pollack W ssn 123456789 boss null W( ) getlname() getssn() getboss() setboss(w) tostring() equals(object) hashcode()

Class Object 7 Java: Every class that does not extend another extends class Object. That is, public class W { is equivalent to public class W extends Object { We often omit this partition to reduce clutter; we know that it is always there. We draw object like this: W@af tostring() Object equals(object) hashcode() lname Pollack W ssn 123456789 boss null W( ) getlname() getssn(), getboss() setboss(w)

Extends: Is A 8 Extension should reflect semantic data model: meaning in real world A should extend B if and only if A is a B An elephant is an animal, so Elephant extends Animal A car is a vehicle, so Car extends Vehicle An instance of any class is an object, so AnyClass extends java.lang.object

Extends: Is A 9 Which of the following seem like reasonable designs? A. Triangle extends Shape { B. PhDTester extends PhD { C. BankAccount extends CheckingAccount {

Extends: Is A 10 Which of the following seem like reasonable designs? A. Triangle extends Shape { A. Yes! A triangle is a kind of shape. B. PhDTester extends PhD { A. No! A PhDTester tests a PhD, but itself is not a PhD. C. BankAccount extends CheckingAccount { A. No! A checking account is a kind of bank account; we likely would prefer: CheckingAccount extends BankAccount {...

Investigate: JFrame 11 1. How many levels deep is JFrame in the class hierarchy? (Object is JFrame s super-super- -superclass. How many supers are there?) 2. In which class is JFrame s getheight() method defined? (hint: it s not JFrame!)

What s in a name? 12 The name of the object below is PhD@aa11bb24 The name is <class> @ <address in memory>. Variable e, declared as PhD e; contains not the object but the name of the object (i.e., it is a reference to the object). e PhD@aa11bb24 PhD PhD@aa11bb24 PhD name Gries ad1 null ad2 null advisees 17

Method tostring() 13 tostring() in Object returns the name of the object: W@af Java Convention: Define tostring() in any class to return a representation of an object, giving info about the values in its fields. New definitions of tostring() override the definition in Object.toString() In appropriate places, the expression e automatically does e.tostring() e.tostring() calls this method e W@af W@af Object tostring() W lname Pollack ssn 123456789 boss null getssn() tostring()

Method tostring() 14 tostring() in Object returns the name of the object: W@af public class W { /** Return a representation of this object */ public String tostring() { return Worker + lname + has SSN???-??- + getssn() + (boss == null conditional expression? : and boss + boss.lname); e.tostring() calls this method e W@af W@af Object tostring() W lname Pollack ssn 123456789 boss null getssn() tostring()

Another example of tostring() 15 /** An instance represents a point (x, y) in the plane */ public class Point { Point@fa8 private int x; // x-coordinate private int y; // y-coordinate Point x 9 y 5 /** = repr. of this point in form (x, y) */ public String tostring() { return ( + x +, + y + ) ; Function tostring should give the values in the fields in a format that makes sense for the class. (9, 5)

this: the object s own name 16 this keyword: this evaluates to the name of the object in which it occurs Makes it possible for an object to access its own name Example: a shadowed class field public class Point { public int x= 0; public int y= 0; public class Point { public int x= 0; public int y= 0; public Point(int x, int y) { x= x; y= y; public Point(int x, int y) { this.x= x; this.y= y;

Static components 17 /** = this object is c s boss. Pre: c is not null. */ public boolean isboss(w c) { return this == c.boss; Spec: return the value of that true-false sentence. True if this object is c s boss, false otherwise keyword this evaluates to the name of the object in which it appears x W@b4 W@b4 W lname Jo boss W@af isboss(w c) { return this == c.boss; x.isboss(y) is false y.isboss(x) is true y W@af W@af W lname Om boss null isboss(w c) {

Static components 18 /** = b is c s boss. Pre: b and c are not null. */ public boolean isboss(w b, W c) { return b == c.getboss(); /** = this object is c s boss. Pre: c is not null. */ public boolean isboss(w c) { return this == c.boss; Body doesn t refer to any field or method in the object. Why put method in object? x W@b4 W@b4 W lname Jo boss W@af ssn 21 isboss(w) isboss(w,w) y W@af W@af W lname Om boss null ssn 35 isboss(w) isboss(w,w)

19 Static components static: there is only one copy of the method. It is /** = b is c s boss. not in each object Pre: b and c are not null. */ public static boolean isboss(w b, W c) { return b == c.getboss(); Box for W (objects, static components) x.isboss(x, y) y.isboss(x, y) Preferred: W.isBoss(x, y) x W@b4 y W@af W@b4 W@af W W lname Jo lname Om boss W@af ssn 21 boss null ssn 35 isboss(w) isboss(w) isboss(w,w)

Good example of static methods 20 java.lang.math http://docs.oracle.com/javase/8/docs/api/java/lang/math.html (Or find it by googling java.lang.math 8)

A use for static fields (aka class variables): Maintain info about created objects 21 public class W { private static int numobs; // number of W objects created /** Constructor: */ public W( ) { numobs= numobs + 1; W@bd W@12 To have numobs contain the number of objects of class W that have been created, simply increment it in constructors. lname Ra W W lname Kn numobs 2 Box for W

Class java.awt.color uses static fields 22 An instance of class Color describes a color in the RGB (Red-Green- Blue) color space. The class contains about 20 static variables, each of which is (i.e. contains a pointer to) a non-changeable Color object for a given color: public static final Color black= ; public static final Color blue= ; public static final Color cyan= new Color(0, 255, 255); public static final Color darkgray= ; public static final Color gray= ; public static final Color green= ;

Java application 23 Java application: a program with at least one class that has this procedure: public static void main(string[] args) { Type String[]: array of elements of type String. We will discuss later Running the application effectively calls method main Command line arguments can be entered with args

24 Uses of static fields: Implement the Singleton pattern Only one Singleton can ever exist. public class Singleton { private static final Singleton instance= new Singleton(); private Singleton() { //... constructor public static Singleton getinstance() { return instance; //... methods Singleton@x3k3 instance Singleton Singleton@x3k3 Box for Singleton

Looking ahead: Recitation 3 25 No prework! Concentrate on A1 this weekend TA teaches testing; you test a class using Junit You can work in groups of up to 3; form a CMS group before submitting You will find faults in the class (fun!) and fix them Upload to CMS when done Hopefully during recitation If not, on/by Friday