Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming in Java Habib Rostami Lecture 7.

Similar presentations


Presentation on theme: "Object Oriented Programming in Java Habib Rostami Lecture 7."— Presentation transcript:

1 Object Oriented Programming in Java Habib Rostami Lecture 7

2 Today’s Presentation  Abstract classes ( Some examples )  Interfaces

3 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;} }

4 Casting to super class public class Test { Shape s = new Triangle(1,3); Shape s1 = new Square(2); }

5 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(); }

6 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

7 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 }

8 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) public interface MyStack { public int size(); public boolean isEmpty(); public Object top(); public void push(Object elt); public Object pop(); } states that this is an interface, not a class states that this is an interface, not a class note no bodies for the methods note no bodies for the methods

9 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

10 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

11 Example import java.util.ArrayList; public class ALStack implements MyStack { private ArrayList al; public ALStack() { al = new ArrayList (); } public int size() { return al.size(); } //and versions of isEmpty, push, pop and top, as //in the previous lecture } promises that we will provide bodies for all the MyStack methods promises that we will provide bodies for all the MyStack methods

12 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... }

13 Polymorphism public class Test{ private MyStack s = new new ArrayStack(20); public doSomeThing(){ s.put(new Point(10,10)); }

14 Some java examples java.util.List Interface

15 1.0t15 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();

16 1.0t16 List interface List extends Collection An ordered collection of objects Duplicates allowed

17 1.0t17 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


Download ppt "Object Oriented Programming in Java Habib Rostami Lecture 7."

Similar presentations


Ads by Google