1 / 62

CE203 - Application Programming

CE203 - Application Programming. Part 3. Inheritance 1.

glynis
Download Presentation

CE203 - Application Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CE203 - Application Programming Part 3 CE203 Part 3

  2. Inheritance 1 Suppose we have written a class Person to store information about persons (such as name and date of birth) and wish to write a class to store information about students. A student is a person (with some extra attributes such as a registration number) so we should implement the class Student as a subclass of Person. We can then apply methods from the Person class to objects of type Student and also store objects of type Student in arrays or other collections of persons. CE203 Part 3

  3. Inheritance 2 public class Student extends Person{ private intregno; public Student(String s, Date d,int r) { super(s,d);regno = r; } public intregNumber() { return regno; } public String toString() { return super.toString()+" Regno: "+regno; }} CE203 Part 3

  4. Inheritance 3 To invoke the constructor from the superclass within the subclass constructor we have used the super statement, which, if used, must be the first statement in the constructor. If there is no such statement the no-argument constructor of the superclass will be invoked implicitly; if there is no such constructor the compiler will report an error. The keyword super has also been used to prefix the call of toString to ensure that the Person version is called; if this prefix was not used dynamic binding would result in a recursive call to the Student version. CE203 Part 3

  5. Inheritance 4 Since all students are persons a variable of type Person may refer to an object of type Student: Person harry = new Student(”Harry Potter”, new Date(23,1,1981), 9907077); If we were to apply toString to the variable harry the student version would be called. It is not legal to write int r = harry.regNumber(); since, although we know that harry refers to a student, the variable is of type Person and the Person class has no regNumber method. CE203 Part 3

  6. Inheritance 5 In order to apply the regNumber method to a student referenced by a variable of type person we must perform a downcast, telling the compiler that the variable will refer to a subclass object. int r = ((Student)harry).regNumber(); Note that the highlighted parentheses are necessary since method application has a higher precedence than downcasting; without the parentheses the downcast would be applied to the result of the regNumber call. If the variable harry did not refer to a student an exception of type ClassCastException would be thrown when the code was run. CE203 Part 3

  7. Inheritance 6 Since a student is a person we may of course store students in an array of objects of type Person. If we have such an array and wish to process all of the students in the array (but not non-students) we need to determine whether a particular element is a student. To do this we must use the instanceof operator, which tests whether a superclass object is a member of a particular subclass. CE203 Part 3

  8. Inheritance 7 To print details of all students in an array a of objects of type Person we could use: for (inti = 0; i<a.length; i++) if (a[i] instanceof Student)System.out.println(a[i]); Similarly, to print details of all non-students in the array we could use: for (inti = 0; i<a.length; i++) if (!(a[i] instanceof Student))System.out.println(a[i]); Again the highlighted parentheses are essential. CE203 Part 3

  9. Inheritance 8 Since a student is a person an array of students is an array of persons and we may use an array of students anywhere an array of persons is expected. Hence if Employee is a subclass of Person, but not of Student, and s has been declared as an array of students, the following code fragment is legal: static void changeLast(Person[] p){ p[p.length-1] = new Employee(……);}…… changeLast(s); …… CE203 Part 3

  10. Inheritance 9 Although the code on the previous slide will compile without any problems it will not run successfully since we cannot store an employee in an array of students. When we try to run the code an exception of type ArrayStoreException will be thrown. CE203 Part 3

  11. Inheritance 10 If a method has an argument of type Object[] , any array of objects may be passed to it (but not an array of primitive values). We can use this to write a method which will copy the contents of one array into another: static void arrCopy(Object[] source, Object[] dest){ if (dest.length<source.length) throw new ArrayStoreException(); inti; for (i = 0; i<source.length; i++)dest[i] = source[i];} CE203 Part 3

  12. Inheritance 11 The method on the previous slide will work if the element-types of the two arrays passed as arguments are the same and also if the source array type is a subclass of the destination array type. It will also work in certain other circumstances, as long as all of the elements of the source array are of the correct type for the destination. For example, we can copy an array of type Person into an array of type Student if all of the people in the array happen to be students, but an exception will be thrown if any of the elements is not a student. CE203 Part 3

  13. Abstract Classes 1 Suppose that we wish to write a program to draw and manipulate simple shapes (circles, squares, etc.) on a frame. We might write a square class to store information about squares. public class Square{ private Colorcol; private intxpos, ypos; // centre coords private intslen; // side length public Square(Color c, int x, int y, int s) { col = c; xpos = x; ypos = y; slen = s; } CE203 Part 3

  14. Abstract Classes 2 public void setColor(Color c) { col = c; } public void move(int xdist, int ydist) { xpos += xdist; ypos += ydist; } public void draw(Graphics g) { g.setColor(col); g.drawRect(xpos-slen/2, ypos-slen/2, slen, slen); }} CE203 Part 3

  15. Abstract Classes 3 Assume that we also have Circle and Triangle classes with setColor, move and draw methods and that details of all of the shapes currently displayed are stored in an array called shapes whose item-type is Object. Suppose we wish to change the colour of all of the shapes in the array to blue. We would like to write for (inti = 0; i<shapes.length; i++) shapes[i].setColor(Color.blue); but cannot do this since the Object class has no method called setColor. CE203 Part 3

  16. Abstract Classes 4 The solution is to introduce a new class called Shape with a setColor method and declare all of the shape classes as subclasses of this class. We can then use an array of Shape objects instead of an array of items of type Object. The new class should be declared as an abstract class – this means that all of its members must be instances of one of its subclasses and explicit calls to new Shape(……) are not allowed. We should place the attributes and methods that are common to all shapes in the Shape class, as seen on the next slide. CE203 Part 3

  17. Abstract Classes 5 public abstract class Shape{ protected Color col; protected int xpos, ypos; public Shape(Color c, int x, int y) { col = c; xpos = x; ypos = y; } public void setColor(Color c) { col = c; } public void move(int xdist, int ydist) { xpos += xdist; ypos += ydist; }} CE203 Part 3

  18. Abstract Classes 6 The instance variables have been declared as protected so that the subclasses can access them in their draw methods. Since the Square class will now inherit attributes and methods from Shape it will be much more concise. public class Square extends Shape{ private intslen; // side length public Square(Color c, int x, int y, int s) { super(c, x, y); slen = s; } public void draw(Graphics g) // as before} CE203 Part 3

  19. Abstract Classes 7 Since the draw methods for each individual shape are different they must be written in the individual classes. However, to allow a loop such as for (inti = 0; i<shapes.length; i++) shapes[i].draw(g); to be written, the Shape class must have a draw method. The use of dynamic binding means that this method will never be called so we could simply write a method that does nothing. However, the preferred approach is to declare the method as abstract. CE203 Part 3

  20. Abstract Classes 8 public abstract class Shape{ protected Colorcol; protected intxpos, ypos; public Shape(Color c, int x, int y) { col = c; xpos = x; ypos = y; } // setColor and move methods as before public abstract void draw(Graphics g); } Note that only abstract classes can have abstract methods. CE203 Part 3

  21. Interfaces 1 An interface is essentially an extreme example of an abstract class – all of its methods are abstract. There are, however, some significant differences between interfaces and abstract classes: • the only variables that an interface can have are public static final ones (i.e. constants) • an interface can not have any constructors • a class does not inherit from an interface but instead is said to implement it and uses the keyword implements instead of extends CE203 Part 3

  22. Interfaces 2 The most common use of interfaces is to provide a specification of the methods of a class that is to be used in one package but implemented in different ways in many others – ActionListener is a typical example; classes that implement it are written by the programmer but used by classes in the java.awt.event package. A class may implement more than one interface: class Silly implements ActionListener, Collection, Comparable CE203 Part 3

  23. Comparing Objects 1 Suppose we have a Date class such as the one seen previously and wish to compare some dates that have been input with a specific date. If we try to use code such as Date xmasDay = new Date(25,12,2013);Date theDay = Date.getDate();if (theDay==xmasDay) …… we will find that the result of the comparison is always false. When comparing references to two objects they are regarded as equal only if they refer to the same object. CE203 Part 3

  24. Comparing Objects 2 To determine if two objects have the same contents we should use an equals method that compares the contents, overriding the equals method inherited from the Object class: if (theDay.equals(xmasDay)) …… To override the inherited version an equals method must have an argument of type Object and downcast its argument. An equals method for the Date class is provided on the next slide. CE203 Part 3

  25. Comparing Objects 3 public boolean equals(Object o){ // check if the argument is a non-null date if (o!=null && o instanceof Date) { Date arg = (Date)o; return y==arg.y && m==arg.m && d==arg.d; } else // if the argument isn’t a date they cannot // be equal return false;} CE203 Part 3

  26. The Java Collections Framework 1 The Java Collections framework provides a number of classes that can be used to store various kinds of collections of objects. These kinds are distinguished by properties such as whether duplicate elements are allowed and whether the elements have a defined order. Methods that are common to all collection classes are declared in an interface called Collection from the package java.util. CE203 Part 3

  27. The Java Collections Framework 2 public interface Collection{ public boolean isEmpty(); public int size(); public boolean contains(Object o); public boolean containsAll(Collection c); public boolean add(Object o); public boolean addAll(Collection c); public void clear(); CE203 Part 3

  28. The Java Collections Framework 3 public boolean remove(Object o); public boolean removeAll(Collection c); public boolean retainAll(Collection c); public Object[] toArray(); public Object[] toArray(Object[] a); public Iterator iterator(); public int hashCode(); public boolean equals(Object o);} CE203 Part 3

  29. The Java Collections Framework 4 The contains and containsAll methods use equals to compare elements; the latter returns true if the collection contains at least one instance of each of the elements of the argument. The add method may fail if an attempt is made to add an element that is already present to a collection that does not allow duplicates; hence a result is returned to indicate whether the addition was successful. For the same reasons the addAll method may add to the collection some, all or none of the elements in its argument; it will return true if at least one element has been added. CE203 Part 3

  30. The Java Collections Framework 5 The remove method attempts to remove one instance of its argument from the collection; if the argument occurs more than once, other instances will be retained. The result indicates whether an instance was found and removed. The removeAll method will remove all instances of all of the elements contained in the argument; true will be returned if at least one element has been removed. The retainAll method will remove all elements that are not contained in the argument; true will be returned if at least one element has been removed. CE203 Part 3

  31. The Java Collections Framework 6 The toArray methods return an array containing all of the elements of the collection; duplicate elements in the collection will occur more than once in the array, so the number of items in the array will be equal to the size of the collection. The version without an argument will always return a new array of type Object[]. The other version allows existing arrays to be reused by attempting to store the elements of the collection in the array supplied as an argument. If this array is too small a new array of the same type as the argument will be created. An exception of type ArrayStoreException may be thrown if any of the elements cannot be stored in the array due to a type mismatch. CE203 Part 3

  32. The Java Collections Framework 7 The behaviour of the equals method depends on the kind of collections being compared – two sets are equal if they contain the same elements but two lists are equal only if they contain the same elements in the same order. The result will always be false if an attempt is made to compare two collection objects of different types, even if they contain the same elements. [ The hashCode and iterator methods will be explained later. ] CE203 Part 3

  33. The Java Collections Framework 8 A simple program fragment demonstrating the use of methods from the Collection interface is presented on the next slide. We have to select a particular class that implements the interface. Note that the elements of collections are objects – when we use the int variable i as an argument to a method from the Collection interface a new Integer object containing the value i is implicitly created. If we were to modify this program fragment to use, for example, Vector instead of TreeSet the behaviour would be different since vectors may contain duplicate elements whereas sets may not. CE203 Part 3

  34. The Java Collections Framework 9 Collection col = new TreeSet();col.add(4); int i; for (i = 0; i<6; i++) if (col.add(i)) System.out.println("added "+i); else System.out.println("could not add "+i); for (i = 2; i<5; i++) col.remove(i); for (i = 0; i<7; i++) if (col.contains(i)) System.out.println("found "+i); else System.out.println("could not find "+i); CE203 Part 3

  35. The Java Collections Framework 10 We now provide a method that will remove from a collection of objects of type String all strings whose length is greater than 10. If the collection contains any non-strings an exception will be thrown since downcasting will fail. static void removeLongStrings(Collection c){ Object[] arr = c.toArray(); for (inti = 0; i<arr.length; i++) { String s = (String)arr[i]; if (s.length()>10)c.remove(s); }} CE203 Part 3

  36. The Java Collections Framework 11 Note that if the collection contained two instances of the same long string this string would appear twice in the array and hence both instances would be removed. The method is rather inefficient; having found a long string in the array, the remove method when called will have to search for it again in the collection. We could implement a much more efficient version by moving through the collection an item at a time, removing long strings as we find them. We shall later see how to do this using an iterator. CE203 Part 3

  37. Vectors 1 The Vector class provides objects similar to arrays but whose size can change. This class was provided in versions of the Java library that pre-dated the introduction of the Collection interface, but extra methods were added to it in subsequent versions to enable it to implement the interface. A vector has both a size and a capacity – the size refers to the number of elements currently held in the vector whereas the capacity refers to the amount of memory space currently allocated. The initial capacity may be supplied as an argument to a constructor when the vector is created. CE203 Part 3

  38. Vectors 2 At any time the size of a vector must be less than or equal to the capacity; if the addition of an element would cause the size to become larger than the capacity the latter will be increased. To optimise use of space the capacity should be increased by a small amount, but to optimise performance a larger increase would be preferred, reducing the number of times the capacity has to change. To allow the programmer to select a trade-off between these two conflicting criteria, the amount by which the capacity should be increased can be specified in a constructor; if this has not been done the size will be doubled. CE203 Part 3

  39. Vectors 3 The capacity of a vector is never decreased automatically when items are removed, but there is a method called trimToSize that will reduce the capacity to the current size. This would normally be used only if no more items are going to be added to the vector or the capacity has become much larger than the size. In addition to the size method specified in the Collection interface the Vector class has a method called capacity that returns the current capacity. CE203 Part 3

  40. Vectors 4 The Vector class has four constructors. The first has no arguments and sets the capacity to 10. The second constructor takes an argument of type int and uses that value for the initial capacity. The third takes two arguments of type int, the first specifying the initial capacity and then second the amount by which the capacity should be increased when necessary. All of these initialise the vector to be empty. The fourth constructor takes an argument of type Collection and copies the contents of the collection into the vector. CE203 Part 3

  41. Vectors 5 In order to permit a vector to be used like an array, methods are provided to examine and replace the contents of a particular location. The elementAt method is used to examine the contents (indexing starts from 0 as with arrays). An ArrayIndexOutOfBoundsException will be thrown if the argument is out of range. There are also methods called firstElement and lastElement. These will throw an exception of type NoSuchElementException if the vector is empty. These exception types are subclasses of RunTimeException. CE203 Part 3

  42. Vectors 6 To replace the contents of a specific location in a vector the setElementAt method can be used. This takes two arguments, a reference to an object and the index of the location in which it is to be stored. There is also a method called insertElementAt that shifts elements to make room for a new one and a method called removeElementAt which will also shift the elements. These methods may throw an exception of type ArrayIndexOutOfBoundsException. CE203 Part 3

  43. Vectors 7 The add method as specified in the Collection interface will append an element to the end of the vector and will always return true. The remove method will remove the first instance of the element if there is more than one occurrence. CE203 Part 3

  44. Vectors 8 In addition to the contains method specified in the Collection interface there is a method that will find the location of an item if it is present in the vector. This method, called indexOf, will return the location of the first occurrence of its argument, or the value -1 if the argument is not found in the vector. There is also a lastIndexOf method, and two-argument versions of both methods that start searching from a particular location (specified by a second argument) instead of from the beginning or end of the vector. CE203 Part 3

  45. Vectors 9 Here is a method to determine how many times an item occurs in a vector. public static int occurrences(Vector v, Object o){ int count = 0; int pos = v.indexOf(o); while (pos!=-1) { count++; pos = v.indexOf(o, pos+1); } return count;} CE203 Part 3

  46. Vectors 10 How does the Vector class differ from ArrayList? In what context would you use Vector and when would you use ArrayList? CE203 Part 3

  47. Generics / Parameterised Types As a consequence of the introduction of generics in Java 5.0 collection classes and interfaces may now be parameterised by types, e.g. Collection<Integer>. If a type parameter is provided the argument and result types of the methods will be more specific than Object, so, for example, for an object of type Vector<Date> the add method would expect an argument of type Date and the elementAt method would return a Date object. CE203 Part 3

  48. The Set Interface The Set interface extends the Collection interface, but adds no extra definitions: public interface Set extends Collection {} Classes that implement this interface must therefore provide all of the methods specified in the Collection interface and should exhibit the expected properties of sets (e.g. no duplicate elements are allowed, two sets with the same elements are equal). The collections framework provides two classes that implement the interface, HashSet and TreeSet. CE203 Part 3

  49. Iterators 1 Iterators can be used to access the contents of a collection one-by-one. An iterator may be thought of as a sequence of elements, together with a place-marker that lies between adjacent elements in this sequence. The sequence comprises all of the elements in the collection; for a hash set the order in which the elements occur in the sequence is unspecified, for a list or vector the elements will appear in the same order as in the collection. CE203 Part 3

  50. Iterators 2 The iterator method specified in the Collection interface will return an iterator for the collection to which it is applied; the place-marker will be in front of the first element of the sequence. The iterator will implement the Iterator interface, which specifies three methods: public interface Iterator{ public booleanhasNext(); public Object next(); public void remove();} CE203 Part 3

More Related