Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for.

Similar presentations


Presentation on theme: "Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for."— Presentation transcript:

1 Classes Modeling the Object

2 Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for instances of the class Classes have two parts  Fields – which describe what the class is  Methods – which describe what the class does Each instance of a Class is an object.

3 Java OOP In Java everything is an object or class Objects send messages to teach other by method calls Instance methods belong to a particular object Static methods belong to a particular class

4 Different objects of a class have the same fields and methods. The values in the fields are different for each object.  Each Person has eye color as a field Each instance of a Person would have its own value or color.

5 Example Class Car { String LicensePlate; // “Ill 534 343” double speed; // in miles per hour double maxSpeed; // in miles per hour public void stopCar(){ speed = 0; }

6 Constructing an Object Instantiating an object in Java is done with the keyword new followed by the call to the class’s constructor. Car mycar ; declaration mycar = new Car(); initialization Car mycar = new Car();

7 Member access Accessing the members/fields of an object is done with the dot separator  mycar.speed  mycar.speed = 50;  mycar.stopCar();

8 Objects from a different class Class CarTest { public static void main(String[] args){ Car c = new Car(); c.speed = 55; c.LicensePlate = “Il 564 586” c.maxSpeed = 94.5; }

9 Initializing Fields Fields should be initialized when they are declared. class Car { String licensePlate = “”; double speed = 0; double maxSpeed =0; String toString(){ System.out.println(licensePlate + ” is moving “ + speed + “ miles per hour”); }

10 class CarTest2{ public static void main(String[] args){ Car c = new Car(); c.toString(); } javac Car.java javac CarTest2.java jara CarTest2 is moving at 0.0 miles per hour

11 Methods Each method in Java has a signature similar to other languages Access modifier Return type Name of method Any parameters/ arguments

12 Class Car { String LicensePlate =“”; // “Ill 534 343” double speed = 0; // in miles per hour double maxSpeed = 98.6; // in miles per hour public void stopCar(){ this.speed = 0; } public void floorIt(){ this.speed = this.maxSpeed; }

13 Implied this The “this” reference is not always used Each object has it’s own data and methods and this instance would know what its data is. This reference can also be used to distinguish between local and instance variables.

14 Passing Arguments Altering the fields of an object is usually done by method calls, not direct access to the field. This protects the data. public void accelerate(double speed){ this.speed = this.speed + speed; if(this.speed >this maxSpeed){ this.speed = this.maxSpeed; } if(this.speed < 0.0){ this.speed = 0.0; }

15 Setters and Getters Accessing data and mutating it Setter/ Mutator methods merely set the value of a field to the value specified in the argument. public void setSpeed(double speed) {} Accesors/ getters retrieve data Public double getSpeed() {}

16 Constructors A constructor is required in order to instantiate an object.  If you don’t write one, the compiler will. Constructors have no return type, and the method name is the same as the class type. public Car () {}

17 Constructor function Besides instantiating an object of the class type Constructors initialize the data fields. If you don’t provide a value, all values are set to defaults.

18 Default and Alternate Constructors You can build a constructor with any number of arguments as a way of getting data into your objects. public Car(String license, double speed, double max) {} If you write an alternate,,, and you want a zero argument constructor you must write it.

19 Setting constraints. Setting up constructor and setter methods are the best way to control your data. Public Car(double speed, double max){ this.speed = speed; maxSpeed =(max !> 125 ) ? max : 125 if(this.speed >this maxSpeed){ this.speed = this.maxSpeed; } if(this.speed < 0.0){ this.speed = 0.0; }

20 Access Protection Four levels  Public  Private  Protected  Default aka package

21 Public - Open access from anywhere or any other object Private – the fields of an object or any object of the same class (siblings) Protected – subclasses and classes in same package Default – classes in same package

22 Benefits of Access Protection Allows enforcement of constraints Provides simpler client interface. Clients do not need to know everything, only the public parts Separates interface from implementation, allowing them to vary independently.

23 OK, what is what General guidelines Classes public Fields private Constructors public Getters and setter public Other methods?? Decide as appropriate case by case

24 Constructors in Java

25 Basic Syntax for constructors [Access modifier] (any arguments) { }

26 Constructor Restrictions Modifiers  Public or package for Top level classes No return type Name MUST match class name

27 Default constructor The zero argument constructor  If no constructors are defined, java creates this  The only action by the implicit constructor is to call the superclass constructor insuring that the inherited state is initialized.

28 Overloaded Constructors Differ from default in arguments The first line of every constructor must be either A this call to another constructor in the same class. A super call to a parent constructor.

29 Why you might want to call super explicitly Normally, you won't need to call the constructor for your parent class because it's automatically generated, but there are two cases where this is necessary. You want to call a parent constructor which has parameters (the automatically generated super constructor call has no parameters). There is no parameterless parent constructor because only constructors with parameters are defined in the parent class.

30 Static Initializer Blocks It doesn't have a name, arguments, or any access modifiers, and does not return a value. All we need to declare a static initializer is the keyword "static" and the code enclosed in braces. static { Pi = 3.1415; }


Download ppt "Classes Modeling the Object. Objects model the world Classes are programmer defined types that model the parts of a system Class serve as blueprints for."

Similar presentations


Ads by Google