CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
Unit 011 Inheritance Recall What Inheritance is About The extends Keyword The Object Class Overriding versus Overloading What is Actually Inherited? Single.
CS 1110 Final Exam: Review Session 2 Part 1 : Inheriting classes 1. Inheritance Facts 2. Constructors in Subclasses BREAK : 10 sec. Part 2 : Working with.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
1 Biggest issue!!! You can’t do questions on this topic correctly unless you draw variables, draw objects when they are created, and draw frames for method.
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 and Polymorphism Daniel Liang, Introduction to Java Programming.
CSC 142 Computer Science II Zhen Jiang West Chester University
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CS/ENGRD 2110 SPRING 2012 Lecture 2: Objects and classes in Java 1.
ECE122 Feb. 22, Any question on Vehicle sample code?
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Inheritance (Part 2) Notes Chapter KomondorBloodHound PureBreedMix Dog Object Dog extends Object PureBreed extends Dog Komondor extends PureBreed.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CS/ENGRD 2110 SPRING 2016 Lecture 2: Objects and classes in Java 1.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Methods.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
1 CS1110 Thursday, 10 Feb 2011 Discussion of Methods: Executing method calls. If-statements. The return statement in a function. Local variables. For this.
1 CS February 2009 Inside-out rule; use of this and super Developing methods (using String ops). Read sec. 2.5 on stepwise refinement Listen to.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Defining Your Own Classes II
CS/ENGRD 2110 Fall 2017 Lecture 2: Objects and classes in Java
Some Eclipse shortcuts
CS/ENGRD 2110 Spring 2014 Lecture 5: Local vars; Inside-out rule; constructors
Review Session.
CS/ENGRD 2110 Spring 2018 Lecture 2: Objects and classes in Java
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 302 Week 11 Jim Williams, PhD.
CS/ENGRD 2110 Spring 2018 Lecture 5: Local vars; Inside-out rule; constructors
CSC240 Computer Science III
Chapter 9 Inheritance and Polymorphism
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
CSC 113 Tutorial QUIZ I.
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
Overloading and Overriding
Building Java Programs
Building Java Programs
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
Sit next to someone. Today, we do some work in pairs.
Building Java Programs
Building Java Programs
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
Sit next to someone. Today, we do some work in pairs.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Building Java Programs
CS360 Client/Server Programming Using Java
Building Java Programs
Chapter 11 Inheritance and Polymorphism Part 1
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
CS 240 – Advanced Programming Concepts
Inheritance and Polymorphism
Presentation transcript:

CS/ENGRD 2110 Spring 2019 Lecture 5: Local vars; Inside-out rule; constructors http://courses.cs.cornell.edu/cs2110

Announcements A1 is due Thursday If you are working with a partner: form a group well before submitting on CMS. After forming group, only one you needs to submit. This week's recitation is on testing. No tutorial/quiz this week. A2 is out now

Today’s Topics Scope, local variables, inside-out rule Overloading, bottom-up rule Constructors, this, default, super All searchable in JHT!

