Download presentation
Presentation is loading. Please wait.
Published byDominick Butler Modified over 8 years ago
1
ABSTRACT DATA TYPES, ABSTRACT CLASSES AND INTERFACES And abstract art….
2
What is a type? All objects have a type. Is Tom Brady of type 'defensive lineman' or 'quarterback'? Is Wisconsin of type 'state' or 'continent'? Is Homer Simpson of type 'cartoon character' or 'pastry'? A type is defined by two sets A set of values A set of operations on those values Consider the int type for example What is the set of values? An ‘int’ must be at integer number in the interval [-2,147,483,648 to 2,147,483,647] What are the operations? +, -, *, /, % etc…
3
Does Java have types (ADTs)? Consider more complex types such as TV What might the values be? VOLUME – an int in the range [0, 100] STATUS – a boolean CHANNEL – an int in the range [0, 999] What might the operations be? powerOn/powerOff increaseVolume/decreaseVolume upChannel/downChannel
4
A Class in Java IS an ADT A class has a name and defines the set of values that instances may take on the set of operations that can be performed on those instances From the TV example on the previous slide: Might a TV have the following values? {VOLUME=32, STATUS=true, CHANNEL=31} {VOLUME=99, STATUS=12, CHANNEL=true} Given a TV instance named ‘tv’ could we perform the following: tv.unplug() tv.increaseVolume()
5
Class Example Values are defined by instance variables Operations are defined by public methods public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } }
6
What is the 'state' of an object? The state of an object is given by the values of all instance variables. In a sense, the state is the 'value' of the object. Consider the following code and give the state of the relevant object after each highlighted line of code is executed. 6 public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } public class TV { private int channel; private int volume; private boolean status; public TV( ) { channel = 0; volume=0; status = false; } public void powerOn() { … } public void increaseVolume() { … } public void upChannel() { … } } TV lg1 = new TV(); lg1.powerOn(); lg1.increaseVolume(); lg1.upChannel(); TV lg2 = new TV(); lg2.powerOn(); lg2.upChannel(); TV lg1 = new TV(); lg1.powerOn(); lg1.increaseVolume(); lg1.upChannel(); TV lg2 = new TV(); lg2.powerOn(); lg2.upChannel();
7
What does the word 'abstract' really mean? 7 What does the term 'abstract' in 'abstract art' convey? Abstract artists capture the essence without the details (non- representational). Abstract in programming essentially means “not completely defined” In Java, two things can be labeled as abstract: Method Class Captures a property or behavior of an object while not defining the implementation. It specifies only the values and operations (the 'what') Does not specify the operational details (the 'how')
8
Abstract thinking in Java 8 Abstract Methods: Only the signature is known. modifiers/return type/method name/formal parameters/exceptions The method body (implementation) is unknown. Abstract Classes: Contain one or more abstract methods. Cannot be instantiated Non-abstract sub-classes must implement all abstract methods
9
Abstract Class Example 9 public abstract class Shape { private double width, height; public abstract double getArea(); Shape(double w, double h) { width = w; height = h; } public String toString() { return “[" + width + "," + height + “,“ + getArea() + “]”; }
10
Abstract Class Example 10 class Rectangle extends Shape { Rectangle(double w, double h) { super(w,h); } public double getArea() { return width * height; } public String toString() { return “Rectangle” + super.toString(); }
11
Abstract Class Example 11 public class Demo { public static void main(String[] args) { Shape[] shapes = new Shape[5]; // Initialize the elements of the array at random for(int i=0; i<shapes.length; i++) { int width = (int)(Math.random() * 30) + 1; int height = (int)(Math.random() * 30) + 1; // Half of the shapes are ovals and half are rectangles // The type of shape is determined at runtime if(Math.random() <.5) { shapes[i] = new Oval(width, height); } else { shapes[i] = new Rectangle(width, height); } // Compute and print the total area of all shapes in the array double totalArea = 0; for(int i=0; i<shapes.length; i++) { System.out.println(shapes[i]); totalArea += shapes[i].getArea(); } System.out.println(“Total area of all shapes is “ + totalArea); } public class Demo { public static void main(String[] args) { Shape[] shapes = new Shape[5]; // Initialize the elements of the array at random for(int i=0; i<shapes.length; i++) { int width = (int)(Math.random() * 30) + 1; int height = (int)(Math.random() * 30) + 1; // Half of the shapes are ovals and half are rectangles // The type of shape is determined at runtime if(Math.random() <.5) { shapes[i] = new Oval(width, height); } else { shapes[i] = new Rectangle(width, height); } // Compute and print the total area of all shapes in the array double totalArea = 0; for(int i=0; i<shapes.length; i++) { System.out.println(shapes[i]); totalArea += shapes[i].getArea(); } System.out.println(“Total area of all shapes is “ + totalArea); }
12
What is an interface in Java? 12 An interface is a purely abstract class. It contains only abstract methods final variables Any implementing class will contain all of the specified methods and the interface imposes no constraints on how the methods will work (although it does describe what they will do). To be useful, an interface must be implemented by another class. If class A implements interface B then class A defines all methods in B. Represents an is-a relationship between A and B. A class can implement multiple interfaces All interface methods must be implemented
13
Interface Example (Comparable) 13 Consider comparing two variables in Java to determine if one variable is less-than another. if(a < b) { … } What are the constraints on the types of variables a and b? Can variables a and b be String (text) objects? Can they be TV objects? Can they be Color objects? Comparable is a built-in interface defining a method for comparing complex objects. Implementing classes must define how comparison should be done.
14
Interface Example (Comparable) 14 public interface Comparable { public int compareTo(T other); } Compares this object with the specified other object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. SignMeaninga.compareTo(b) NegativeThis is less than Thata < b ZeroThis is equal to Thata = b PositiveThis is greater than Thata > b
15
Interface Example (Comparable Shapes) 15 public abstract class Shape implements Comparable { private double width, height; public abstract double getArea(); Shape(double w, double h) { width = w; height = h; } public String toString() { return “[" + width + "," + height + “,“ + getArea() + “]”; } public int compareTo(Object rhs) { Shape other = (Shape)rhs; if(getArea() < other.getArea()) return –1; else if(getArea() == other.getArea()) return 0; else return 1; }
16
Interfaces essentially allow “multiple inheritance” 16 Object Number String Integer Date > Comparable > Comparable The Integer, String and Date classes all implement the Comparable interface. They all claim to be “Comparable” objects. In UML, implementing and interface is indicated by the dashed lines. The Integer, String and Date classes all implement the Comparable interface. They all claim to be “Comparable” objects. In UML, implementing and interface is indicated by the dashed lines.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.