Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.

Similar presentations


Presentation on theme: "1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data."— Presentation transcript:

1 1 Class and Object Lecture 7

2 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data fields/attributes and methods to define behaviors. Additionally, a class provides a special type of methods, known as constructors, which are invoked to construct objects from the class.

3 3 Class Object A blueprint for objects of a particular type Defines the structure (number, types) of the attributes Defines available behaviors of its objects Attributes Behaviors

4 4 Class: Car Object: a car Attributes: String model Color color int numPassengers double amountOfGas Behaviors: Add/remove a passenger Get the tank filled Report when out of gas Attributes: model = "Mustang" color = Color.YELLOW numPassengers = 0 amountOfGas = 16.5 Behaviors:

5 5 Color Constants There are predefined constants that you can use for colors. Color.BLACKColor.BLUE Color.CYANColor.DARK_GRAY Color.GRAYColor.GREEN Color.LIGHT_GRAYColor.MAGENTA Color.ORANGEColor.PINK Color.REDColor.WHITE Color.YELLOW

6 6 Classes and Source Files Each class is stored in a separate file The name of the file must be the same as the name of the class, with the extension.java public class Car {... } Car.java By convention, the name of a class (and its source file) always starts with a capital letter. (In Java, all names are case-sensitive.)

7 7 public class SomeClass Fields Constructors Methods } Attributes / variables that define the object’s state; can hold numbers, characters, strings, other objects Procedures for constructing a new object of this class and initializing its fields Actions that an object of this class can take (behaviors) { Class header SomeClass.java import... import statements

8 8 Class Definition public class Circle { private double radius, area; final double PI = 3.14159; Circle (double r) { radius=r; } public double findArea() { area= radius*radius*PI; return area; } UML class diagram Circle - radius: double - area: double + findArea(): double

9 9 Data Fields A.k.a. instance variables Constitute “private memory” of an object Each field has a data type (int, double, String, Image, Foot, etc.) Each field has a name given by the programmer

10 10 private [static] [final] datatype name ; Fields (cont’d) Usually private May be present: means the field is a constant primitive: int, double, etc., or an object: String, Image, Foot You name it! May be present: means the field is shared by all objects in the class private Foot leftFoot ;

11 11 Constructors A constructor is a a special kind of methods for creating objects of the class. Constructors do not have a return type (not even void) and they do not return a value. All constructors in a class must have the same name — the name of the class. Constructors may take parameters. Constructors are invoked using the new operator when an object is created. Constructors play the role of initializing objects.

12 12 Creating Objects Using Constructors new ClassName(); Example: new Circle(); new Circle(5.0);

13 13 Default Constructor A class may be declared without constructors. In this case, a no-arg constructor that takes no parameters, with an empty body is implicitly declared in the class. This constructor, called a default constructor, is provided automatically only if no constructors are explicitly declared in the class. In other words: If a programmer does not define any constructors, Java provides one default no-args constructor, which allocates memory and sets fields to the default values.

14 14 Constructors (cont’d) If a class has more than one constructor, they must have different numbers and/or types of parameters. This is called constructor overloading.

15 15 Constructors (cont’d) public class Fraction { private int num, denom; public Fraction ( ) { num = 0; denom = 1; } public Fraction (int n) { num = n; denom = 1; } Continued  public Fraction (int n, int d) { num = n; denom = d; reduce (); } public Fraction (Fraction other) { num = other.num; denom = other.denom; }... } “No-args” constructor Copy constructor

16 16 Constructors (cont’d) A nasty bug: public class MyWindow extends JFrame {... // Constructor: public void MyWindow ( ) {... }... Compiles fine, but the compiler thinks this is a method and uses MyWindow’s default no-args constructor instead.

17 17 Constructors (cont’d) Constructors of a class can call each other using the keyword this — a good way to avoid duplicating code:... public Fraction (int p, int q) { num = p; denom = q; reduce (); }... public class Fraction {... public Fraction (int n) { this (n, 1); }...

18 18 Declaring Object Reference Variables To reference an object, assign the object to a reference variable. To declare a reference variable, use the syntax: ClassName objectRefVar; Example: Circle myCircle;

19 19 Declaring/Creating Objects in a Single Step ClassName objectRefVar = new ClassName(); Example: Circle myCircle = new Circle(); Create an object Assign object reference

20 20 Accessing Objects Referencing the object’s data: objectRefVar.data e.g., myCircle.radius Invoking the object’s method: objectRefVar.methodName(arguments) e.g., myCircle.getArea()

21 21 Example: Declare an object 2 steps are Involved : 1. Declaring Object Reference Variable named circle1 Circle circle1; 2. Creating Objects circle1 = new Circle(2.3); Can combine; Circle circle1 = new Circle(2.3);

22 22 public class Circle { private double radius, area; final PI = 3.14159; Circle (double r) { radius = r; } public double findArea() { area= radius*radius*PI; return area; } public class TestCircle { public static void main(String[] args) { Circle circle1 = new Circle(2.3); System.out.println("The area of the circle of radius "+ circle1.radius + " is " + circle1.findArea()); } File Name? Save as “______.java”


Download ppt "1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data."

Similar presentations


Ads by Google