350 likes | 489 Views
Java. Object Oriented Programming. Objectives. Be able to implement inheritance using extends , super , and abstract Be able to describe differences between a concrete and abstract class. Be able to recognize polymorphic behavior.
E N D
Java Object Oriented Programming
Objectives • Be able to implement inheritance using extends, super, and abstract • Be able to describe differences between a concrete and abstract class. • Be able to recognize polymorphic behavior
You can get stuck at the object-based level because you can quickly get there and you get a lot of benefit without much mental effort. It’s also easy to feel like you’re creating data types – you make classes and objects, you send messages to those objects, and everything is nice and neat. • But don’t be fooled. If you stop here, you’re missing out on the greatest part of the language, which is the jump to true object-oriented programming. You can do this only with virtual [abstract] functions. - Bruce Eckel,Thinking in C++,
Example: Analysis • We want to make a payroll program that lists each employee, some information about them, and their pay. ... Employee e = new Employee(0, ”Bashful", "customer representative”, 10.0, 40.0); System.out.println(e); System.out.println(e.computePay()); ... OUTPUT: 0 Bashful Role: customer representative Payrate: 10.0 Hours Worked: 40.0 Pay: $400.0
Adding More Employee Types • Our initial iteration’s design assumes that all employees are paid in exactly the same way • If we want a different kind of employee, we need to create an entirely new type for it • Bashful was an hourly employee, what about a salaried employee? • What would be the same? • ID, Name, Role • What would be different? • No mention of hours, salary instead of payRate
Modeling Objects • Object-Oriented programming models objects and the relationships between them. • Examples: • figures, squares, rectangles, polygons, doodles; • people, students, teachers, staff members, you; • animals, birds, penguins, wings, ; • naval vessels, submarines, carriers, fighter jets. image from http://www.linux.org/info/penguin.html
Modeling and Implementing Relationships We have seen two inter-object relationships: • “is a” – instantiation • implemented using constructor methods (for reference objects); • “has a” – aggregation • implemented using instance data references; • specifies container-contained relationships between classes. • The container references the contained.
Modeling and Implementing Relationships • Inheritance adds another kind of relationship: • implemented using extends clauses;
Class Inheritance • Inheritance specifies one-directional, parent-child relationships. • “is a kind of” – implemented using extends clause • The child inherits the parent’s: • data • methods • Each child can polymorphically “specialize” itself by overridingor adding data or methods.
Carl Linneaus (1707-1778) Taxonomy • Systema Naturae, 1758 • 7 levels: • Kingdom • Phylum • Class • Order • Family • Genus • Species images from www.linnean.org & www.kheper.auz.com
Example: Java’s Classes • All Java classes fit into one hierarchy. • All classes inherit from the root class: java.lang.Object • You can find the full Java hierarchy here: http://java.sun.com/javase/6/docs/api/overview-tree.html
Implementing Inheritance • A child class specifies inheritance from a parent class using the extends clause. • Java provides three modifiers specifying access for class variables and methods: • private • protected • public • It can be safer to declare attributes as private and provide protected accessor and mutator methods.
Super-Class Constructors • super is a reference to the parent. • A child can invoke its parent’s constructor. super(parentConstructorArguments) • A call to the parent’s constructor: • must be the first statement in the constructor; • is added automatically if you don’t add it. • A child can invoke an overridden method: super.parentMethod(arguments)
public class Employee { private intmyId; private String myName, myRole; public Employee() { myId = 0; myName = myRole = ""; } public Employee(int id, String name, String role) { myId = id; myName = name; myRole = role; } //accessors and mutators omitted for space @Override public String toString() { return myId + " " + myName + "\n\tRole: " + myRole; } }
public class HourlyEmployeeextends Employee { private static final double WORK_WEEK = 40.0, OVERTIME_RATE = 1.5; private double myPayrate, myHours; public HourlyEmployee() { myPayrate = myHours = 0.0; } public HourlyEmployee(int id, String name, String role, double payrate, double hours) { super(id, name, role); myPayrate = payrate; myHours = hours; } //accessor and mutators omitted for space, compute pay on comparison slide @Override public String toString() { return super.toString() + "\n\tPayrate: " + myPayrate + "\n\tHours worked: " + myHours; } }
public class SalariedEmployeeextends Employee { private double mySalary; public SalariedEmployee() { mySalary = 0.0; } public SalariedEmployee(int id, String name, String role, double salary) { super(id, name, role); mySalary = salary; } //accessors, mutators omitted for space, computePay() on following @Override public String toString() { return super.toString() + "\n\tAnnual Salary: " + mySalary; } }
Comparison of computePay() //Compute pay for hourly employee @Override public double computePay() { if (myHours <= WORK_WEEK) { return myPayrate * myHours; } else { return (myPayrate * WORK_WEEK) + ((myPayrate * OVERTIME_RATE) * (myHours - WORK_WEEK)); } } Hourly //Compute pay for salaried employee @Override public double computePay() { return mySalary; } Salaried
public class PayrollConsole{ public static void main(String[] args) { // 1. Employee only Employee e = new Employee(0, ”Bashful", "customer representative"); System.out.println(e); // 2. HourlyEmployee HourlyEmployee he = new HourlyEmployee(); he.setPayrate(10.0); he.setHours(50.0); System.out.println(he.computePay()); he.setId(1); he.setName("Sneezy"); he.setRole("intern"); System.out.println(he); // 3. SalariedEmployee SalariedEmployee se = new SalariedEmployee(); se.setId(2); se.setName("Doc"); se.setRole("physician"); se.setSalary(100000.0); System.out.println(se); System.out.println(se.computePay()); }}
Fruit Example Fruit Apple Orange VALID OR INVALID?? Fruit fruit = new Gala(); Orange orange = new Orange(); Orange p = new Apple(); Gala p = new Apple(); Apple p = new Fuji(); Gala Fuji
Examples public class Test { public static void main(String[] args) { B b= new B(); } } class A { public A(){ System.out.println("A's constructor invoked"); } } class B extends A{ }
Examples public class Test { public static void main(String[] args) { B b= new B(); } } class A { public A(int x){ System.out.println("A's constructor invoked"); } } class B extends A{ public B (){ System.out.println(“B’s constructor invoked”); } }
class Test { public static void main(String[] args) { Aa= new B(); System.out.println(a.doThis()); } } class A { public A(){ System.out.println("A's constructor invoked"); } public String doThis(){ return “A’s doThis()”; } } class B extends A{ public B (){ System.out.println(“B’s constructor invoked”); } public String doThis(){ return “B’s doThis()”; } } Examples
Back to our goal We want to be able to print out payroll information: ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(new HourlyEmployee(1, "Sneezy", "intern", 10.0, 40.0)); employees.add(new SalariedEmployee(2, "Doc", "physician", 100000.0)); employees.add(new SalariedEmployee(3, ”Snow White", ”CEO", 175000.0)); employees.add(new SalariedEmployee(4, "Dopey", "trainer", 65000)); employees.add(new HourlyEmployee(5, "Sleepy", "trainee", 15.00, 40.00)); for (inti = 0; i < employees.size(); i++) { System.out.println(employees.get(i)); System.out.println("\tPay: $" + employees.get(i).computePay()); } Problem: There is no computePay() for a generic Employee!
Abstract Classes • Classes can be abstract or concrete. visibility abstract class className { classDefinition } • Like concrete classes, abstract classes: • Can have sub-classes; • Can implement data and methods. • Unlike concrete classes, abstract classes: • Cannot be instantiated.
Abstract Methods • As with classes, methods can also be declared as abstract or concrete. • Abstract classes do not provide definitions for their abstract methods. • Classes that contain abstract methods must be declared as abstract. • visibility abstract typemethodName(params);
Implementing Inheritance • A child class specifies inheritance from a parent class using the extends clause. • Concrete sub-classes must define the abstract methods that they inherit. abstract class Parent { public abstract void aMethod(); } class Child1 extends Parent { public void aMethod() { // define method here... } }
Abstract Class public abstract class Employee { //private instance variables public Employee(int id, String name, String role) { myId = id; myName = name; myRole = role; } public abstract void computePay(); @Override public String toString() { return myId + " " + myName + "\n\tRole: " + myRole; } }
Overriding Methods • When an object to asked to execute a method, Java searches up the inheritance hierarchy for a matching method definition. • Thus, a sub-class that defines its own version of a method overrides any definitions of the methods that it inherits. • A concrete sub-class must implement the abstract methods it inherits using a method with an identical signature.
public class HourlyEmployee extends Employee { private static final double WORK_WEEK = 40.0, OVERTIME_RATE = 1.5; private double myPayrate, myHours; public HourlyEmployee(int id, String name, String role, double payrate, double hours) { super(id, name, role); myPayrate = payrate; myHours = hours; } @Override public double computePay() { if (myHours <= WORK_WEEK) { return myPayrate * myHours; } else { return (myPayrate * WORK_WEEK) + ((myPayrate * OVERTIME_RATE) * (myHours - WORK_WEEK)); } } @Override public String toString() { return super.toString() + "\n\tPayrate: " + myPayrate + "\n\tHours worked: " + myHours; }}
Polymorphism • Polymorphism allows different concrete sub-classes to provide potentially different definitions for a given method inherited from their shared parent. • Java chooses the appropriate method definition based upon which child the object instantiates at either: • Compile time (static binding); • Run time (dynamic binding).
Example List<Parent> myObjects = new ArrayList<Parent>(); myObjects.add(new Child1(arguments)); myObjects.add(new Child2(arguments)); // add more children of either type... for (inti = 0; i < myObjects.size(); i++) { myObjects.get(i).aMethod(arguments); }
Now with abstract Employee class ArrayList<Employee> employees = new ArrayList<Employee>(); employees.add(new HourlyEmployee(1, "Sneezy", "intern", 10.0, 40.0)); employees.add(new SalariedEmployee(2, "Doc", "physician", 100000.0)); employees.add(new SalariedEmployee(3, ”Snow White", ”CEO", 175000.0)); employees.add(new SalariedEmployee(4, "Dopey", "trainer", 65000)); employees.add(new HourlyEmployee(5, "Sleepy", "trainee", 15.00, 40.00)); for (inti = 0; i < employees.size(); i++) { System.out.println(employees.get(i)); System.out.println("\tPay: $" + employees.get(i).computePay()); } This now works because Java knows that every employee will have a computePay method.
What’s the Big Idea Fredrick P. Brooks (1931- )The Mythical Man-Month Joys of programming We enjoy designing things because we are created in the image of God. The computer is a powerful and rewarding tool to use. Woes of programming The “mindless” details can be excessively tedious. Products become obsolete too quickly. As the child delights in his mud pie, so the adult enjoys building things, especially things of his own design. I think this delight must be an image of God's delight in making things, a delight shown in the distinctness and newness of each leaf and each snowflake. - F. P. Brooks, Jr. The Mythical Man-Month, 1975 images from: http://www.amazon.com/