Presentation is loading. Please wait.

Presentation is loading. Please wait.

OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.

Similar presentations


Presentation on theme: "OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects."— Presentation transcript:

1 OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects

2 OOP in Java : © W. Milner 2005 : Slide 2 Objects  OOP programs make and use objects  An object has data members (fields)  An object has methods  The program can tell an object to carry out one of its methods

3 OOP in Java : © W. Milner 2005 : Slide 3 Classes  A class is a type of object  Objects belong to classes  An object instantiates a class  Most classes have several objects - instances  A class is defined in a source code file with the same name as the class

4 OOP in Java : © W. Milner 2005 : Slide 4 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 public class Product { public int barcode; public int stockLevel; }

5 OOP in Java : © W. Milner 2005 : Slide 5 Using the Product class  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 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; }

6 OOP in Java : © W. Milner 2005 : Slide 6 Adding a display method  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 public class Product { public void display() { System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stockLevel); System.out.println("========================="); } public int barcode; public int stockLevel; }

7 OOP in Java : © W. Milner 2005 : Slide 7 Using the display method  We tell products p1 and p2 to do their display methods.  Try this code out public class First { public static void main(String[] args) { Product p1 = new Product(); p1.barcode=3; p1.stockLevel=20; p1.display(); Product p2 = new Product(); p2.barcode=4; p2.stockLevel=60; p2.display(); }

8 OOP in Java : © W. Milner 2005 : Slide 8 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.

9 OOP in Java : © W. Milner 2005 : Slide 9 Product constructor public class Product { public Product(int initBarcode, int initStockLevel) { barcode=initBarcode; stockLevel=initStockLevel; } public void display() { System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stockLevel); System.out.println("========================="); } public int barcode; public int stockLevel; } check how a constructor is named

10 OOP in Java : © W. Milner 2005 : Slide 10 Using the constructor  new invokes constructor  Check how initial values are passed in 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(); }

11 OOP in Java : © W. Milner 2005 : Slide 11 Task  Define an Employee class  2 data members – name (String) and payrollNumber (int)  Define a constructor  Define a display method  In First, instantiate and display 2 Employees  Keep this for later use

12 OOP in Java : © W. Milner 2005 : Slide 12 Methods which take parameters public class Product {..previous code omitted public void deliver(int howMany) { stockLevel+=howMany; } public int barcode; public int stockLevel; } parameter

13 OOP in Java : © W. Milner 2005 : Slide 13 Using the deliver method Product p1 = new Product(3,20); p1.deliver(10); p1.display(); Product p2 = new Product(4,60); p2.deliver(20); p2.display();

14 OOP in Java : © W. Milner 2005 : Slide 14 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

15 OOP in Java : © W. Milner 2005 : Slide 15 methods which return values  This is a method of the Product class  See how the return type (boolean) is included, like a C function return type public boolean needMore() { if (stockLevel==0) return true; else return false; }

16 OOP in Java : © W. Milner 2005 : Slide 16 Using the new method Product p1 = new Product(3,20); p1.deliver(10); p1.sell(40); if (p1.needMore()) p1.deliver(50); p1.display();

17 OOP in Java : © W. Milner 2005 : Slide 17 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..

18 OOP in Java : © W. Milner 2005 : Slide 18 Encapsulation  This is part of the definition of the class Product  Data members should be declared private not public  Rest of code runs unaltered, but.... private int barcode; private int stockLevel;..

19 OOP in Java : © W. Milner 2005 : Slide 19 Attempt to access private member from another class: C:\Walter\java\javaprogs\First.java:11: stockLevel has private access in Product p1.stockLevel=22; ^ 1 error Tool completed with exit code 1 Product p1 = new Product(3,20); p1.deliver(10); p1.sell(40); if (p1.needMore()) p1.deliver(50); p1.display(); p1.stockLevel=22;

20 OOP in Java : © W. Milner 2005 : Slide 20 Validating access  Methods which alter data members should validate the change  Here the deliver method of Product checks for –ve quantity delivered 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();

