principles of
play

Principles of Everything is an object Computer Science II Program - PDF document

Object-Oriented Programming Principles of Everything is an object Computer Science II Program is a bunch of objects Tell each other what to do by sending messages (calling methods) Each object has its own memory made up of other


  1. Object-Oriented Programming Principles of  Everything is an object Computer Science II  Program is a bunch of objects – Tell each other what to do by sending messages (calling methods)  Each object has its own memory made up of other objects  Every object has a type Prof. Nadeem Abdul Hamid – Each object is an instance of a class CSC 121A – Spring 2005  All objects of a particular type can receive Lecture Slides 2 - the same messages OO Review, Scope, and Inheritance CSC 121A - Berry College - Spring 2005 2 Definition of an Object Scope  Determines the visibility and lifetime of variables  An object has state, behavior and identity.  In Java, scope determined by placement of curly [Booch] braces {} – State: internal data (fields or instance variables) { int x = 12; – Behavior: methods // Only x available – Identity: each object uniquely distinguished { int q = 96; from others; i.e. has a unique address in memory // Both x & q available } // Only x available // q “out of scope” } CSC 121A - Berry College - Spring 2005 3 CSC 121A - Berry College - Spring 2005 4 Object Lifetimes Access Specifiers  Not the same as primitive variables  Every class member may have an access { specifier before it: public/protected/private String s = “a string”;  Package access } // end of scope – Also known as “friendly”: default access which  The reference, s, vanishes at the end of scope applies when you don’t specify access  String object pointed to is still there – All classes in the same package have access to  Unlike other languages, don’t worry about that class member (field/method) cleaning up memory – To all classes outside the package, the member  Java uses a garbage collector to figure out which appears private objects are no longer in use and reclaim their memory CSC 121A - Berry College - Spring 2005 5 CSC 121A - Berry College - Spring 2005 6 1

  2. Access Specifiers (cont.) Accessibility Table  public: member is available to everyone  private: only the class containing the member can access it class Sundae { private Sundae() {} static Sundae makeASundae() { return new Sundae(); } } public class IceCream { public static void main(String[] args) { //! Sundae x = new Sundae(); Sundae x = Sundae.makeASundae(); } }  protected: access granted to derived classes CSC 121A - Berry College - Spring 2005 7 CSC 121A - Berry College - Spring 2005 8 Inheritance Inheritance Relationships  Primary feature of OO programming for  “Is-a” vs. “Has-a” relationship software reuse  “Has-a” relationship  Define a new class by taking features of an – Determines the fields of a class existing class and modifying or extending – Employee has a Name , SSN , PayRate , etc . them – Car has a SteeringWheel , FuelTank , etc . – Existing class: superclass  “Is-a” relationship – New, derived class: subclass – Represented using inheritance  A subclass is more specific than its superclass – HourlyEmployee is an Employee , … – Car is a Vehicle , Boat is a Vehicle , …  Subclasses and superclasses form a class hierarchy – Java class hierarchy starts with class Object CSC 121A - Berry College - Spring 2005 9 CSC 121A - Berry College - Spring 2005 10 Shapes Hierarchy Hierarchy for University Community Members CommunityMember Shape Employee Student Alumnus TwoDShape ThreeDShape Faculty Staff Circle Square Triangle Sphere Cube Tetrahedron Administrator Teacher CSC 121A - Berry College - Spring 2005 11 CSC 121A - Berry College - Spring 2005 12 2

  3. CommissionEmployee Class CommissionEmployee Class // CommissionEmployee.java // CommissionEmployee.java // Class represents employee paid on commission // Class represents employee paid on commission // // // Nadeem Abdul Hamid (based on Deitel & Deitel Ch. 9) // Nadeem Abdul Hamid (based on Deitel & Deitel Ch. 9) // CSC 121 - Spring 2005 // CSC 121 - Spring 2005 // // public class CommissionEmployee extends Object { public class CommissionEmployee extends Object { CSC121 Standard header format private String name; // full name private String name; // full name private double sales; // gross weekly sales private double sales; // gross weekly sales private double rate; // commission percentage private double rate; // commission percentage Every Java class directly or indirectly inherits Object’s methods (explicitly or implicitly) -- only included here for public CommissionEmployee( String name, double sales, public CommissionEmployee( String name, double sales, demonstration purposes double rate ) { double rate ) { this.name = name; this.name = name; setSales( sales ); // validate and store gross sales setSales( sales ); // validate and store gross sales setRate( rate ); // validate and store commission rate setRate( rate ); // validate and store commission rate } // end CommissionEmployee constructor } // end CommissionEmployee constructor // set name // set name public void setName( String name ) { this.name = name; } public void setName( String name ) { this.name = name; } // return name // return name public String getName() { return name; } public String getName() { return name; } CSC 121A - Berry College - Spring 2005 13 CSC 121A - Berry College - Spring 2005 14 // set gross sales amount CommissionEmployee Class public void setSales( double sales ) { this.sales = ( sales < 0.0 ) ? 0.0 : sales; } // CommissionEmployee.java // Class represents employee paid on commission conditional operator (ternary) // // return gross sales amount public double getSales() { return sales; } // Nadeem Abdul Hamid (based on Deitel & Deitel Ch. 9) // CSC 121 - Spring 2005 // // set commission rate public void setRate( double rate ) { this.rate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; public class CommissionEmployee extends Object { } private String name; // full name private double sales; // gross weekly sales private double rate; // commission percentage // return commission rate public double getRate() { return rate; } public CommissionEmployee( String name, double sales, double rate ) { // calculate earnings public double earnings() { return rate * sales; } this.name = name; setSales( sales ); // validate and store gross sales setRate( rate ); // validate and store commission rate // return String representation of CommissionEmployee object public String toString() { } // end CommissionEmployee constructor return "commission employee: " + name + "\n" + // set name "gross sales: " + sales + "\n" + "commission rate: " + rate; public void setName( String name ) { this.name = name; } } // end method toString // return name style to help match braces } // end class CommissionEmployee public String getName() { return name; } CSC 121A - Berry College - Spring 2005 15 CSC 121A - Berry College - Spring 2005 16 // public class BasePlusCommissionEmployee { // CommissionEmployeeTest.java private String name; // full name // Testing class CommissionEmployee private double sales; // gross weekly sales // private double rate; // commission percentage // Nadeem Abdul Hamid (based on Deitel & Deitel Ch. 9) private double salary; // base salary per week // CSC 121 - Spring 2005 // public BasePlusCommissionEmployee( String name, double sales, double rate, double salary ) { public class CommissionEmployeeTest { this.name = name; setSales( sales ); // validate and store gross sales public static void main( String args[] ) { setRate( rate ); // validate and store commission rate // instantiate CommissionEmployee object setSalary( salary ); // validate and store salary CommissionEmployee empl } // end BasePlusCommissionEmployee constructor = new CommissionEmployee( "Sue Jones", 10000, 0.06 ); // set name // get commission employee data public void setName( String name ) { this.name = name; } System.out.println( "Employee info obtained by get methods: \n" ); System.out.println( "Name is " + empl.getName() ); // return name System.out.println( "Gross sales are " + empl.getSales() ); public String getName() { return name; } System.out.println( "Commission rate is " + empl.getRate() ); // set gross sales amount empl.setSales( 500 ); public void setSales( double sales ) { empl.setRate( .1 ); this.sales = ( sales < 0.0 ) ? 0.0 : sales; } System.out.println( "\nUpdated employee info obtained by toString:\n\n" + empl + "\n" ); // return gross sales amount } // end main public double getSales() { return sales; } } // end class CommissionEmployeeTest // set commission rate public void setRate( double rate ) { CSC 121A - Berry College - Spring 2005 17 this.rate = ( rate > 0.0 && rate < 1.0 ) ? rate : 0.0; CSC 121A - Berry College - Spring 2005 18 } 3

Recommend


More recommend