Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 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

4

5 CLASSES The basic syntax for a class definition: Bare bone class – no fields, no methods 5 public class Circle { // my circle class } class ClassName { [fields declaration] [methods declaration] }

6 CLASSES 6

7 ADDING PROPERTIES (FIELDS): Add fields 7 public class Circle { double radius; // radius of the circle double x, y; // center coordinate double circ; // circumference }

8 CONSTRUCTORS 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 Constructors are invoked when we first make an object (a variable) of the type of the Class.

9 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) 9 type MethodName (parameter-list) { Method-body; }

10 ADDING METHODS TO CLASS CIRCLE 10 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)); } Method Body

11 ALL TOGETHER 11 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()); } Method Body

12 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

13 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.) 13 aCircle Points to nothing (Null Reference) bCircle Points to nothing (Null Reference) null

14 CREATING OBJECTS OF A CLASS Objects are created using the new keyword. aCircle and bCircle refer to Circle objects 14 bCircle = new Circle() ; aCircle = new Circle() ;

15 15 CREATING OBJECTS OF A CLASS aCircle = new Circle(); bCircle = new Circle() ; bCircle = aCircle; P aCircle Q bCircle Before Assignment P aCircle Q bCircle After Assignment

16 AUTOMATIC GARBAGE COLLECTION The object 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 Q P aCircle Q bCircle

17 ACCESSING OBJECT/CIRCLE DATA 17 Circle aCircle = new Circle(); aCircle.x = 2.0; // initialize center and radius aCircle.y = 2.0; aCircle.radius = 1.0; ObjectName.VariableName ObjectName.MethodName(parameter-list)

18 EXECUTING METHODS IN OBJECT/CIRCLE Using Object Methods: 18 Circle aCircle = new Circle(); aCircle.r = 1.0; System.out.println(aCircle.area()); Gets Circle’s method and runs it for this particular circle

19 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); } 19 Radius=5.0 Area=78.5 Radius=5.0 Circumference =31.40

20 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…)

21 EXAMPLE OF PUBLIC VS PRIVATE: public class Circle { private double rad; private double circ; private double area; 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; }

22 WHAT IF? public class Circle { public double rad; private double circ; private double area; 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 Hello2 { public static void main (String[] args) { Circle c = new Circle(3.2); System.out.println(c); c.rad = 100; System.out.println(c); } >> Radius: 3.2 Cicumference: 20.1 Area: 32.16 >> Radius: 100.0 Cicumference: 20.1 Area: 32.16

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


Download ppt "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."

Similar presentations


Ads by Google