21 OOP in Java : © W. Milner 2005 : Slide 21 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

22 OOP in Java : © W. Milner 2005 : Slide 22 Typical get method:.. public int getStockLevel() { return stockLevel; }.. private int stockLevel; } Product p1 = new Product(3,20); int x = p1.getStockLevel(); System.out.println(x); using it

23 OOP in Java : © W. Milner 2005 : Slide 23 Task  Go back to the Employee class  Make the data members private  Add public get and set methods  Check it works

24 OOP in Java : © W. Milner 2005 : Slide 24 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..

25 OOP in Java : © W. Milner 2005 : Slide 25 Overloading example  One version delivers a default 100 units  Second version allows specifying the delivery quantity public void deliver() { stockLevel+=100; } public void deliver(int howMany) { if (howMany<0) { System.out.println("Invalid delivery"); return; } else stockLevel+=howMany; }

26 OOP in Java : © W. Milner 2005 : Slide 26 Constructor overloading  Constructors are usually overloaded  Such as..

27 OOP in Java : © W. Milner 2005 : Slide 27 Constructor overloading public Product(int initBarcode, int initStockLevel) { barcode=initBarcode; stockLevel=initStockLevel; } public Product(int initBarcode) { barcode=initBarcode; stockLevel=100; } Product p1 = new Product(3,20); p1.display(); Product p2=new Product(4); p2.display();  One defaults to an initial stock level of 100  Other allows to specify it  In use..

28 OOP in Java : © W. Milner 2005 : Slide 28 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

29 OOP in Java : © W. Milner 2005 : Slide 29 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..

30 OOP in Java : © W. Milner 2005 : Slide 30 Using a static field  Revised constructor  barcode and stocklevel have different values for each Product object  There is a single lastBarcodeUsed value for the class.. public Product() { lastBarcodeUsed++; barcode=lastBarcodeUsed; stockLevel=100; }.. private static int lastBarcodeUsed=0; private int barcode; private int stockLevel;..

31 OOP in Java : © W. Milner 2005 : Slide 31 Using the revised constructor Product p1 = new Product(); Product p2=new Product(); Product p3=new Product(); p1.display(); p2.display(); p3.display();

32 OOP in Java : © W. Milner 2005 : Slide 32 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");

33 OOP in Java : © W. Milner 2005 : Slide 33 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

34 OOP in Java : © W. Milner 2005 : Slide 34 Review  Review the product class definition we have:

35 OOP in Java : © W. Milner 2005 : Slide 35 Product class definition public class Product { private static int lastBarcodeUsed=0; private int barcode; private int stockLevel; public Product() { lastBarcodeUsed++; barcode=lastBarcodeUsed; stockLevel=100; } public Product(int initStock) { lastBarcodeUsed++; barcode=lastBarcodeUsed; stockLevel=initStock; } What are these called? What is this? Two things same name – called what? Why are these private?

36 OOP in Java : © W. Milner 2005 : Slide 36 More Product public static int count() { return lastBarcodeUsed; } public void display() { System.out.println("Barcode = "+barcode); System.out.println("Stocklevel = "+stockLevel); System.out.println("========================="); } Why is this static? Why is this int? Why is this void?

37 OOP in Java : © W. Milner 2005 : Slide 37 Rest of Product public void deliver(int howMany) { if (howMany<0) { System.out.println("Invalid delivery"); return; } else stockLevel+=howMany; } public int getStockLevel() { return stockLevel; } Why do this? When do we use getXXX? What are these methods called?

38 OOP in Java : © W. Milner 2005 : Slide 38 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

39 OOP in Java : © W. Milner 2005 : Slide 39 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();

40 OOP in Java : © W. Milner 2005 : Slide 40 Object array exercise  Make an array of 10 Employee objects  Give them data and display them


Download ppt "OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects."

Similar presentations


Ads by Google