7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 1
Introduction to Object Oriented Programming Introduction to Object - - PowerPoint PPT Presentation
Introduction to Object Oriented Programming Introduction to Object - - PowerPoint PPT Presentation
Introduction to Object Oriented Programming Introduction to Object Oriented Programming plw@ku.ac.th
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 2
Constraints
void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 3
Constraints 2
Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 4
Access Protection
Car c = new Car("New York A234 567", 100.0); c.speed = 150.0;
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 5
Examples of Access Protection
public class Car { private private String licensePlate; // e.g. "New York A456 324" private private double speed; // kilometers per hour private private double maxSpeed; // kilometers per hour public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public double getSpeed() { return this.speed; } public double getMaxSpeed() { return this.maxSpeed; }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 6
Examples of Access Protection 2
// setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 7
Examples of Access Protection
class CarTest8 { public static void main(String args[]) { Car c = new Car("New York A45 636", 100.0); c.licensePlate = "New York A45 636"; c.speed = 0.0; c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c. speed + " kilometers per hour."); c.floorIt(); System.out.println(c.licensePlate + " is moving at " + c. speed + " kilometers per hour."); } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 8
Output
c:\> javac Car.java c:\> javac CarTest8.java CarTest8.java:7: Variable licensePlate in class Car not accessible from class CarTest8. c.licensePlate = "New York A45 636"; ^ CarTest8.java:8: Variable speed in class Car not accessible from class CarTest8. c.speed = 0.0; ^ CarTest8.java:9: Variable maxSpeed in class Car not accessible from class CarTest8. c.maxSpeed = 123.45; Examples of Access Protection ^ CarTest8.java:11: Variable licensePlate in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:11: Variable speed in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:16: Variable licensePlate in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ CarTest8.java:16: Variable speed in class Car not accessible from class CarTest8. System.out.println(c.licensePlate + " is moving at " + c.speed + ^ 7 errors c:\>
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 9
The Four Levels of Access Protection
If you want any object at all to be able to call a method or change a field, declare it public public. If you want only objects in the same class to be able to get or set the value of a field or invoke a method, declare it private private. If you want access restricted to subclasses and members of the same package, declare it protected protected. Finally, to restrict access only to objects in the same package, use no access no access declaration declaration at all. This is called "package" or "default" access, but it has no keyword.
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 10
The Three Benefits of Access Protection The Three Benefits of Access Protection
It allows you to enforce constraints on an object's state. It provides a simpler client interface. Client programmers don't need to know everything that's in the class, only the public parts. It separates interface from implementation, allowing them to vary
- independently. For instance consider making the licensePlate field of Car an
instance of a new LicensePlate class instead of a String.
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 11
Changing the Implementation
public class Car { private String licensePlate; // e.g. "New York A456 324" private float speed; // kilometers per hour private float maxSpeed; // kilometers per hour public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0F; if (maxSpeed >= 0.0) { this.maxSpeed = (float) maxSpeed; } else { maxSpeed = 0.0F; } } // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public double getSpeed() { return this.speed; } public double getMaxSpeed() { return this.maxSpeed; }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 12
Changing the Implementation 2
// setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + (float) deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0F; } } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 13
What should be public What should be public? ? What should be private What should be private? ?
Classes are public. Fields are private. Constructors are public. Getter and Setter methods are public. Other methods must be decided on a case-by-case basis.
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 14
Overloading
public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour // constructors public Car(String licensePlate, double maxSpeed) { this.licensePlate = licensePlate; this.speed = 0.0; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } } public Car(String licensePlate, double speed, double maxSpeed) { this.licensePlate = licensePlate; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } } // other methods... }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 15
this in constructors
public Car(String licensePlate, double maxSpeed) { Car Car(licensePlate, 0.0, maxSpeed); } public Car(String licensePlate, double maxSpeed) { this this(licensePlate, 0.0, maxSpeed); }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 16
this in constructors 2
public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour // constructors public Car(String licensePlate, double maxSpeed) { this(licensePlate, 0.0, maxSpeed); } public Car(String licensePlate, double speed, double maxSpeed) { this.licensePlate = licensePlate; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } } // other methods... }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 17
Operator Overloading
Therefore Java does not support operator overloading.
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 18
Inheritance Inheritance
Inheritance is the mechanism by which this is achieved. An object can inherit the variables and methods of another object. It can keep those it wants, and replace those it doesn't want.
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 19
Inheritance
public class Car { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour private String make; // e.g. "Ford" private String model; // e.g. "Taurus" private int year; // e.g. 1997, 1998, 1999, 2000, 2001, etc. private int numberPassengers; // e.g. 4 private int numberWheels = 4; // all cars have four wheels private int numberDoors; // e.g. 4 // constructors public Car(String licensePlate, double maxSpeed, String make, String model, int year, int numberOfPassengers, int numberOfDoors) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers, numberOfDoors); } public Car(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, speed, maxSpeed, make, model, year, numberOfPassengers, 4); }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 20
Inheritance
public Car(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers, int numberOfDoors) { // I could add some more constraints like the // number of doors being positive but I won't // so that this example doesn't get too big. this.licensePlate = licensePlate; this.make = make; this.model = model; this.year = year; this.numberPassengers = numberOfPassengers; this.numberDoors = numberOfDoors; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 21
Inheritance
// getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public String getMake() { return this.make; } public String getModel() { return this.model; } public int getYear() { return this.year; } public int getNumberOfPassengers() { return this.numberPassengers; } public int getNumberOfWheels() { return this.numberWheels; }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 22
Inheritance
public int getNumberOfDoors() { return this.numberDoors; } public double getMaxSpeed() { return this.speed; } public double getSpeed() { return this.maxSpeed; } // setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 23
Inheritance
public class Motorcycle { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour private String make; // e.g. "Harley-Davidson" private String model; // e.g. "panhead" private int year; // e.g. 1997,1998,1999,2000,2001,etc. private int numberPassengers; // e.g. 4 private int numberWheels = 2; // all motorcycles have two wheels // constructors public Motorcycle(String licensePlate, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers); } public Motorcycle(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this.licensePlate = licensePlate; this.make = make; this.model = model; this.year = year; this.numberPassengers = numberOfPassengers; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 24
Inheritance 2
public int getYear() { return this.year; } public int getNumberOfPassengers() { return this.numberPassengers; } public int getNumberOfPassengers() { return this.numberWheels; } public double getMaxSpeed() { return this.speed; } // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public String getMake() { return this.make; } public String getModel() { return this.model; }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 25
Inheritance 3
public double getSpeed() { return this.maxSpeed; } // setter method for the license plate property public void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; } // accelerate to maximum speed // put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 26
Inheritance: the Superclass
public class MotorVehicle { private String licensePlate; // e.g. "New York A456 324" private double speed; // kilometers per hour private double maxSpeed; // kilometers per hour private String make; // e.g. "Harley-Davidson", "Ford" private String model; // e.g. "Fatboy", "Taurus" private int year; // e.g. 1998, 1999, 2000, 2001, etc. private int numberPassengers; // e.g. 4 // constructors public MotorVehicle(String licensePlate, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers); } public MotorVehicle(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { // I could add some more constraints like the // number of doors being positive but I won't // so that this example doesn't get too big. this.licensePlate = licensePlate; this.make = make; this.model = model; this.year = year; this.numberPassengers = numberOfPassengers; if (maxSpeed >= 0.0) { this.maxSpeed = maxSpeed; } else { maxSpeed = 0.0; } if (speed < 0.0) { speed = 0.0; } if (speed <= maxSpeed) { this.speed = speed; } else { this.speed = maxSpeed; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 27 // getter (accessor) methods public String getLicensePlate() { return this.licensePlate; } public String getMake() { return this.make; } public String getModel() { return this.model; } public int getYear() { return this.year; } public int getNumberOfPassengers() { return this.numberPassengers; } public int getNumberOfPassengers() { return this.numberWheels; } public double getMaxSpeed() { return this.speed; } public double getSpeed() { return this.maxSpeed; } // setter method for the license plate property protected void setLicensePlate(String licensePlate) { this.licensePlate = licensePlate; }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 28
// accelerate to maximum speed // put the pedal to the metal public void floorIt() { this.speed = this.maxSpeed; } public void accelerate(double deltaV) { this.speed = this.speed + deltaV; if (this.speed > this.maxSpeed) { this.speed = this.maxSpeed; } if (this.speed < 0.0) { this.speed = 0.0; } } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 29
The Motorcycle subclass
public class Motorcycle extends MotorVehicle { private int numberWheels = 2; // constructors public Motorcycle(String licensePlate, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers); } public Motorcycle(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { // invoke superclass constructor super(licensePlate, speed, maxSpeed, make, model, year, numberOfPassengers); } public int getNumberOfWheels() { return this.numberWheels; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 30
The Car subclass
public class Car extends MotorVehicle { private int numberWheels = 4; private int numberDoors; // constructors public Car(String licensePlate, double maxSpeed,String make, String model, int year, int numberOfPassengers, int numberOfDoors) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers, numberOfDoors); } public Car(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, speed, maxSpeed, make, model, year, numberOfPassengers, 4); } public Car(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers, int numberOfDoors) { super(licensePlate, speed, maxSpeed, make, model, year, numberOfPassengers); this.numberDoors = numberOfDoors; } public int getNumberOfWheels() { return this.numberWheels; } public int getNumberOfDoors() { return this.numberDoors; } }
7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 31
Multilevel Inheritance
public class Compact extends Car { // constructors public Compact(String licensePlate, double maxSpeed, String make, String model, int year, int numberOfPassengers) { this(licensePlate, 0.0, maxSpeed, make, model, year, numberOfPassengers); } public Compact(String licensePlate, double speed, double maxSpeed, String make, String model, int year, int numberOfPassengers) { super(licensePlate, speed, maxSpeed, make, model,year, numberOfPassengers, 2); } }