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

Slides:



Advertisements
Similar presentations
1 For more info: CS Sep 2008 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston to.
Advertisements

1 Inheritance Chapter 9. 2 Module Outcomes To develop a subclass from a superclass through inheritance To invoke the superclass ’ s constructors and methods.
1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
1 Review of classes and subclasses M fast through this material, since by now all have seen it in CS100 or the Java bootcamp First packages Then classes.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
CS/ENGRD 2110 FALL 2014 Lecture 3: Fields, getters and setters, constructors, testing 1.
OOP Languages: Java vs C++
CS/ENGRD 2110 SPRING 2015 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
CS1110 lecture 5 14 Sept 2010 Testing; the class Object; toString; static variables & methods Reading for this lecture: Testing with JUnit (Appendix I.2.4.
CS/ENGRD 2110 SPRING 2015 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 FALL 2014 Lecture 6: Consequence of type, casting; function equals 1.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
CS/ENGRD 2110 SPRING 2014 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
CS1110 lecture 5 8 Feb 2010 Testing; class Object; toString; static variables/methods Reading for this lecture: Testing with JUnit (Appendix I.2.4 & pp.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
CS/ENGRD 2110 FALL 2013 Lecture 4: The class hierarchy; static components 1.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
CSC 212 Object-Oriented Programming and Java Part 2.
Java for C++ Programmers A Brief Tutorial. Overview Classes and Objects Simple Program Constructors Arrays Strings Inheritance and Interfaces Exceptions.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
CS/ENGRD 2110 FALL 2015 Lecture 6: Consequence of type, casting; function equals 1.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
1 For more info: CS February 2009 In 1968, the Defense Department hired Bolt Beranek and Newman (BBN) of Boston.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
CS/ENGRD 2110 SPRING 2016 Lecture 4: The class hierarchy; static components 1.
CS/ENGRD 2110 SPRING 2016 Lecture 6: Consequence of type, casting; function equals 1.
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Topic: Classes and Objects
Lecture 10 Review of classes
Yanal Alahmad Java Workshop Yanal Alahmad
Java Primer 1: Types, Classes and Operators
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
Sorry these slides weren’t available last evening!
The fattest knight at King Arthur's round table was Sir Cumference
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
CS 302 Week 11 Jim Williams, PhD.
CS/ENGRD 2110 Spring 2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall2017 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CSC 113 Tutorial QUIZ I.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Spring 2016 Lecture 5: Local vars; Inside-out rule; constructors
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
CS/ENGRD 2110 Fall 2018 Lecture 4: The class hierarchy; static components
CS/ENGRD 2110 Fall 2018 Lecture 2: Objects and classes in Java
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS/ENGRD 2110 Spring 2019 Lecture 4: The class hierarchy; static components
Constructors, GUI’s(Using Swing) and ActionListner
CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Special instance methods: toString
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Presentation transcript:

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

Announcements 2  A0 has been graded  Everyone who submitted gets a grade of 1 (the max)  We're not checking submissions! We wanted you to learn how to make sure that assert statements are executed.  We're pleased with how many people are already working on A1, as evidenced by Piazza activity  Please be sure to look at Piazza every day for frequently asked questions and answers  It's due Friday night  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.  A2 : Practice with strings  Now available on course website + CMS That pesky -ea flag!

References to text and JavaSummary.pptx 3  A bit about testing and test cases  Class Object, superest class of them all. Text: C.23 slide 30  Function toString() C.24 slide  Overriding a method C15–C16 slide  Static components (methods and fields) B.27 slide 21, 45  Java application: a program with a class that declares a method with this signature: public static void main(String[])

Homework 4 1.Read the text, Appendix A.1–A.3 2.Read the text, about the if-statement: A.38–A.40 3.Visit course website, click on Resources and then on Code Style Guidelines. Study 2. Format Conventions 4.5 About then-part and else-part of if-statement

A bit about testing 5 Test case: Set of input values, together with the expected output. Develop test cases for a method from its specification --- even before you write the methods body. /** = number of vowels in word w. Precondition: w contains at least one letter and nothing but letters */ public int numberOfVowels(String w) { … } Developing test cases first, in “critique” mode, can prevent wasted work and errors How many vowels in each of these words? creek syzygy

Class W (for Worker) 6 /** Constructor: worker with last name n, SSN s, boss b (null if none). Prec: n not null, s in 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) W lname “Obama” ssn boss null W(…) getLname() getSsn() getBoss() setBoss(W) Contains other methods! toString() equals(Object) hashCode()

Class Object: the superest class of them all 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 {…} W lname “Obama” ssn boss null W(…) getLname() getSsn(), getBoss() setBoss(W) Object toString() equals(Object) hashCode() We draw object like this We often leave off this to reduce clutter; we know that it is effectively always there.

A note on design 8  Don’t use extends just to get access to hidden members! public class PhD { protected String name; … } public class PhDTester extends PhD { … if (student.name == …) … }

A note on design 9  Don’t use extends just to get access to hidden members!

A note on design 10  Don’t use extends just to get access to hidden members!  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  A PhDTester is not a PhD student!  The inheritance hierarchy should reflect modeling semantics, not implementational shortcuts

What is “the name of” the object? 11 The name of the object below is It contains a pointer to the object –i.e. its address in memory, and you can call it a pointer if you wish. But it contains more than that. “Mumsie” null ad1 ad2 advisees null 1 name e PhD Variable e, declared as PhD e; contains not the object but the name of the object (or a pointer to the object). PhD

Method toString 12 Object lname “Obama” ssn boss null W getSsn() … toString() … toString() in Object returns the name of the object: 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() c toString() … c.toString() calls this method In appropriate places, the expression c automatically does c.toString()

Method toString 13 Object lname “Obama” ssn boss null W getSsn() … toString() … toString() in Object returns the name of the object: public class W { … /** Return a representation of this object */ public String toString() { return “Worker ” + lname + “.” + “ Soc sec: …” + getSSn() + “.” + (boss == null ? “” : “Boss ” + boss.lname + “.”); } c toString() … c.toString() calls this method

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

What about this 15  this keyword: this evaluates to the name of the object in which it occurs  Let’s an object instance access its own object reference  Example: Referencing a shadowed class field public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { x = x; y = y; } public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; }

Intro to static components 16 W lname “Om” boss null isBoss(W c) { …} W lname “Jo” boss isBoss(W c) { return this == c.boss; } /** = “this object is c’s boss”. Pre: c is not null. */ public boolean isBoss(W c) { return this == c.boss; } keyword this evaluates to the name of the object in which it appears x.isBoss(y) is false y x y.isBoss(x) is true Spec: return the value of that true-false sentence. True if this object is c’s boss, false otherwise

Intro to static components 17 W lname “Om” ssn 35 boss null isBoss(W) W lname “Jo” ssn 21 boss isBoss(W) /** = “this object is c’s boss”. Pre: c is not null. */ public boolean isBoss(W c) { return this == c.boss; } /** = “b is c’s boss”. Pre: b and c are not null. */ public boolean isBoss(W b, W c) { return b == c.getBoss(); } isBoss(W,W) y x Body doesn’t refer to any field or method in the object. Why put method in object?

Intro to static components 18 W lname “Om” ssn 35 boss null isBoss(W) W lname “Jo” ssn 21 boss isBoss(W) /** = “b is c’s boss”. Pre: b and c are not null. */ public static boolean isBoss(W b, W c) { return b == c.getBoss(); } isBoss(W,W) y x static: there is only one copy of the method. It is not in each object Box for W (objects, static components) x.isBoss(x, y) y.isBoss(x, y) Preferred: W.isBoss(x, y)

Good example of static methods 19  java.lang.Math 

Java application 20 Java application: bunch of classes 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 the method main Command line arguments can be entered with args

Uses of static variables: Maintaining info about created objects 21 W lname “Bid” W “Ob” lname numObjects 2 Box for W public class W { private static int numObjects; … } To have numObjects contain the number of Objects of class W that have been created, simply increment it in constructors. /** Constructor: */ public W(…) { … numObjects = numObjects + 1; }

public class Singleton { private static final Singleton INSTANCE = new Singleton(); private Singleton() { } //... constructor public static Singleton getInstance() { return INSTANCE; } //... methods } Uses of static variables: Implementing the Singleton pattern 22 Singleton INSTANCE Box for Singleton Only one Singleton can ever exist. …