140 likes | 341 Views
university of palestine. What object oriented programming. M.Sc. : Nael M M Alian naelalian@yahoo.com. What object oriented programming. In Java everything is an object or a class (or a piece of one or a collection of several). Objects send messages to each other by calling methods.
E N D
university of palestine What object oriented programming advancedprogramming M.Sc. : Nael M MAlian naelalian@yahoo.com
What object oriented programming • In Java everything is an object or a class (or a piece of one or a collection of several). • Objects send messages to each other by calling methods. • Instance methods belong to a particular object. • Static methods belong to a particular class.
Example : The Car Class • Suppose you need to write a traffic simulation program that watches cars going past an intersection. Each car has: • a speed. • a maximum speed. • a license plate. that uniquely identifies it. In traditional programming languages you'd have two floating point and one string variable for each car. With a class you combine these into one thing like this.
Example 1: The Car Class class Car { String licensePlate; // e.g. “Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } These variables (licensePlate, speed and maxSpeed) are called the member variables, instance variables, or fields of the class. Fields tell you what a class is and what its properties are.
Example 1: The Car Class • An object is a specific instance of a class with particular values (possibly mutable) for the fields. While a class is a general blueprint for objects, an instance is a particular object.
Constructing objects with new class Car { String licensePlate; // e.g. “Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } • To instantiate an object in Java, use the keyword new followed by a call to the class's constructor. Here's how you'd create a new Car variable called c: Car c; c = new Car();
Constructing objects with new • The first word, Car, declares the type of the variable c. Classes are types and variables of a class type need to be declared just like variables that are ints or doubles. • The equals sign is the assignment operator and new is the construction operator. • Finally notice the Car() method. The parentheses tell you this is a method and not a data type like the Car on the left hand side of the assignment. This is a constructor, a method that creates a new instance of a class. You'll learn more about constructors shortly. However if you do nothing, then the compiler inserts a default constructor that takes no arguments.
Constructing objects with new • This is often condensed into one line like this: Car c = new Car();
The Member Access Separator class Car { String licensePlate; // e.g. “Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } • Once you've constructed a car, you want to do something with it. To access the fields of the car you use the . separator. The Car class has three fields • licensePlate • speed • maxSpeed
The Member Access Separator • Therefore if c is a Car object, c has three fields as well: c.licensePlate c.speed c.maxSpeed You use these just like you'd use any other variables of the same type. For instance: Car c = new Car(); c.licensePlate = “Palestine 12546"; c.speed = 70.0; c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour."); The separator selects a specific member of a Car object by name.
Using a Car object in a different class class Car { String licensePlate; // e.g. “Palestine 543 A23" double speed; // in kilometers per hour double maxSpeed; // in kilometers per hour } • The next program creates a new car, sets its fields, and prints the result: class CarTest { public static void main(String args[]) { Car c = new Car(); c.licensePlate = " Palestine 12546 "; c.speed = 70.0; c.maxSpeed = 123.45; System.out.println(c.licensePlate + " is moving at " + c.speed + " kilometers per hour."); } }
Using a Car object in a different class • This program requires not just the CarTest class but also the Car class. To make them work together put the Car class in a file called Car.java. Put the CarTestclass in a file called CarTest.java. Put both these files in the same directory. Then compile both files in the usual way. Finally run CarTest. For example, % javac Car.java % javac CarTest.java % java CarTest Palestine 12546 is moving at 70.0 kilometers per hour. Note that Car does not have a main() method so you cannot run it. It can exist only when called by other programs that do have main() methods.
Initializing Fields • Fields can (and often should) be initialized when they're declared, just like local variables. class Car { String licensePlate = “ "; // e.g. “Palestine 543 A23" double speed = 0.0; // in kilometers per hour double maxSpeed = 123.45; // in kilometers per hour } The next program creates a new car and prints it: class CarTest2 { public static void main(String[] args) { Car c = new Car(); System.out.println(c.licensePlate + " is moving at " + c.speed + "kilometers per hour."); } }
Initializing Fields • For example, • $ javac Car.java • $ javac CarTest2.java • $ java CarTest is moving at 0.0 kilometers per hour.