Download Local Vars - Object-Oriented Programming and Data Structures - Lecture Sl and more Lecture notes Object Oriented Programming in PDF only on Docsity! Local variables /** Return middle value of b, c, d (no ordering assumed) */ public static int middle(int b, int c, int d) { if (b > c) { int temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); } Parameter: variable declared in () of method header middle(8, 6, 7) b 8 d 7 c 6 Local variable: variable declared in method body 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. docsity.com Scope of local variable /** Return middle value of b, c, d (no ordering assumed) */ public static int middle(int b, int c, int d) { if (b > c) { int temp= b; b= c; c= temp; } // { b <= c } if (d <= b) { return b; } // { b < d and b <= c } return Math.min(c, d); } Scope of local variable (where it can be used): from its declaration to the end of the block in which it is declared. block docsity.com Bottom‐up/overriding rule toString() { … } Object Butterfly@20 Butterfly toString() name “Beaut” c Butterfly@20 Which method toString() is called by c.toString() ? Overriding rule or bottom-up rule: To find out which is used, start at the bottom of the objectand search upward until a matching one is found. docsity.com Inside‐out rule Inside‐out rule: Code in a construct can reference any names declared in that construct, as well as names that appear in enclos‐ ing constructs. (If name is declared twice, the closer one prevails.) Person Person@a0 n getNAndPop() { return n + PersonPop; } Person Person@a1 n getNAndPop() { return n + PersonPop; } Person’s objects and static components PersonPop docsity.com Parameters participate in inside‐out rule setN(String name) { n= name; } Person Person@a0 n setN(String n) { n= n; } Person Person@a0 n Parameter n “blocks” reference to field n. Doesn’t work right docsity.com Calling a constructor from a constructor Time@fa8 Time hr 9 min 5 … Time(int, int) Time (int) 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) { …} /** Constructor: instance with m minutes … */ public Time(int m) { hr= m / 60; min= m % 60; } … } Want to change body to call first constructor docsity.com Calling a constructor from a constructor Time@fa8 Time hr 9 min 5 … Time(int, int) Time (int) 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) { …} /** Constructor: instance with m minutes … */ public Time(int m) { this(m / 60, m % 60); } … } Use this (Instead of Time) to call another constructor in the class. Must be first statement in constructor body! docsity.com Initialize superclass fields first Executive@a0 Object name “G” start 1969 salary 10,000 Employee(String, int) toString() getCompensation() toString() … Employee Executive bonus getBonus() getCompensation() toString() 50,000 Class Employee contains info that is common to all employees — name, start date, salary, etc. getCompensation gives the salary Executives also get a bonus. getCompensation is overridden to take this into account Could have other subclasses for part-timers, temporary workers, consultants, etc., each with a different getCompensation docsity.com Principle: initialize superclass fields first Executive@a0 name start salary Employee(String, int, double) Employee Executive bonus Executive(String, int, double) /** Constructor: employee with name n, year hired d, salary s */ public Employee(String n, int d, double s) /** Constructor: executive with name n, year hired d, salary of $50,000, bonus b */ public Executive(String n, int d, double s, double b) Principle: In subclass constructor, fill in the superclass fields first How to do that if they are private? Call constructor in superclass docsity.com Principle: initialize superclass fields first Executive@a0 name start salary Employee(String, int, double) Employee Executive bonus Executive(String, int, double) /** Constructor: employee with name n, year hired d, salary s */ public Employee(String n, int d, double s) /** Constructor: executive with name n, year hired d, salary of $50,000, bonus b */ public Executive(String n, int d, double s, double b) { Employee(n, d, 50000); bonus= b; } super To call a superclass constructor, use super( … ) docsity.com /** Constructor: an instance with …*/ public C (…) { S0; S1; … } Principle: initialize superclass fields first C@a0 C1( … ) C1 C( … ) C Java syntax: First statement of any constructor you write must be a call on another constructor this( … ); or super( … ); Object Object( … ) … If you don’t put on in, Java inserts this one: super(); super(); docsity.com