Object Oriented Programming in Java Habib Rostami Lecture
Object Oriented Programming in Java Habib Rostami Lecture 7
Today’s Presentation ØAbstract classes (Some examples) ØInterfaces
Abstract class Shape abstract class Shape { abstract double area (); } class Triangle extends Shape { double b, h; Triangle (double b, double h) { this. b=b; this. h=h; } double area () {return 0. 5*b*h; } } class Square extends Shape { double a; Square (double a) { this. a=a; } double area () {return 0. 5*a*a; } }
Casting to super class public class Test { Shape s = new Triangle(1, 3); Shape s 1 = new Square(2); }
Casting to super class public class Test { Shape[] ar = new Shape[2]; public Test(){ ar[0] = new Triangle(1, 3); ar[1] = new Square(2); } public double get. Area(int i){ return ar[i]. area(); } }
Determining the interface – before writing a class definition, determine the interface • the set of services we offer to clients – similarly, if defining data structures, we should first determine the interface • stacks support a constructor, push, pop, size, is. Empty, and top • queues offer a constructor, enqueue, dequeue, size, is. Empty and front
Data Abstraction – if the interface remains the same, clients don't need to be changed, even if the implementation behind the interface changes public class Time { private int time. In. Secs; //public methods public class Time { private int hours; private int minutes private int secs; } //same public methods //but with different //bodies }
Java Interfaces – Java allows us to take this one stage further, by formally recording the interface as a Java interface – a java interface is just a collection of abstract methods (i. e. we state the signatures, but not the bodies) states that this is an interface, not a class public interface My. Stack { public int size(); public boolean is. Empty(); public Object top(); public void push(Object elt); public Object pop(); } note no bodies for the methods
interfaces vs classes – a class definition can contain instance/class variables and instance/class methods, including both the signature and the body – a java interface contains only the signatures of public instance methods (and named constants) – a java interface acts like a specification • it says what it means to be e. g. a stack • to be a stack, an object must offer at least those methods in its public interface
using Java interfaces – Java allows us to tell the compiler that a class will implement an interface – regard it as a contract stating that you will meet the specification – any class that implements an interface must provide implementations of the public methods (or it must be abstract, and its subclasses provide them) – the compiler will check, and if the bodies are not provided, it won't compile
Example • import java. util. Array. List; • public class ALStack implements My. Stack { • private Array. List al; promises that we • • • public ALStack() { al = new Array. List (); will provide bodies for all the My. Stack methods } • public int size() { return al. size(); } • • • } //and versions of is. Empty, push, pop and top, as //in the previous lecture
Example (cont) • • public class Array. Stack implements My. Stack { private int capacity; private Object[] s; private int top = -1; • • public Array. Stack(int reqd. Capacity) { capacity = reqd. Capacity; s = new Object[capacity]; } • public int size() { return top+1; } • • //and versions of is. Empty, push, pop and top. . . }
Polymorphism public class Test{ private My. Stack s = new Array. Stack(20); public do. Some. Thing(){ s. put(new Point(10, 10)); } }
Some java examples • java. util. List Interface
Collection (interface) • A group of objects • Major methods: – int size(); – boolean is. Empty(); – boolean contains(Object); – Iterator iterator(); – Object[] to. Array(); – boolean add(Object); – boolean remove(Object); – void clear(); 1. 0 t 15
List • interface List extends Collection • An ordered collection of objects • Duplicates allowed 1. 0 t 16
List Details • Major additional methods: – Object get(int); – Object set(int, Object); – int index. Of(Object); – int last. Index. Of(Object); – void add(int, Object); – Object remove(int); – List sub. List(int, int); • add() inserts • remove() deletes • Implemented by: – Array. List, Linked. List, Vector 1. 0 t 17
- Slides: 17