Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
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 s1 = 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 getArea(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, isEmpty, and top • queues offer a constructor, enqueue, dequeue, size, isEmpty 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 timeInSecs; //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 note no public interface MyStack { this is an bodies for public int size(); interface, the methods public boolean isEmpty(); not a class public Object top(); public void push(Object elt); public Object pop(); } 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.ArrayList; • public class ALStack implements MyStack { • private ArrayList al; promises that we will provide bodies for all the MyStack methods • • • public ALStack() { al = new ArrayList (); } • public int size() { return al.size(); } • • • } //and versions of isEmpty, push, pop and top, as //in the previous lecture Example (cont) • • • • public class ArrayStack implements MyStack { private int capacity; private Object[] s; private int top = -1; • • • • public ArrayStack(int reqdCapacity) { capacity = reqdCapacity; s = new Object[capacity]; } • public int size() { return top+1; } • • //and versions of isEmpty, push, pop and top ... } Polymorphism public class Test{ private MyStack s = new new ArrayStack(20); public doSomeThing(){ s.put(new Point(10,10)); } } Some java examples • java.util.List Interface Collection (interface) • A group of objects • Major methods: – int size(); – boolean isEmpty(); – boolean contains(Object); – Iterator iterator(); – Object[] toArray(); – boolean add(Object); – boolean remove(Object); – void clear(); 1.0t 15 List • interface List extends Collection • An ordered collection of objects • Duplicates allowed 1.0t 16 List Details • Major additional methods: – Object get(int); – Object set(int, Object); – int indexOf(Object); – int lastIndexOf(Object); – void add(int, Object); – Object remove(int); – List subList(int, int); • add() inserts • remove() deletes • Implemented by: – ArrayList, LinkedList, Vector 1.0t 17