Local Variables middle(8, 6, 7) /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { if (b > c) { int temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } Parameter: variable declared in () of method header Local variable: variable declared in method body a 8 c 7 b 6 temp ? All parameters and local variables are limited in scope…

Scope of Local Variables /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { if (b > c) { int temp= b; b= c; c= temp; } if (a <= b) { return b; return Math.min(a, c); } block Scope of local variable (where it can be used): from its declaration to the end of the block in which it is declared. Every pair of curly braces defines a block.

Scope In General: Inside-out rule Inside-out rule: Code in a construct can reference names declared in that construct, as well as names that appear in enclosing constructs. (If name is declared twice, the closer one prevails.) public class C{ private int field; public void method(int parameter) {      if (field > parameter) {          int temp= parameter;      } } class method block

What 3 numbers are printed? public class ScopeQuiz {   private int a;   public ScopeQuiz(int b) {     System.out.println(a);     int a= b + 1;     this.a= a;     a= a + 1;   }   public static void main(String[] args) {     int a= 5;     ScopeQuiz s= new ScopeQuiz(a);     System.out.println(s.a); } A: 5, 6, 6 B: 0, 6, 6 C: 6, 6, 6 D: 0, 6, 0 Answer: B Cool trick: in Eclipse, syntax highlighting distinguishes fields from local variables

Review: Constructor public class Person { private String firstName; // not null private String lastName; /** Constructor: a Person with first and last names f, l. * Precondition: f is not null. */ public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } Constructor: Initialize fields to truthify class invariant Not everyone has a lastName in this [semantic data] model.

Review: Which toString is called? public class Person { private String firstName; private String lastName; public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } public String toString() { return firstName + “ ” + lastName; Person p1= new Person(“Grace”, “Hopper”); p1.toString(); p1 Person@20 Person@20 toString() Object firstName “Grace” lastName “Hopper” toString() Person

Which toString : Bottom-up Rule public class Person { private String firstName; private String lastName; public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } public String toString() { return firstName + “ ” + lastName; Person p1= new Person(“Grace”, “Hopper”); p1.toString(); Which method is used? Start at bottom of the object and search upward until you find a match. p1 Person@20 Person@20 toString() Object firstName “Grace” lastName “Hopper” toString() Person

Grace Hopper (1906-1992) Rear Admiral, US Navy PhD, Math, Yale, 1934 Pioneering computer programmer Posthumous Presidential Medal of Freedom (highest civilian honor), 2016

Grace Hopper: “bug” Her team credited with first documented ”bug”

Middle Names (v1) 👍🏽 or 👎 ? public class Person { private String firstName; //not null private String middleName; private String lastName; /** Constructor: … */ public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } /** Constructor: … */ public Person(String f, String m, String l) { assert f != null; firstName= f; middleName= m; lastName= l; } 👍🏽 or 👎 ? Better: reuse first constructor without copying code

Middle Names (v2) public class Person { private String firstName; //not null private String middleName; private String lastName; public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } public Person(String f, String m, String l) { this(f, l); middleName= m; Use this (not Person) to call another constructor in the class. Must be first statement in constructor body!

Default Constructor 👍🏽 or 👎 ? public class Person { private String firstName; //not null private String lastName; } Person p= new Person(); public Person() {}; Bad: doesn’t truthify class invariant 👍🏽 or 👎 ?

Default Constructor Nope! public class Person { private String firstName; //not null private String middleName; private String lastName; public Person(String f, String l) { assert f != null; firstName= f; lastName= l; } public Person(String f, String m, String l) { this(f, l); middleName= m; Person p= new Person(); Nope! Syntax Error: No constructor in Person matches this invocation Arguments: () Expected return type: Person Candidate signatures: Person(String, String) Person(String, String, String)

Super Constructor Object firstName lastName toString() Person Cornellian@a0 Object firstName lastName toString() Person Cornellian netID null public class Cornellian extends Person { private String netID; /** Constructor: Person with a netID. */ public Cornellian(String f, String l, String id) { super(f, l); netID= id; } new Cornellian("Bill", "Nye", “bn29”); "Bill" "Nye" Use super (not Person) to call superclass constructor. Before next slide, make these changes, then override toString in Person and use super.toString(). Must be first statement in constructor body! “bn29”

Super Constructor 👍🏽 or 👎 ? public class Cornellian extends Person { private String netID; /** Constructor: Person with a netID. */ public Cornellian(String f, String l, String id) { netID= id; } super(); If first statement in constructor body is not a constructor call, Java inserts super(); for you! 👍🏽 or 👎 ?

More about super Within a subclass object, super refers to the partition above the one that contains super. Cornellian@a0 Object firstName lastName toString() Person Cornellian netID "Bill" "Nye" “bn29” Because of keyword super, the call toString here refers to the Person partition. toString() { return super.toString() + “<“ + netID + “>”; }

Grace Hopper