Object-Oriented Design • Method for designing computer programs Classes • Consider “objects” interacting in the program – Example: a zoo, a gradebook OOD Goals OOD Principles • Robustness • Abstraction – Gracefully handle failures – Abstract Data Types (ADTs) • Adaptability – Interfaces • Encapsulation – Evolve as necessary – Information Hiding • Reusability • Modularity – Many programs use same piece of code – Easily plug together components What is a class? Methods • Data and the methods that operate on • Typically, data (variables) declared private that data – collectively called members • Methods operate on data – Example: bank account class – accessors – read data, provide access to data but do not change it • Provide structure for organizing programs – mutators – change data • examples from bank account, zoo??? – constructor – builds a new object 1
Writing Classes BankAccount Class • Must be implemented in a file named • public BankAccount(double balance); classname .java • public void withdraw(double amount); – well…there are also inner classes • public void deposit(double amount); • public double checkBalance(); Creating and Using Objects Creating and Using Objects BankAccount b = new BankAccount(500); //how would you withdraw funds? //Type Name = new Type(constructor parameters); b.withdraw(300); object_name.method_name(param list); //how would you withdraw funds? Constructor Flight class • Special-case function called when a • Think about the design of a class to represent a flight… new object is created – Data members? • Used to initialize member variables – Methods? – Examples? • Default constructor takes no parameters 2
Scope static • What is the scope of each of the variables • Static class member - a variable with you declared in your flight class? scope the same as a class member – 1 per class, not per object • Example - car serial number 3
Recommend
More recommend