Lecture 3: More on Classes Textbook: Chapter 2 Recap: –Classes vs. Objects –Q: What comes first a class or an object? –Q: Do we need a class to create an object or vice versa? –Two important parts of classes we discussed on M ? –Fields have data types associated with them e.g: –Parameters vs. Return values (Optional?) –What are keywords? –Signature
Basic Elements A class has three basic elements: public class ClassName { Fields Constructors Methods }
Classes have a wrapper around them. An example for the Circle Class public class Circle { Inner part of the class omitted } The outer wrappings of different classes look pretty much the same, Only the name changes to match the class
The inner workings The inner part of the class deals with the code for fields, constructors and methods It is the fields, constructors and methods that give a class its uniqueness
Different parts of a class: Fields Fields store values for an object. They are also known as instance variables. Use the Inspect option in BlueJ to view an object’s fields. public class Circle { private int diameter ; private int xPosition ; private int yPosition ; private String color ; private boolean isVisible ; }
Field Signature public int diameter; data type field namevisibility modifier Ends with a semicolon What happens if you omit a semicolon?