classes and objects part i n.
Skip this Video
Loading SlideShow in 5 Seconds..
Classes and Objects - Part I PowerPoint Presentation
Download Presentation
Classes and Objects - Part I

play fullscreen
1 / 20
Download Presentation

Classes and Objects - Part I - PowerPoint PPT Presentation

milt
111 Views
Download Presentation

Classes and Objects - Part I

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Classes and Objects - Part I

  2. What is an Object? • An Object has two primary components: • state – properties of the object • behavior – operations the object can perform • Examples

  3. Objects in Java • A class defines a new type of Object • To create a Object type to represent a light switch . . . class LightSwitch { // state and behavior here }

  4. Fields • An Object's state is stored in variables called fields • Fields are declared (and optionally initialized) inside the braces of the class • Light switch example with a field . . . class LightSwitch { boolean on = true; }

  5. Methods • An Object's behavior is defined by its methods • Methods, like fields, are written inside the braces of the class • Methods can access the fields (the state) of their object and can change them

  6. Light Switch Example class LightSwitch { boolean on = true; // field boolean isOn() { // returns return on; // the state } void switch() { // changes on = !on; // the state } }

  7. Constructing Objects • We use the new keyword to construct a new instance of an Object • We can assign this instance to a variable with the same type of the Object • Note: classes define new datatypes ! LightSwitch ls = new LightSwitch();

  8. Using Fields and Methods • To access the field of an instance of an Object use instance.field • To access the method of an instance use instance.method(arguments) LightSwitch ls = new LightSwitch(); ls.on; ls.isOn(); ls.switch();

  9. Example Using Light Switch • What does this main method print out? LightSwitch ls = new LightSwitch(); System.out.println(ls.on); ls.switch(); System.out.println(ls.isOn()); true false

  10. Person Example class Person { String name = “Jamal”; int age = 26; String getName() { return name; } void setName(String n) { name = n; } int getAge() { return age; } void setAge(int a) { age = a; } boolean smellsBad(){return true;} }

  11. Constructing Person Objects • To create an instance of the Person class with a name of "George" and an age of 22 • Can we create a Person that has the name George and the age 22 from the moment it is created? • Answer: Yes! Person george = new Person(); george.setName("George"); george.setAge(22);

  12. Constructors • Constructors are special methods used to construct an instance of a class • They have no return type • They have the same name as the class of the Object they are constructing • They initialize the state of the Object • Call the constructor by preceding it with the new keyword

  13. Person Constructor class Person { String name; int age; Person(String n, int a) { name = n; age = a; } // . . . } • Now we can construct George as follows: Person george = new Person("George", 22);

  14. Default Constructor • When you do not write a constructor in a class, it implicitly has a constructor with no arguments and an empty body • Result: every class has a constructor class LightSwitch { // Leaving out the constructor // is the same as . . . LightSwitch() {} }

  15. Multiple Constructors • A class can have multiple constructors class LightSwitch { boolean on; LightSwitch() { on = true; } LightSwitch(boolean o) { on = o; } }

  16. This Keyword • Instance can refer to itself with the keyword this class LightSwitch { boolean on; LightSwitch() { this.on = true; //(same as on=true;) } LightSwitch(boolean on) { this.on = on; } }

  17. Cascading Constructors • A constructor can call another constructor with this(arguments) class LightSwitch { boolean on; LightSwitch() { this(true); } LightSwitch(boolean on) { this.on = on; } }

  18. Classes Recap • Classes have fields to store the state of the objects in the class and methods to provide the operations the objects can perform. • We construct instances of a class with the keyword new followed by a call a constructor method of the class. • We can assign an instance to a variable with a datatype named the same as the class. • If you do not provide a constructor, the class will have one with no arguments and no statements by default.

  19. Apple Example class Apple { String color; double price; Apple(String color, double price) { this.color = color; this.price = price; } Apple(double price) { this("green", price); } String getColor() { return color; } double getPrice() { return price; } void setPrice(double p) { price = p; } }

  20. Apple Quiz • What will these lines print out? Apple a = new Apple("red", 100.0); System.out.println(a.getColor()); System.out.println(a.getPrice()); a.setPrice(50.5); System.out.println(a.getPrice()); Apple b = new Apple(74.6); System.out.println(b.getColor()); System.out.println(b.getPrice()); b.setPrice(a.getPrice()); System.out.println(b.getPrice()); red 100.0 50.5 green 74.6 50.5