Java and OOP Part 2 Classes and objects 1
Objects OOP programs make and use objects An object has data members (fields) An object has methods The program can tell an object to execute some of its methods 2
Classes A class is a definition or type for an object Objects belong to classes An object instantiates a class Classes can have several objects - instances A class is defined in a source code file with the same name as the class 3
public class Product public int barcode; public int stocklevel; The Product Class Models a stock control item 2 data members, barcode and stocklevel Defined in file name Product.java Compiled to Product.class Cannot run it no main method 4
Using the Product class public class First public static void main(string[] args) Product p1 = new Product(); p1.barcode=3; p1.stocklevel=20; Product p2 = new Product(); p2.barcode=4; p2.stocklevel=60; Classes start with capital letters Everything else does not This instantiates 2 Product objects ( new ) and sets their data members This can be run but no output 5
public class Product Adding a display method public void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); public int barcode; public int stocklevel; Check out how the display method is defined barcode means the barcode field of the object executing this method NB barcode is not a variable 6
public class First public static void main(string[] args) Product p1 = new Product(); p1.barcode=3; We tell products p1 and p2 to do p1.stocklevel=20; p1.display(); their display methods. Product p2 = new Product(); Run this code out p2.barcode=4; p2.stocklevel=60; p2.display(); Using the display method 7
Constructors A constructor is something which 'makes' an object A class will nearly always have a constructor defined A constructor can have parameters, usually used to give initial values to fields. 8
public class Product public Product(int initbarcode, int initstocklevel) barcode=initbarcode; stocklevel=initstocklevel; Product constructor check how a constructor is named public void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); public int barcode; public int stocklevel; 9
Using the constructor public class First public static void main(string[] args) Product p1 = new Product(3,20); p1.display(); Product p2 = new Product(4,60); p2.display(); new invokes constructor Check how initial values are passed in the constructor 10
Methods which take parameters public class Product..previous code omitted parameter public void deliver(int howmany) stocklevel+=howmany; public int barcode; public int stocklevel; 11
Product p1 = new Product(3,20); p1.deliver(10); p1.display(); Product p2 = new Product(4,60); p2.deliver(20); p2.display(); Using the deliver method 12
Exercise Copy the Product class (constructor, display and deliver methods) Add a sell method. This takes a parameter of how many to sell, and it reduces the stock level. If it is told to sell more than it has, the stock level should become 0. Check it works 13
methods which return values public boolean needmore() if (stocklevel==0) return true; else return false; This is a method of the Product class See how the return type (boolean) is included, like a C function return type 14
Product p1 = new Product(3,20); p1.deliver(10); p1.sell(40); if (p1.needmore()) p1.deliver(50); p1.display(); Using the new method 15
Encapsulation Central to the philosophy of OOP Means data in objects are 'closed up' Other parts of the application cannot accidentally alter data within an object Increases modularity When you use a class, no need to worry about messing it up. How to do it.. 16
Encapsulation.. private int barcode; private int stocklevel;.. This is part of the definition of the class Product Data members should be declared private not public Rest of code runs unaltered, but.. 17
Attempt to access private member from another class: Product p1 = new Product(3,20); p1.deliver(10); p1.sell(40); if (p1.needmore()) p1.deliver(50); p1.display(); p1.stocklevel=22; C:\Walter\java\javaprogs\First.java:11: stocklevel has private access in Product p1.stocklevel=22; ^ 1 error 18
public void deliver(int howmany) if (howmany<0) System.out.println("Invalid delivery"); return; else stocklevel+=howmany; Product p1 = new Product(3,20); p1.deliver(-10); p1.display(); Validating access Methods which alter data members should validate the change Here the deliver method of Product checks for ve quantity delivered 19
Accessor methods Data members should usually be private But often we want to find out the values of those members from outside the class Or to change them Solution public accessor methods Method to 'read' a data member XXX usually called 'getxxx' Methods to 'write' to a data member XXX called 'setxxx' Set methods must validate the change they are making 20
.. public int getstocklevel() return stocklevel;.. private int stocklevel; Typical get method: Product p1 = new Product(3,20); int x = p1.getstocklevel(); System.out.println(x); using it 21
Overloading You can have different versions of the same method with the same name This is called overloading Different versions must have different numbers or types of arguments For example.. 22
public void deliver() stocklevel+=100; public void deliver(int howmany) if (howmany<0) System.out.println("Invalid delivery"); return; else stocklevel+=howmany; Overloading example One version delivers a default 100 units Second version allows specifying the delivery quantity 23
Constructor overloading Constructors are usually overloaded Such as.. 24
Constructor overloading public Product(int initbarcode, int initstocklevel) barcode=initbarcode; One defaults to an initial stocklevel=initstocklevel; stock level of 100 public Product(int initbarcode) Other allows to specify it In use.. barcode=initbarcode; stocklevel=100; Product p1 = new Product(3,20); p1.display(); Product p2=new Product(4); p2.display(); 25
Default no-arg constructors The constructor with no arguments is called 'the no-args constructor' Like Product p = new Product(); If you do not define any constructors, then.. the system calls a default version for you If you do define some (with args), and you call Product p = new Product(); then You must explicitly define the no-arg version 26
Static variables A static or class variable is a piece of data for the whole class, not for individual objects For example, we need to ensure product barcodes are unique. One way is to 'autonumber' them This means the Product class must remember the last one used.. 27
Using a static field.. public Product() lastbarcodeused++; barcode=lastbarcodeused; stocklevel=100;.. private static int lastbarcodeused=0; private int barcode; private int stocklevel;.. Revised constructor barcode and stocklevel have different values for each Product object There is a single lastbarcodeused value for the class 28
Using the revised constructor Product p1 = new Product(); Product p2=new Product(); Product p3=new Product(); p1.display(); p2.display(); p3.display(); 29
Static methods // a static method to the Product class public static int count() return lastbarcodeused; // use it.. Product p1 = new Product(); Product p2=new Product(); Product p3=new Product(); System.out.println("There are now "+Product.count()+" products"); 30
public static void main() This is a method It does not return a value (void) It is static (just one of it- don't need to construct an object) It is public (so we can call it) It is special in that execution starts there as for C 31
Review Review the product class definition we have: 32
Product class definition public class Product What are these called? private static int lastbarcodeused=0; private int barcode; private int stocklevel; Why are these private? public Product() lastbarcodeused++; barcode=lastbarcodeused; stocklevel=100; public Product(int initstock) lastbarcodeused++; barcode=lastbarcodeused; stocklevel=initstock; What is this? Two things same name called what? 33
More Product Why is this static? public static int count() return lastbarcodeused; Why is this int? public void display() System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stocklevel); System.out.println("========================="); Why is this void? 34
Rest of Product public void deliver(int howmany) Why do this? if (howmany<0) When do we use getxxx? System.out.println("Invalid delivery"); What are these methods return; called? else stocklevel+=howmany; public int getstocklevel() return stocklevel; 35
Arrays of objects To have an array of objects there are 2 steps: 1. make the array 2. make the objects to put in the array 36
Arrays of objects // declare the type of stock.. Product stock[]; // call the array constructor to make the array.. stock = new Product[10]; // make 10 objects and put them in the array: for (int i=0; i<10; i++) stock[i]=new Product(); // show they exist: for (int i=0; i<10; i++) stock[i].display(); 37