Introduction to Object Oriented Programming Introduction to Object - - PowerPoint PPT Presentation

introduction to object oriented programming introduction
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 1

Introduction to Object Oriented Programming Introduction to Object Oriented Programming

ปรีดา เลิศพงศวิภูษณะ plw@ku.ac.th ภาควิชาวิศวกรรมคอมพิวเตอร คณะวิศวกรรมศาสตร มหาวิทยาลัยเกษตรศาสตร

slide-2
SLIDE 2

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; } }

slide-3
SLIDE 3

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; } }

slide-4
SLIDE 4

7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 4

Access Protection

Car c = new Car("New York A234 567", 100.0); c.speed = 150.0;

slide-5
SLIDE 5

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; }

slide-6
SLIDE 6

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; } } }

slide-7
SLIDE 7

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."); } }

slide-8
SLIDE 8

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:\>

slide-9
SLIDE 9

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.

slide-10
SLIDE 10

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.

slide-11
SLIDE 11

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; }

slide-12
SLIDE 12

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; } } }

slide-13
SLIDE 13

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.

slide-14
SLIDE 14

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... }

slide-15
SLIDE 15

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); }

slide-16
SLIDE 16

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... }

slide-17
SLIDE 17

7 กรกฎาคม 2547 ปรีดา เลิศพงศวิภูษณะ (plw@ku.ac.th) 17

Operator Overloading

Therefore Java does not support operator overloading.

slide-18
SLIDE 18

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.

slide-19
SLIDE 19

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); }

slide-20
SLIDE 20

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; } }

slide-21
SLIDE 21

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; }

slide-22
SLIDE 22

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; } } }

slide-23
SLIDE 23

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; } }

slide-24
SLIDE 24

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; }

slide-25
SLIDE 25

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; } } }

slide-26
SLIDE 26

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; } }

slide-27
SLIDE 27

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; }

slide-28
SLIDE 28

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; } } }

slide-29
SLIDE 29

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; } }

slide-30
SLIDE 30

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; } }

slide-31
SLIDE 31

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); } }