Real Objects SWEN 501 Co 1 The world
Real Objects SWEN 501 C&o: 1 The world contains many real objects for example: • David and Sue are both objects of kind Student • SWEN 501 and COMP 112 are objects of kind Course 1. The students are of a different class to the courses. 2. Different classes of object can do different things 3. Different objects can be in different states. 4. Many objects can be of the same class.
SWEN 501 C&o: 2 Of Classes and Objects • A Class is the kind of a thing • An object is of some Class Student David Course SWEN 501 Sue COMP 112
SWEN 501 C&o: 3 Java Classes and Objects • Two Classes Student and Course • public class Student { … } • Two student objects David and Sue • Student s = new Student(“David”); • Two course objects SWEN 501 and COMP 112 Student Course SWEN 501 David Sue COMP 112
Classes in Java SWEN 501 C&o: 4 • Student. java defines the class Student • Define Student related things in Student. java • Fields (hold data) name, ID, … • Methods (can be executed) pay, enrol, …. • Constructor methods are used to build objects • Have same name as Class • Have no return type • Used to construct Object of the Class • Best understood by an example.
Example Class Student. java SWEN 501 C&o: 5 Fields Constructor Methods
Java Objects SWEN 501 C&o: 6 • You can make many objects from one class: • Student x = new Student(“David”); • All of them have the same methods as each other: • Any Student object can be asked to do the same things • All of them have the same fields, but their own copies of them • The fields store the object’s “state” • private String name; • Each object has its own name field, that can change independently • The this variable means the current object • this. name is the “private String name” from above • this. describe() calls the “public void describe()” method • A class is a template for objects with the same set of: • Private data, in the fields • Public methods, providing actions the object can perform
Fields on Classes and Objects SWEN 501 C&o: 7 • One Class Student but many student objects “david”, … • private String name; • private int id; • Adds a field “name” and one “id” to each student • private static int Sid = 0; • static fields are added to the Class but can be seen by each student. Student Sid = 2 David id = 1 Sue id = 2
static SWEN 501 C&o: 8 • A static modifier means something belongs to the class itself • This could be a field: • private static int instance. Count = 0; • Then every object made from the class shares the same field • It could also be a method: • public static void main(String[] args) { … } • public static void println(String text) { … } • These methods can be called on the class itself: • UI. println(“Hello”) • They can only access static methods and fields • private String name; • public static void go() { UI. println(this. name); } // Doesn’t work!
SWEN 501 C&o: 9 Primitives and Objects int x = 1; x z x names a piece of memory in which the value 1 is stored Car z = new Car(2); 1 Car 21 Fx 2 The value in z is the “reference” to the Object - it tells the machine how to find the object
Methods SWEN 501 C&o: 10 • You’ve used lots of methods already • The methods that you found on library objects (println, add) that you called • The methods that were pre-defined in the exercises (map. Square, do. Display) that you implemented • A class can declare many methods, and every object made from the class gets all of them • A method declaration has: • • • A visibility modifier, who can use it: public A return type, what the method gives back: String A name: get. Name A parameter list in parentheses, which can be empty: (), (int index) And a body in braces: { return name; } public String get. Name() { return name; }
Calling methods on objects SWEN 501 C&o: 11 • You’ve done this before: Array. List<String> people = …; people. add(“Bob”); Student s = new Student(“Jim”); UI. println(s. get. Name()); this. do. Display(); • To call a method takes: • • A name of the object: people, s, this A dot: . The name of the method you’re using: add, get. Name, do. Display A list of arguments in parentheses (which might be empty): (“Bob”), () • A method can give you back a value to use with return …;
Accessors SWEN 501 C&o: 12 • Because fields are usually private, make public methods to access them: private int age; • A getter method is called get. X() for a name X: public int get. Age() { return age; } • A setter method is called set. X(T value) for the type T: public void set. Age(int new. Age) { this. age = new. Age; } • Sometimes the methods get more complicated • For these simple ones, Eclipse can generate the accessors for you.
SWEN 501 C&o: 13 References • Can have multiple references to the same object: List<Student> a = new Student(“Bob”); List<Student> b = a; • Both b and a are now names for the same object • If we modify the list, we will see the changes from both names: a. add(new Student(“Jim”)); UI. println(b. size()); => 2 • Assigning to a variable copies the reference to the object, not the object itself • This is very useful! • It’s also a source of bugs where you have an alias you didn’t intend.
Main method SWEN 501 C&o: 14 • A method called exactly: • public static void main(String[] args) • Can be in any class, but often given one of its own • Automatically runs when the program starts • … and the program ends when it does
- Slides: 14