Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 13 Abstract Classes and Interfaces

Similar presentations


Presentation on theme: "Chapter 13 Abstract Classes and Interfaces"— Presentation transcript:

1 Chapter 13 Abstract Classes and Interfaces
CIS265/506 Cleveland State University – Prof. Victor Matos Adapted from: Introduction to Java Programming: Comprehensive Version, Eighth Edition by Y. Daniel Liang

2 Abstract Classes and Abstract Methods
More general More specialized 2 This example was introduced in Chapter 11.

3 Abstract Classes and Abstract Methods
Aside - Useful links: An UML Tutorial Google’s Draw-io: A free-ware UML drawing tool 3

4 Example1:Superclasses and Subclasses
public abstract class GeometricObject { // Return TRUE is object is filled private String color = "white"; public boolean isFilled() { private boolean filled; return filled; private java.util.Date dateCreated; // Construct a default geometric object // Set filled to either: true/false protected GeometricObject() { public void setFilled(boolean filled) { dateCreated = new java.util.Date(); } // createa obj with color and filled value protected GeometricObject(String color, boolean filled) { // Get dateCreated public java.util.Date getDateCreated() { this.color = color; return dateCreated; this.filled = filled; // Return a string representing this object // Return color public String toString() { public String getColor() { return "created on " return color; + dateCreated + "\ncolor: " + color + " and filled: " + filled; // Set a new color public void setColor(String color) { // Abstract method getArea public abstract double getArea(); // Abstract method getPerimeter public abstract double getPerimeter(); 1 2 2 4 5

5 Example1: Superclasses and Subclasses
public class Circle4 extends GeometricObject { private double radius = 1; /** Return diameter */ public double getDiameter() { public Circle4() { return 2 * radius; } public Circle4(double radius) { super(); /** Return perimeter */ this.radius = radius; public double getPerimeter() { return 2 * radius * Math.PI; public Circle4(double radius, String color, /* Print the circle info */ boolean filled) { public void printCircle() { super(color, filled); System.out.println(toString() + " Circle created on: " + getDateCreated() //setColor(color); //setFilled(filled); + " and the radius is " + radius); /** Return radius */ public String toString() { public double getRadius() { return ">>> Circle " + getColor() return radius; + "\n" + super.toString() + "\n>>> Radius: " + getRadius() + " Perimeter: " + getPerimeter() /** Set a new radius */ + " Area: " + getArea(); public void setRadius(double radius) { /** Return area */ public double getArea() { return radius * radius * Math.PI; 1 4 2 3 3 5

6 Example1: Superclasses and Subclasses
public class Rectangle extends GeometricObject { private double width; /** Return height */ private double height; public double getHeight() { return height; public Rectangle() { } /** Set a new height */ public Rectangle(double width, public void setHeight(double height) { double height) { super(); this.width = width; this.height = height; /** Return area */ public double getArea() { return width * height; public Rectangle( double width, double height, String color, /** Return perimeter */ boolean filled) { public double getPerimeter() { super(color, filled); return 2 * (width + height); // setColor(color); public String toString() { // setFilled(filled); return "\n>>> Rectangle " + getColor() // Return width + "\n" + super.toString() public double getWidth() { + "\n>>> Height: " + getHeight() return width; + " Width: " + getWidth() + " Perimeter: " + getPerimeter() // Set a new width + " Area: " + getArea(); public void setWidth(double width) { 2 3 4 3 5

7 Abstract Classes and Abstract Methods
An abstract class, like any other Java class, consists of class variables, constructors, mutators, and user-defined (concrete and abstract) methods. Unlike concrete classes they cannot be instantiated . Abstract classes are good for: specifying virtual methods via signatures, those methods must be implemented by descendents of the abstract class. Providing some concrete although very general methods and variables for subclasses to use Example: public abstract double computeTaxes(int ssn, int w2Nbr) ; One of my subclasses must compute taxes using SSN and W2 form. If returned value is positive then “you owe”, a negative value means “a refund” 7

8 abstract method in abstract class
Some regulations: An abstract method cannot be contained in a non-abstract class (concrete classes must have concrete methods). If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be defined abstract. In other words, in a non-abstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass. 8

9 object cannot be created from abstract class
More rules: An abstract class cannot be instantiated using the new operator. However you can still define its constructors, which are invoked in the constructors of its subclasses. For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class. 9

10 superclass of abstract class may be concrete
A subclass can be abstract even if its superclass is concrete. Example: The Object class is concrete, but its subclasses, such as GeometricObject, may be abstract. 10

11 abstract class as type Output:
Although you cannot create an instance from an abstract class, an abstract class can be used as a data type. The following fragment is correct: GeometricObject g1 = new Circle(2, "RED", true); System.out.println( g1.toString() ); System.out.println( g1.getClass() ); System.out.println( g1 instanceof GeometricObject ); Output: >>> Circle RED created on Fri Feb 03 17:11:00 EST 2012 color: RED and filled: true >>> Radius: 2.0 Perimeter: Area: csu.matos.Circle true Concrete subclass 11

12 Example: The Abstract Calendar and the Date class
An instance of java.util.Date represents a ‘concrete’ specific instant in time with millisecond precision. java.util.Calendar is an abstract base class for extracting detailed information such as year, month, date, hour, minute and second from a Date object. Subclasses of Calendar can implement specific calendar systems such as Gregorian calendar, Lunar Calendar, Jewish calendar, and Chinese calendar. Currently, java.util.GregorianCalendar for the Gregorian calendar is supported in the Java API. 12

13 Example: The Abstract Calendar and the Date class
Java.util.Date Aside-1 Date Class Dates are represented as a signed long that counts the number of milliseconds since midnight, January 1, 1970, GMT Time. Java.util.Date has many deprecated methods to manipulate date/time Recommended approach is to use a java.text.DateFormat object to obtain a meaningful string out of a date 13

14 Example: The Abstract Calendar and the Date class
Java.text.DateFormat Aside-2 DateFormat Class Date now = new Date(); DateFormat df = DateFormat.getDateInstance(); String s = df.format(now); System.out.println("Today is " + s); DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT); DateFormat df2 = DateFormat.getDateInstance(DateFormat.MEDIUM); DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG); DateFormat df4 = DateFormat.getDateInstance(DateFormat.FULL); String s0 = df.format(now); String s1 = df1.format(now); String s2 = df2.format(now); String s3 = df3.format(now); String s4 = df4.format(now); System.out.println("(Default) Today is " + s0); System.out.println("(SHORT) Today is " + s1); System.out.println("(MEDIUM) Today is " + s2); System.out.println("(LONG) Today is " + s3); System.out.println("(FULL) Today is " + s4); // old deprecated style, bad-habit int hour = now.getHours(); int min = now.getMinutes(); int sec = now.getSeconds(); System.out.println ( hour + ":" + min + ":" + sec ); Today is Feb 16, 2012 (Default) Today is Feb 16, 2012 (SHORT) Today is 2/16/12 (MEDIUM) Today is Feb 16, 2012 (LONG) Today is February 16, 2012 (FULL) Today is Thursday, February 16, 2012 15:43:50 14

15 Example: The Abstract Calendar Class and Its GregorianCalendar subclass
Aside 3- A note from Wikipedia Lunar calendars differ as to which day is the first day of the month. For some lunar calendars, such as the Chinese calendar, the first day of a month is the day when an astronomical new moon occurs in a particular time zone. For others, such as some Hindu calendars, each month begins on the day after the full moon or the new moon. Others were based in the past on the first sighting of a lunar crescent, such as the lunisolar Hebrew calendar (19-years cycle). Pope Gregory XIII ordered in 1582 adoption of a new calendar to replace the Julian calendar (in use since 45BC). The average length of a year under the Julian system was 365days and 6 hours. This was about11 minutes longer than the correct value (365d 5h 49m 12s). In 13 centuries this error grew up to be ten days. The Pope decreed on 24 February 1582, that the day after Thursday, 4 October 1582 would be not Friday, 5 October, but Friday, 15 October 1582. Hindu Calendar Maya Apocalypse Calendar Gregory XIII 1582 15

16 Example: The Abstract Calendar Class and Its GregorianCalendar subclass
No. Name Days 1 January 31 2 February 28 or 29 3 March 4 April 30 5 May 6 June 7 July 8 August 9 September 10 October 11 November 12 December Aside 4- A note from Wikipedia The Gregorian solar calendar counts days as the basic unit of time. Years have 365 or 366 days; and repeats completely every 400 years. Of these 400 years, 303 common years have 365 days and 97 leap years have 366 days. A calendar mean year has exactly /400 days = days = 365 days, 5 hours, 49 minutes and 12 seconds. Month length is L = 30 + { [ M + floor(M/8) ] MOD 2 }, where L is the month length in days and M is the month number 1 to 12. The expression is valid for all 12 months, but for M = 2 (February) adjust by subtracting 2 and then if it is a leap year add 1. 16

17 Example: The Abstract Calendar Class and Its GregorianCalendar subclass

18 Example: The GregorianCalendar Class
You can use new GregorianCalendar() to construct a default GregorianCalendar with the current time. Calendar calendar = new GregorianCalendar(); Use new GregorianCalendar(year, month, date) to construct a GregorianCalendar with the specified year, month, and date. Calendar calendar = new GregorianCalendar(2525, 0, 31); Date d = cal.getTime(); System.out.println ( d ); // Mon Jan 01 00:00:00 EST 2525 The month values are 0-based, days are not 0 is for January, 11 for December. 1 for Sunday, 7 for Saturday 18

19 Example: Fields contained in the Calendar class
Constant Description YEAR The year of the calendar. MONTH The month of the calendar with 0 for January. DATE The day of the calendar (1-31). HOUR The hour of the calendar (12-hour notation). HOUR_OF_DAY The hour of the calendar (24-hour notation). MINUTE The minute of the calendar. SECOND The second of the calendar. DAY_OF_WEEK The day number within the week with 1 for Sunday. DAY_OF_MONTH Same as DATE. DAY_OF_YEAR The day number in the year with 1 for the first day of the year. WEEK_OF_MONTH The week number within the month. WEEK_OF_YEAR The week number within the year. AM_PM Indicator for AM or PM (0 for AM and 1 for PM). Usage: Calendar calendar = new GregorianCalendar(); int minute = calendar.get(Calendar.MINUTE); int year = calendar.get(Calendar.YEAR); int hourOfDay = calendar.get(Calendar.HOUR_OF_DAY); 19

20 Example: Getting Date/Time Information from Calendar
import java.util.*; public class TestCalendar { public static void main(String[] args) { // Calendar calendar1 = new Calendar() // ERROR!!! Calendar is abstract // Construct a Gregorian calendar for the current date and time Calendar calendar = new GregorianCalendar(); System.out.println("Current time is " + new Date()); System.out.println("YEAR:\t" + calendar.get(Calendar.YEAR)); System.out.println("MONTH:\t" + calendar.get(Calendar.MONTH)); System.out.println("DATE:\t" + calendar.get(Calendar.DATE)); System.out.println("HOUR:\t" + calendar.get(Calendar.HOUR)); System.out.println("HOUR_OF_DAY:\t" + calendar.get(Calendar.HOUR_OF_DAY)); System.out.println("MINUTE:\t" + calendar.get(Calendar.MINUTE)); System.out.println("SECOND:\t" + calendar.get(Calendar.SECOND)); System.out.println("DAY_OF_WEEK:\t" + calendar.get(Calendar.DAY_OF_WEEK)); System.out.println("DAY_OF_MONTH:\t" + calendar.get(Calendar.DAY_OF_MONTH)); System.out.println("DAY_OF_YEAR: " + calendar.get(Calendar.DAY_OF_YEAR)); System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.DAY_OF_YEAR)); 20

21 Example: Getting Date/Time Information from Calendar
System.out.println("WEEK_OF_MONTH: " + calendar.get(Calendar.WEEK_OF_MONTH)); System.out.println("WEEK_OF_YEAR: " + calendar.get(Calendar.WEEK_OF_YEAR)); System.out.println("AM_PM: " + calendar.get(Calendar.AM_PM)); // Construct a calendar for September 11, 2001 Calendar calendar2 = new GregorianCalendar(2001, 8, 11); System.out.println("September 11, 2001 is a " + dayNameOfWeek(calendar2.get(Calendar.DAY_OF_WEEK))); } public static String dayNameOfWeek(int dayOfWeek) { if(dayOfWeek == 1) return "Sunday"; else if(dayOfWeek == 2) return "Monday"; else if(dayOfWeek == 3) return "Tuesday"; else if(dayOfWeek == 4) return "Wednesday"; else if(dayOfWeek == 5) return "Thursday"; else if(dayOfWeek == 6) return "Friday"; else if(dayOfWeek == 7) return "Saturday"; else return null; 21

22 Example: Getting Date/Time Information from Calendar
Console Current time is Fri Feb 03 20:18:09 EST 2012 YEAR: 2012 MONTH: 1 DATE: 3 HOUR: 8 HOUR_OF_DAY: 20 MINUTE: 18 SECOND: 9 DAY_OF_WEEK: 6 DAY_OF_MONTH: 3 DAY_OF_YEAR: 34 WEEK_OF_MONTH: 1 WEEK_OF_YEAR: 5 AM_PM: 1 September 11, 2001 is a Tuesday 22

23 What is an interface? Why is an interface useful?
An interface is a special Java class that contains only constants and abstract methods. Like abstract classes, they cannot be instantiated using new. The intent of an interface is to specify behavior for objects. For example, you can indicate that the objects are: Comparable Cloneable Serializable Fulfilling any other user-define forms of behavior 23

24 UML Representation of Classes and Interfaces
Java classes use single inheritance. An interface is depicted with a relatively similar UML notation. A Match-Making app may want Client to extend person and implement the interface: LoveCompatible as well as Comparable A banking app may want its Client class to extend Person implement Comparable and ignore the LoveCompatible interface. 24

25 Define an Interface Example:
To distinguish an interface from a class, Java uses the following syntax to define an interface: public interface InterfaceName { constant declarations; method signatures; } Example: public interface Edible { /** Describe how to eat */ public abstract String howToEat(); } 25

26 Example: Using Interfaces
The Edible interface specifies whether an object is edible and if so, it recommends the best way to eat the object. This behavior is accomplished by letting the class for the object implement the Edible interface using the implements keyword. In the example, the classes Chicken and Fruit implement the Edible interface, however DeadlySpider does not. public interface Edible { /** Describe best way to eat it */ public abstract String howToEat(); } 26

27 Example: Using Interfaces
public class TestEdible { public static void main(String[] args) { Object[] objects = {new DeadlySpyder(), new Chicken(), new Apple()}; for (int i = 0; i < objects.length; i++) if (objects[i] instanceof Edible) System.out.println(((Edible)objects[i]).howToEat()); } class Animal { // Data fields, constructors, and methods omitted here class Chicken extends Animal implements Edible { public String howToEat() { return "Chicken: Fry it"; class DeadlySpider extends Animal { abstract class Fruit implements Edible { class Apple extends Fruit { return "Apple: Make apple cider"; class Orange extends Fruit { return "Orange: Make orange juice"; 27

28 Omitting Modifiers in Interfaces
In an interface All data fields are public final static and all methods are public abstract. For this reason, these modifiers can be omitted, as shown below: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.k ). 28

29 Example: The Comparable Interface
Assume you need to compare two Circle objects. You could use a Java fragment such as: if ( circle1.getArea() == circle2.getArea() ){ // do something here... } However to compare two Client objects you may say if ( client1.getId() == client2.getId() ){ // do something here... } Previous ideas are OK, but perhaps a more general way of comparing objects is needed. For example if ( circle1.compareTo(circle2) == 0 ){ // do something here... } 29 29

30 The Comparable Interface
When you try to contrast two arbitrary objects, they should be comparable. The Java interface supporting object comparisons is defined as follows: // interface for comparing objects defined in the java.lang package package java.lang; public interface Comparable { public int compareTo(Object o); } Example: int result = firstObject.compareTo(secondObject); 0 if firstObject == secondObject result > 0 if firstObject > secondObject < 0 if firstObject < secondObject 30

31 Why the Comparable Interface ?
The fundamental goal of the Comparable interface is to decide how to arrange two objects from an arbitrary class according to an intuitive notion of a ‘natural ordering’, sequencing, or arrangement of those kind of elements. For instance, consider the following sets of objects Integers: -n < … <-2 < -1 < 0 < 1 < 2 < … n < n + 1 Strings: Unicode (alphabetic) collating sequence Dates: Jan < Feb < … < Dec Rectangles: < 31

32 String and Date Classes are Comparable
Many classes (e.g., String and Date) in the Java library implement Comparable to define a natural order for the objects. If you examine the source code of these classes, you will see the keyword implements used in the classes, as shown below: All the following expressions are true new String() instanceof String new String() instanceof Comparable new java.util.Date() instanceof java.util.Date new java.util.Date() instanceof Comparable 32

33 Defining Classes to Implement Comparable
The Rectangle class does not provide a method to compare rectangles. However, you can define a new subclass of the Rectangle class that implements Comparable. The instances of this new class are therefore comparable. ComparableRectangle Rectangle = new ComparableRectangle(4, 5); ComparableRectangle rectangle2 = new ComparableRectangle(3, 6); System.out.println( Rectangle,compareTo(rectangle2) ); 33 33

34 Example: Implement Comparable Rectangles
public class ComparableRectangle extends Rectangle implements Comparable { // Construct a ComparableRectangle with specified properties public ComparableRectangle(double width, double height) { super(width, height); } // Implement the compareTo method defined in Comparable public int compareTo(Object obj ) { ComparableRectangle otherRectangle = (ComparableRectangle) obj; if (this.getArea() > otherRectangle.getArea()) return 1; else if (this.getArea() < otherRectangle.getArea()) return -1; else return 0; 34 34

35 Example: Implement Comparable Rectangles
... ComparableRectangle cr1 = new ComparableRectangle(10, 20); ComparableRectangle cr2 = new ComparableRectangle(20, 10); ComparableRectangle cr3 = new ComparableRectangle( 4, 50); Rectangle r4 = new Rectangle(10, 20); int c12 = cr1.compareTo(cr2); // c12 is 0 int c13 = cr1.compareTo(cr3); // c13 is 0 int c23 = cr2.compareTo(cr3); // c23 is 0 int c14 = cr1.compareTo(r4); // produces run-time error int c41 = r4.compareTo(cr1); // syntax error 1 2 3 4 Observation: In this example the ComparableRectangles cr1, cr2, and cr3 are equal (they all have different width and height but they have a similar area -which is the criteria used for establishing the comparison among them). 35 35

36 The Cloneable Interface
A class that implements the Cloneable interface is designated cloneable, and its objects can be duplicated using the clone() method defined in the Object class. package java.lang; public interface Cloneable { } Aside: Observe that this interface is empty. A marker interface does not contain constants or methods.

37 Example: Implementing Cloneable Interface
We will make a better Rectangle class by allowing it to implement the Cloneable and Comparable interfaces. public class Rectangle extends GeometricObject implements Cloneable, Comparable { // variables, constructors, and other methods omitted for editing @Override public int compareTo(Object obj) { Rectangle otherRectangle = (Rectangle) obj; if ( this.getArea() == otherRectangle.getArea() ) return 0; else if ( this.getArea() > otherRectangle.getArea() ) return 1; else return -1; } // Override the protected clone method defined in the Object class public Object clone() throws CloneNotSupportedException { return super.clone(); 1 2 3 37 37

38 Example: Implementing Cloneable Interface
Complex objects including cloneable sub-components require individual declaration on each sub-component (primitive types are exempted) public class Heap<E extends Comparable<E>> implements Cloneable { private ArrayList<E> myArray = new ArrayList<E>(); private int size = 0; // constructors... // accessors... // user-defined methods... // Override the protected clone method defined in the Object class public Heap<E> clone() throws CloneNotSupportedException { Heap<E> clonedHeap = (Heap<E>)super.clone(); clonedHeap.myArray = (ArrayList<E>) myArray.clone(); return clonedHeap; } 38 38

39 Example: The Cloneable Interface
public static void main(String[] args) throws CloneNotSupportedException { Rectangle r1 = new Rectangle(10, 20, "blue", true); Rectangle r2 = (Rectangle) r1.clone(); System.out.println(" r1==r2 is " + (r1 == r2)); ⟵ prints false System.out.println(" r1.equals(r2) is " + r1.equals(r2)); ⟵ prints false System.out.println(" r1.compareTo(r2) is " + r1.compareTo(r2)); ⟵ prints 0 } Rectangles r1 and r2 are two different objects with identical contents. The clone method in the Object class copies each field from the original object to the target object. If the field is of a primitive type, its value is copied. For example, the value of area ( double type) is copied from r1 to r2. If the field is of an object, the reference of the field is copied. For example, the field dateCreated is of the Date class, so its reference is copied into r2. This process is referred to as a shallow copy rather than a deep copy, meaning that if the field is of an object type, the object’s reference is copied rather than its contents. 39

40 Example: Overriding the Equals method
public class Rectangle extends GeometricObject implements Cloneable, Comparable { // variables, constructors, and other methods omitted for editing ... @Override public boolean equals(Object obj) { Rectangle other = (Rectangle) obj; if ( this.getColor().equals(other.getColor() ) && this.getDateCreated().equals(other.getDateCreated() ) && this.getHeight() == other.getHeight() && this.getWidth() == other.getWidth() ) return true; else return false; } You may override the Equals method of the Rectangle class to return true each time the contents of the two compared objects are the same. After this change is made the test code in previous page produces: r1==r2 is false r1.equals(r2) is true r1.compareTo(r2) is 0 40

41 Serializable Interface
//Serialize Circle object Circle circle1 = new Circle(10, "YELLOW", true); System.out.println( circle1.toString() ); FileOutputStream fileOut = new FileOutputStream("c:/temp/myCircleFile.ser"); ObjectOutputStream objOut = new ObjectOutputStream(fileOut); objOut.writeObject(circle1); objOut.close(); fileOut.close(); // Deserialize Circle object FileInputStream fileIn = new FileInputStream("c:/temp/myCircleFile.ser"); ObjectInputStream objIn = new ObjectInputStream(fileIn); Circle p2 = (Circle) objIn.readObject(); System.out.println( p2.toString() ); objIn.close(); fileIn.close(); Assume GeometricObject implements the Serializable Interface A serializable class needs only to write(read) its objects to(from) an Object IO Stream. Serializable classes are allowed to call: readObject( ) & writeObject( ) 41

42 Serializable Interface
//output from previous code fragment After Deserializing DateCreation=Mon Oct 06 17:45:35 EDT 2014 Color=YELLOW Filled=true Circle: Radius=10.0 In reality serializing saves a bit more than just the object’s data. The outstream.writeObject(obj) method writes the specified object (obj) to the ObjectOutputStream (outstream), it also includes the object’s class name, the signature of the class, and the values of the non-transient and non-static fields of the class and all of its supertypes, literal data is saved as strings, and numeric values are stored in binary format.  42

43 Serializable Interface
Extended Example – Reading a file of objects until EOF Exception is risen FileOutputStream outStream = new FileOutputStream("c:/temp/xxx.bin"); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream); objectOutputStream.writeObject(new Circle("red",true,11)); objectOutputStream.writeObject(new Circle("red",true,22)); objectOutputStream.writeObject(new Circle("red",true,33)); objectOutputStream.close(); FileInputStream fileInputStream = new FileInputStream("c:/temp/xxx.bin"); ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); try { while( true ) { GeometricObject g = (GeometricObject) objectInputStream.readObject(); System.out.println( g ); } } catch (EOFException e){ System.out.println( " No more object data..."); } finally { objectInputStream.close(); fileInputStream.close(); 43

44 Interfaces vs. Abstract Classes
In an interface, the data must be constants; an abstract class can have all types of data. Each method in an interface has only a signature without implementation; an abstract class can have concrete methods. Variables Constructors Methods Abstract class No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. Interface All variables must be public static final No constructors. An interface cannot be instantiated using the new operator. All methods must be public abstract instance methods 44

45 Whether to use an interface or a class?
In general, a strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes. For example, a Staff Member is a Person. So their relationship should be modeled using class inheritance. A weak is-a relationship, also known as a ‘is-kind-of’ relationship, indicates that an object possesses certain property. A weak is-a relationship can be modeled using interfaces. For example, all strings are comparable, so the String class implements the Comparable interface. You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. 45

46 Becoming a Better IT Professional
ACM Code of Ethics and Professional Behavior* Rule 2.1 Strive to achieve the highest quality, effectiveness and dignity in both the process and products of professional work. Excellence is perhaps the most important obligation of a professional. The computing professional must strive to achieve quality and to be cognizant of the serious negative consequences that may result from poor quality in a system. Translating the rule to our context Whenever you write a Java class try to make your ‘best effort’; each professionally crafted Java class is expected to implement the following interfaces: Comparable, Comparator, Cloneable, Serializable * For a complete list of rules of conduct see: 46

47 Wrapper Classes Primitive data types (int, long, float,…) are not used as objects in Java (objects add extra processing overhead). However, many Java methods require the use of objects (not primitive types) as arguments. For example, the add( object) method in the ArrayList . Java offers a convenient way to wrap a primitive data type into an object ( e. g., wrapping int into the Integer class, and wrapping double into the Double class). The corresponding class is called a wrapper class. Java provides Boolean, Character, Double, Float, Byte, Short, Integer, and Long wrapper classes for primitive data types. 47

48 Wrapper Classes The wrapper classes do not have zero-argument constructors. The instances of all wrapper classes are immutable, i.e., their internal values cannot be changed once the objects are created. 48

49 Wrappers: The toString, equals, and hashCode Methods
Each wrapper class overrides the toString, equals, and hashCode methods defined in the Object class. With the exception of Boolean all the wrapper classes implement the Comparable interface. Therefore the compareTo method is implemented in the wrapper classes (all but Boolean). 49

50 The Number Class Each numeric wrapper class extends the abstract Number class, which contains the methods doubleValue, floatValue, intValue, longValue, shortValue, byteValue byteValue, shortValue These methods “convert” objects into primitive type values. Subclasses of Number support other methods such as valueOf(string) SIZE MAX_VALUE MIN_VALUE 50

51 Example: Using Class-Wrapper Methods
Integer n1 = new Integer(111); Integer n2 = new Integer("111"); Integer n3 = 111; Integer n4 = Integer.MAX_VALUE; Integer n5 = Integer.MIN_VALUE; Integer n6 = Integer.SIZE; Integer n7 = Integer.decode("1234"); Integer n8 = Integer.parseInt("1234"); Integer n9 = Integer.valueOf("1234"); String s1 = String.valueOf(1234); 51

52 Example: Largest Rectangle in a List
Objective: Various rectangles are placed in a list. Find the location of the largest . Assume the class Rectangle implements Comparable public static void main(String[] args) { Rectangle rectangle; ArrayList list = new ArrayList(); list.add( new Rectangle(10,20) ); list.add( new Rectangle(10,21) ); list.add( new Rectangle(11,15) ); list.add( new Rectangle(12,21) ); list.add( new Rectangle(15,10) ); for (int i=0; i<list.size(); i++) { rectangle = (Rectangle)list.get(i); System.out.println( "Loc: " + i + "\t" + rectangle.toString() ); } int locBiggest = FindPositionBiggestRectangle(list); rectangle = (Rectangle)list.get(locBiggest); System.out.println ( "\nSolution\n" + locBiggest + "\n" + rectangle.toString() ); }//main 1 2 52

53 Example: Largest Rectangle in a List
Objective: Various rectangles are placed in a list. Find the location of the largest . Assume the class Rectangle implements Comparable private static int FindPositionBiggestRectangle(ArrayList list) { int position = -1; if (list.size() == 0 ) return position; Rectangle largest = (Rectangle) list.get(0); Rectangle current; for (int i=1; i<list.size(); i++){ current = (Rectangle) list.get(i); if ( current.compareTo(largest) > 0){ position = i; largest = current; } 53

54 Example: Largest Rectangle in a List
Console Loc: 0 >>> Rectangle white created on Sat Feb 04 22:19:21 EST 2012 color: white and filled: false >>> Height: 20.0 Width: 10.0 Perimeter: 60.0 Area: 200.0 Loc: 1 >>> Height: 21.0 Width: 10.0 Perimeter: 62.0 Area: 210.0 Loc: 2 >>> Height: 15.0 Width: 11.0 Perimeter: 52.0 Area: 165.0 Loc: 3 >>> Height: 21.0 Width: 12.0 Perimeter: 66.0 Area: 252.0 Loc: 4 >>> Height: 10.0 Width: 15.0 Perimeter: 50.0 Area: 150.0 Solution 3 1 2 54

55 BigInteger and BigDecimal
If you need to compute with very large integers or high precision floating-point values, you can use the BigInteger and BigDecimal An instance of BigInteger can represent an integer of any size. The largest integer of the long type is Long. MAX_ VALUE ( i. e., ). Both are part of the java.math package, they extend the Number class and implement the Comparable interface. 55

56 Case Study: The Rational Class ( your turn ! )
56

57 JSON Encoding JSON Encoding
JSON stands for JavaScript Object Notation. This is a text-based “human-readable” and “open-format” standard intended for the representation of structured data. JSON’s original purpose is to act as a language-independent data interchange vehicle. Objects are written in JSON as a curly-braced ({ }) delimited string. The various properties inside the object are recursively annotated as a comma separated set of ‘key:value’ pairs. Array elements are enclosed in square brackets ([ ]) and separated by commas.  JSON is language and platform independent and it is available for a large number of programming environments [Jackson, Gson, Jayrock-Ref-Microsoft].   57

58 JSON Encoding Why JSON ? Positive features:
transparency – the developer can see objects as plain text, it can be used on cross-platform applications involving a variety of operating systems and programming languages, It is relatively easy to use and understand, No need to posses original source code to add Serializable annotation support, The technology is already well understood, diffused, and supported . CAUTION is needed when JSON is used as a data-interchange vehicle over the data network, because as any plain-text message, there are some risks of eavesdropping. 58

59 JSON Encoding Download Google’s GSON Software
Make sure to obtain latest: gsonXX.jar & gsonXX-javadoc.jar Reference: "Reactions to Two Software Approaches for Object Persistence", by Victor Matos, and Rebecca Grasser. International Journal of Applied Science and Technology. Vol. 4, No. 5; October pp Link: 59

60 JSON Encoding Download Encoding Simple Data Types
Get latest gsonXX.jar & gsonXX-javadoc.jar JSON Encoding Download Encoding Simple Data Types // Create an instance of a Gson-JSON encoder Gson gson = new Gson(); // must import com.google.gson.* API // PART1. serialize primitive data elements (strings, numbers) String jsonString = gson.toJson("Game of Thrones"); String javaValue1 = gson.fromJson(jsonString, String.class); System.out.println(javaValue1); // prints “Game of thrones” // PART2. deserialize primitive data elements String jsonString2 = gson.toJson(4321); Integer javaValue2 = gson.fromJson(jsonString2, Integer.class); System.out.println(jsonString2); // prints 4321 Download API (Jar) from: 60

61 JSON Encoding 2. Encoding Simple Objects
// PART1. Serialize a single Person object Gson gson = new Gson(); Person person0 = new Person("Daenerys Targaryen", 18); jsonString = gson.toJson(person0); System.out.println("Json string: " + jsonString ); // PART2. Deserialize a single Person object Person personDecoded = gson.fromJson(jsonString, Person.class); System.out.println("Java object: " + personDecoded ); The previous fragment produces the following output: Json string: {"name":"Daenerys Targaryen","age":18} Java object: Person(name=Daenerys Targaryen, age=18) 61

62 JSON Encoding 3. Encoding a List of Objects
// PART1. create a (Generic) list of Person objects ArrayList<Person> lstPerson = new ArrayList<Person>(); lstPerson.add(new Person("Daenerys Targaryen", 17)); lstPerson.add(new Person("Tiryion Lannister", 30)); lstPerson.add(new Person("Arya Stark", 11)); // convert Java collection to JSON string String jsonList = gson.toJson(lstPerson); System.out.println("Json list: " + jsonList ); // PART2. use Java reflection to find the list's type Type arrPersonType = new TypeToken<ArrayList<Person>>(){}.getType(); ArrayList<Person> lst2 = gson.fromJson(jsonList, arrPersonType); System.out.println("Java list:" + lst2 );  The list of Person objects is encoded as follows: Json list: [ {"name":"Daenerys Targaryen","age":18}, {"name":"Tiryion Lannister","age":30}, {"name":"Arya Stark","age":11} ] 62

63 JSON Encoding 3. Encoding a List of Objects (cont.)
Json list [ {"name":"Daenerys Targaryen","age":18}, {"name":"Tiryion Lannister","age":30}, {"name":"Arya Stark","age":11} ] A JSON array is delimited by square-braces ([ ]). Each element in the array is a Person object including the attributes “name” and “age”. Objects are enclosed inside curly-braces({ }) and separated by commas. The Gson TypeToken class is used to extract the collection’s generic type information 63

64 JSON Encoding 4. Encoding a Subclass
Assume the PersonHouse class is an extension of the Person class. It contains the additional house attribute. Consider the following example: // PART1. Encode subclass PersonHouse person4 = new PersonHouse("Jon Snow", 23, "Stark"); String jsonPerHome = gson.toJson(person4); System.out.println("Json subclass: " + jsonPerHome); // PART2. Decode string PersonHouse person4again = gson.fromJson(jsonPerHome, PersonHouse.class); System.out.println("Java subclass: " + person4again); The output produced by this fragment is Json subclass: {"House":"Stark","name":"Jon Snow","age":23} Java subclass: PersonHouse( House=Stark Person(name=Jon Snow, age=23) ) 64

65 JSON Encoding 5. Encoding a 1:Many Relationship
Assume the existence of a House class including the properties: houseName and characters (represented as a dynamic list of Person objects). ArrayList<Person> theStarks = new ArrayList<Person>(); theStarks.add(new Person("Catelyn Stark", 40)); theStarks.add(new Person("Sansa Stark", 14)); theStarks.add(new Person("Bran Stark", 9)); //PART1. Encoding complex 1:m class House starkHouse = new House("Stark", "Winterfell", theStarks); String jsonHouse = gson.toJson(starkHouse); System.out.println("\nJson House: " + jsonHouse); //PART2. Decoding complex 1:m class House javaHouse = gson.fromJson(jsonHouse, House.class); System.out.println("Java House: " + javaHouse); The output produced by this example is: Json House: {"houseName":"Stark","location":"Winterfell", "personLst":[{"name":"Catelyn Stark","age":40}, {"name":"Sansa Stark","age":14}, {"name":"Bran Stark","age":9}]} 65

66 JSON Encoding Alternative Decoding based on traversing the JSON data structure looking for jsonElements (which could be: jsonObject, jsonArray, or jsonPrimitive tokens) private static void jsonHouseNodeParsing(String jsonHouseStr) { String result = ""; try { JsonElement jelement = new JsonParser().parse(jsonHouseStr); JsonObject jobject = jelement.getAsJsonObject(); String jHouseName = jobject.get("houseName").toString(); String jLocation = jobject.get("location").toString(); // JsonPrimitive jLocation = jobject.getAsJsonPrimitive("location"); System.out.println(jHouseName + "\n" + jLocation); JsonArray jarray = jobject.getAsJsonArray("personLst"); for (int i = 0; i < jarray.size(); i++) { jobject = jarray.get(i).getAsJsonObject(); result = jobject.get("name").toString() + " " + jobject.get("age").toString(); System.out.println(" " + result); } } catch (Exception e) { System.out.println(e.getMessage()); }// jsonNodeParsing 66

67 JSON Encoding Alternative Decoding Approach (continuation)
Example using alternative decoding JSON encoded string Decoded Nodes {"houseName":"Stark", "location":"Winterfell", "personLst":[ {"name":"Catelyn Stark","age":40}, {"name":"Sansa Stark","age":14}, {"name":"Bran Stark","age":9} ] } "Stark" "Winterfell" "Catelyn Stark" 40 "Sansa Stark" 14 "Bran Stark" 9 67


Download ppt "Chapter 13 Abstract Classes and Interfaces"

Similar presentations


Ads by Google