TAO of Java, Class 2: More Java Basics Overview of Object-Oriented Programming Your First Java Program Running Java in the MathLAN More on Input and Output in Java Object-Oriented Programming: Greek Philosophy in Action * Almost everything in the progrm is "an object" * Objects combine data and functionality * Objects are built from "templates" called "classes" Example: Fractions (Also called Rationals) * Every fraction has a numerator and a denominator * Different fractions may have different numerators and denominators * You may expect to divide two fractions * Fraction A: Divide yourself by fraction B * Thingy: Divide fraction A by fraction B * Similarly, you may expect to multiply, add, subtract, ... * Also * Express as a real number * Simplify Creating a fraction: * First, describe the generalities (see above) * Next, say "I want to create the fraction 1/2"; it gets all of the properties described above. public class Fraction { int numerator; int denominator; Fraction multiply(Fraction other) { // Instructions for multiplying } // multiply(Fraction) } // class Fraction Generically: FUNCTION-DEFN: RETURN_TYPE FUNCTION_NAME(LIST-OF-PARAMETRS) { INSTRUCTIONS } PARAMETR: TYPE PARAMETER_NAME How to make a new fraction Fraction oneHalf = new Fraction(1,2); "new" says to create a new object; if it's not there, the compiler simply looks for a function with the given name. --- Second key aspect of object-oriented programming: "Inheritance" You can design new classes based on other classes, without duplicating the stuff from the original class A library book is a book and adds the following * Data: Call number, Checkout status, ... * Capabilitieis: borrow, return, ... --- Third key aspect of object-oriented programming: "Polymorphism" If you've written a procedure that deals with one kind of object, it automatically deals with all "similar" objects --- Final key aspect of object-oriented programming: "Encapsulation" Hide the innards of the object from the outside world. The outside world simply has access to the functionality we choose to reveal -----