Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
INTRODUCTION • Java is a true OO language • the underlying structure of all Java programs is classes. • Everything must be encapsulated in a class • that defines the “state” and “behaviour” of each object made from the class. • A class essentially serves as a template for an object and behaves like a basic data type, e.g., “int”. • We are, in essence, defining a type, along with all the functions and properties we want associated with the type we’re defining. • It is therefore important to understand: • how the properties (fields) and methods are defined in a class • how they are used to build a Java program 2 CLASSES • Contains definition for all Objects of the same type • defines the data types and names of properties • defines methods that define behavior • the “template” for all Object instances of the same type Class Name: Circle A class template Data Fields: radius is _______ Methods: getArea Circle Object 1 Circle Object 2 Circle Object 3 Data Fields: radius is 10 Data Fields: radius is 25 Data Fields: radius is 125 Three objects of the Circle class CLASSES • The basic syntax for a class definition: class ClassName { [fields declaration] [methods declaration] } • Bare bone class – no fields, no methods public class Circle { // my circle class } 5 CLASSES class Circle { /** The radius of this circle */ double radius = 1.0; /** Construct a circle object */ Circle() { } Data field Constructors /** Construct a circle object */ Circle(double newRadius) { radius = newRadius; } /** Return the area of this circle */ double getArea() { return radius * radius * Math.PI; } } 6 Method ADDING PROPERTIES (FIELDS): • Add fields public class Circle { double radius; // radius of the circle double x, y; // center coordinate double circ; // circumference } 7 CONSTRUCTORS Constructors are invoked when we first make an object (a variable) of the type of the Class. public class Circle { double radius; Circle() { #What happens when the code says new radius = 1.0; } Circle(double newRadius) { radius = newRadius; } } Notice: no return value – these are not methods! These are constructors. They make a Circle object. 8 ADDING METHODS • A class with only data fields has no life. • Objects created from such a class cannot respond to anything. • Methods are declared inside the body of the class and after the declaration of data fields and the constructors. • The general form of a method declaration is: (just like a function, only it belongs to a certain class) type MethodName (parameter-list) { Method-body; } 9 ADDING METHODS TO CLASS CIRCLE public class Circle { double radius; // radius of circle double x, y; // center of the circle // …Constructors go here //Methods to return circumference and area public double circumference() { return (2*Math.PI*radius); } public double area() { return (Math.PI * Math.pow(radius,2)); } } 10 Method Body ALL TOGETHER public class Circle { double radius; Circle() { radius = 1.0; } Circle(double d) { radius = d; } public double getArea() { return(Math.pow(radius,2.0) * Math.PI); } public static void main(String[] args) { Circle c = new Circle(3.2); System.out.println(c.getArea()); } } 11 Method Body DATA ABSTRACTION • Declare the Circle class, have created a new data type – Data Abstraction • Can define variables (objects) of that type: Circle aCircle; Circle bCircle; 12 CREATING CIRCLE OBJECTS CONT. Circle aCircle; Circle bCircle; • aCircle, bCircle simply refers to a Circle object, not an object itself. (e.g., this is going to be a Circle, but it isn’t yet.) aCircle null Points to nothing (Null Reference) 13 bCircle null Points to nothing (Null Reference) CREATING OBJECTS OF A CLASS • Objects are created using the new keyword. • aCircle and bCircle refer to Circle objects aCircle = new Circle() ; 14 bCircle = new Circle() ; CREATING OBJECTS OF A CLASS aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; Before Assignment aCircle P 15 bCircle Q After Assignment aCircle P bCircle Q AUTOMATIC GARBAGE COLLECTION aCircle P • The object • Q bCircle Q does not have a reference and cannot be used in future. The object becomes a candidate for automatic garbage collection. • Java automatically collects garbage periodically and releases the memory used to be used in the future. 16 ACCESSING OBJECT/CIRCLE DATA ObjectName.VariableName ObjectName.MethodName(parameter-list) Circle aCircle = new Circle(); aCircle.x = 2.0; // initialize center and radius aCircle.y = 2.0; aCircle.radius = 1.0; 17 EXECUTING METHODS IN OBJECT/CIRCLE • Using Object Methods: Gets Circle’s method and runs it for this particular circle Circle aCircle = new Circle(); aCircle.r = 1.0; System.out.println(aCircle.area()); 18 USING CIRCLE CLASS // Circle.java: Contains both Circle class and its user class //Add Circle class code here class MyMain { public static void main(String[] args) { Circle aCircle; // creating reference aCircle = new Circle(); // creating object aCircle.x = 10; // assigning value to data field aCircle.y = 20; aCircle.radius = 5; double area = aCircle.area(); // invoking method double circumf = aCircle.circumference(); System.out.println("Radius="+aCircle.r+" Area="+area); System.out.println("Radius="+aCircle.r+" Circumference ="+circumf); } } Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.40 19 ENCAPSULATION : MAKING STUFF PUBLIC AND PRIVATE • An object instance owns its state and behavior • Java provides access modifiers to define what code can access an object's state and behavior • public • all code can access the tagged state or behavior • private • only instances of the enclosing class may access this tagged state or behavior • Protected (hold that thought for later…) public class Circle { private double rad; private double circ; private double area; EXAMPLE OF PUBLIC VS PRIVATE: public Circle() { rad = 1.0; setValues(); } public Circle(double r) { rad = r; setValues(); } private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public void setrad(double r) { rad = r; setValues(); } public String toString(){ String s = "Radius: "+rad+" Cicumference: "+circ+" Area: "+area; return s; } } public class Circle { public double rad; private double circ; private double area; public Circle() { rad = 1.0; setValues(); } WHAT IF? class Hello2 { public public static void main (String[] args) { Circle c = new Circle(3.2); System.out.println(c); c.rad = 100; System.out.println(c); } public Circle(double r) { } rad = r; setValues(); >> Radius: 3.2 Cicumference: 20.1 Area: 32.16 } >> Radius: 100.0 Cicumference: 20.1 Area: 32.16 private void setValues(){ circ = Math.PI * 2 * rad; area = Math.PI * Math.pow(rad, 2); } public void setrad(double r) { rad = r; setValues(); } public String toString(){ String s = "Radius: "+rad+" Cicumference: "+circ+" Area: "+area; return s; } } THE RULE: • All Fields should be PRIVATE!!!! • Unless there’s a serious concrete reason why you didn’t make it private • Whenever possible, your methods should be private • Always go for the most protected state you can make things • You’re in control, and you want to control what other people who might be using your code have access to. • You want to keep everything private unless it is something that will be used by other people/code that is using your code.