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

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

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

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

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

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

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

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

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

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

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

Inheritance. Transitivity

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

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

CS/ENGRD 2110 SPRING 2018

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

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

JAVA MOCK TEST JAVA MOCK TEST II

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Lecture 10 Declarations and Scope

Abstract Classes and Interfaces

Java Object Oriented Design. CSC207 Fall 2014

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

CS313D: ADVANCED PROGRAMMING LANGUAGE

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Overriding Variables: Shadowing

Prelim 1 SOLUTION. CS 2110, September 29, 2016, 7:30 PM Total Question Name Loop invariants. Recursion OO Short answer

G52CPP C++ Programming Lecture 9

Inheritance and Encapsulation. Amit Gupta

CSC9T4: Object Modelling, principles of OO design and implementation

Arrays Classes & Methods, Inheritance

Programming overview

Java Review: Objects

CSE 113 A. Announcements - Lab

Overview. Elements of Programming Languages. Objects. Self-Reference

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Declarations and Access Control SCJP tips

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

Exam Duration: 2hrs and 30min Software Design

Oracle 1Z Java Certified Programmer. Download Full Version :

Inheritance and Polymorphism

CS100J Prelim 1 22 February 2007

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Lecture 18 CSE11 Fall 2013 Inheritance

Programming II (CS300)

CS111: PROGRAMMING LANGUAGE II

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Inheritance (IS A Relationship)

Introduction to Programming Using Java (98-388)

More on Objects in JAVA TM

Java Programming Lecture 7

Overview. Elements of Programming Languages. Objects. Self-Reference

24. Inheritance. Java. Fall 2009 Instructor: Dr. Masoud Yaghini

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Midterm 2 Grading Key

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

Object Oriented Programming. Java-Lecture 11 Polymorphism

6.005 Elements of Software Construction

Programming II (CS300)

ECE 122. Engineering Problem Solving with Java

Object Oriented Programming

Chapter 6 Introduction to Defining Classes

CS 61B Discussion 5: Inheritance II Fall 2014

CMSC 132: Object-Oriented Programming II

WA1278 Introduction to Java Using Eclipse

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

EMBEDDED SYSTEMS PROGRAMMING OO Basics

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

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

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

Subclassing for ADTs Implementation

Everything is an object. Almost, but all objects are of type Object!

Unit3: Java in the large. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Java Fundamentals (II)

Lecture 4: The class hierarchy; static components

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Lecture 02, Fall 2018 Friday September 7

Chapter 10 Classes Continued. Fundamentals of Java

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Definition of DJ (Diminished Java)

More On inheritance. What you can do in subclass regarding methods:

Programming Exercise 14: Inheritance and Polymorphism

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Identifiers and Variables

CS61B Lecture #13: Packages, Access, Etc.

Within a source file or class we find the typical layout of file MyClass.java : [/* * header comment, for CVS, IP issues, etc. */] [package foo;]

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

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

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

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Classes, Objects, and OOP in Java. June 16, 2017

Handout 7. Defining Classes part 1. Instance variables and instance methods.

Lecture 2: Java & Javadoc

CS111: PROGRAMMING LANGUAGE II

Lecture 4: The class hierarchy; static components

Transcription:

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

References to text and JavaSummary.pptx 2 Local variable: variable declared in a method body B.10 B.11 slide 45 Inside-out rule, bottom-up/overriding rule C.15 slide 31-32 and consequences thereof slide 45 Use of this B.10 slide 23-24 and super C.15 slide 28, 33 Constructors in a subclass C.9 C.10 slide 24-29 First statement of a constructor body must be a call on another constructor if not Java puts in super(); C.10 slide 29

Homework 3 Visit course website, click on Resources and then on Code Style Guidelines. Study 4.2 Keep methods short 4.3 Use statement-comments 4.4 Use returns to simplify method structure 4.6 Declare local variables close to first use

Local variables middle(8, 6, 7) 4 /** 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); Local variable: variable declared in method body Parameter: variable declared in () of method header a 8 b 6 c 7 temp? All parameters and local variables are created when a call is executed, before the method body is executed. They are destroyed when method body terminates.

Scope of local variables 5 /** 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; block c= temp; if (a <= b) { return b; return Math.min(a, c); Scope of local variable (where it can be used): from its declaration to the end of the block in which it is declared.

Principle: declaration placement 6 /** Return middle value of a, b, c (no ordering assumed) */ public static int middle(int a, int b, int c) { int temp; if (b > c) { temp= b; b= c; c= temp; Not good! No need for reader to know about temp except when reading the then-part of the ifstatement if (a <= b) { return b; return Math.min(a, c); Principle: Declare a local variable as close to its first use as possible.

Assertions promote understanding 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; Assertion: Asserting that b <= c // b <= c at this point. Helps reader if (a <= b) { understand code below. return b; // a and c are both greater than b return Math.min(a, c);

Bottom-up/overriding rule 8 Which method tostring() is called by turing.tostring()? Overriding rule or bottom-up rule: To find out which is used, start at the bottom of the object and search upward until a matching one is found. turing @20 @20 Object tostring() name Turing tostring() {

Calling a constructor from a constructor 9 public class Time private int hr; //hour of day, 0..23 private int min; // minute of hour, 0..59 /** Constructor: instance with h hours and m minutes */ public Time(int h, int m) { hr = h; min = m; assert ; /** Constructor: instance with m minutes */ public Time(int m) { hr = m / 60; min = m % 60; Want to change body to call first constructor

Calling a constructor from a constructor 10 public class Time private int hr; //hour of day, 0..23 private int min; // minute of hour, 0..59 /** Constructor: instance with h hours and m minutes */ public Time(int h, int m) { hr = h; min = m; assert ; /** Constructor: instance with m minutes */ public Time(int m) { this(m / 60, m % 60); Use this (not Time) to call another constructor in the class. Must be first statement in constructor body!

Inside-out rule 11 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.) @a0 sep name getnamewithsep() { return name + sep; @a1 name getnamewithsep () { return name + sep; s objects and static components

Constructing with a Superclass 12 /** Constructor: person f n */ public (String f, String l) { first= n; last= l; Use super (not ) to call superclass constructor. /** Constructor: PhD Dr. f m. l */ public PhD(String f, char m, String l) { super(f, l); middle= m; Must be first statement in constructor body! PhD@a0 tostring() first Ross null getname() Object last Tate null tostring() PhD new PhD( Ross, E, Tate ); middle \0 E

About super 13 PhD@a0 tostring() Object Within a subclass object, super refers to the partition above the one that contains super. first Ross last Tate getname() tostring() middle E PhD tostring() { super.tostring() Because of keyword super, the call tostring here refers to the partition.

Bottom-Up and Inside-Out 14 sep PhD@a0 tostring() Object title Dr. first Ross last Tate getname() tostring() middle E PhD super getname() tostring()

Without OO 15 Without OO, you would write a long involved method: public double getname( p) { if (p is a PhD) { else if (p hates formality) { else if (p prefers anonymity) { else OO eliminates need for many of these long, convoluted methods, which are hard to maintain. Instead, each subclass has its own getname. Results in many overriding method implementations, each of which is usually very short