Docsity

Abstract Classes and Interfaces, Slides of Object Oriented Programming

Toggle sidebar
bg1
Abstract Classes And Interfaces
Circle@x
Circle
Shape
Object
Rect@z
fields for
(x, y) coords
Rect
Shape
Object
Teach using the problem of
using objects to represent
shapes in the plane
fields for
length, width
fields for
(x, y) coords
field for
radius
Every shape has a position
(x, y) in the plane, so use a
superclass Shape to hold the
point.
Subclass has necessary
fields to describe shape.
Circle@y
Circle
Shape
Object
fields for
(x, y) coords
field for
radius
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Abstract Classes and Interfaces and more Slides Object Oriented Programming in PDF only on Docsity!

Abstract Classes And Interfaces

Circle@x Circle Shape Object Rect@z fields for (x, y) coords Rect Shape Object Teach using the problem of using objects to represent shapes in the plane fields for length, width fields for (x, y) coords field for radius Every shape has a position (x, y) in the plane, so use a superclass Shape to hold the point. Subclass has necessary fields to describe shape. Circle@y Circle Shape Object fields for (x, y) coords field for radius

Every subclass has a different area() function

We are dealing with shapes that have areas: Circles, Rectangles, Triangles, Polyhedrons, Squares, etc. Circle@x … area()

Circle Shape … Object Rect@y … area()

Rect Shape … Object Rect@z … area()

Rect Shape … Object Rect@z … area()

Rect Shape … Object Therefore, each subclass has a (different) function area(), which returns its area.

Motivating abstract classes

Shape[] Circle@x … area()

Circle Shape … Object b Shape@y … Shape … Object Rect@z … area()

Rect Shape … Object 0 1 2 3 Rect@z … area()

Rect Shape … Object b[ 1 ].area() is illegal, even though each Subclass object has function area() Don’t want to cast down! Instead, define area() in Shape Cast? if (b[1] instanceof Rect) r= ((Rect)b[1]).area();

Motivating abstract classes

Shape[] area() in class Shape doesn’t return useful value Circle@x … area()

Circle Shape … Object b Rect@y … area()

Rect Shape … Object Trian@z … area()

Trian Shape … Object area() area() area() 0 1 2 3 4 … … Trian@z … area()

Trian Shape … Object public double area() { return 0.0; } Problem: How to force subclasses to override area? Problem: How to ban creation of Shape objects area()

Can extend only one class

public class C extends C1, C2 { public void p() { …; h= m(); … } } public class C1 { public int m() { return 2; } … } public class C2 { public int m() { return 3; } … } if we allowed multiple inheritance, which m used?

About interfaces

Can extend only one class

public class C extends C1, C2 { … } public abstract class C1 { public abstract int m(); public int p() {…} } public abstract class C2 { public abstract int m(); public int q(){…} } Use abstract classes? Seems OK, because method bodies not given! But Java does not allow this, because abstract classes can have non-abstract methods Instead, Java has a construct, the interface, which is like an abstract class but has more restrictions.

Interface declaration and use of an

interface

public class C implements C1, C2 { … } public interface C1 { int m(); int p(); int FF= 32; } public interface C2 { int m(); int q(); } Methods declared in interface are automatically public, abstract Use of public, abstract is optional Use ; not { … } Field declared in interface automatically public, static, final Must have initialization Use of public, static, final optional Eclipse: Create new interface? Create new class, change keyword class to interface C must override all methods in C1 and C

Casting with interfaces

class B extends A implements C1, C2 { … } interface C1 { … } interface C2 { … } class A { … } b= new B(); What does object b look like? A Object B Draw b like this, showing only names of partitions: Add C1, C2 as new dimensions:

C1 C

Object b has 5 perspectives. Can cast b to any one of them at any time. Examples: (C2) b (Object) b (A)(C2) b (C1) (C2) b You’ll see such casting later

Shape[] Want to sort b by shape areas. Don’t want to write a sort procedure —many already exist. Avoid duplication of effort! Circle@x … area()

Circle Shape … Object b Rect@y … area()

Rect Shape … Object Trian@z … area()

Trian Shape … Object area() area() area() 0 1 2 3 4 … … Trian@z … area()

Trian Shape … Object area() b could be sorted on many things: area distance from (0,0) x-coordinate …

Sort array of Shapes

Shape[] Want to sort b by shape areas. Don’t want to write a sort procedure —many already exist. Avoid duplication of effort! Circle@x … area()

Circle Shape … Object b Rect@y … area()

Rect Shape … Object Trian@z … area()

Trian Shape … Object area() area() area() 0 1 2 3 4 … … Trian@z … area()

Trian Shape … Object area()

Sort array of Shapes

Solution: Write a function compareTo that tells whether one shape has bigger area than another. Tell sort procedure to use it.

1 Which class should implement Comparable? Shape Object Circle Comparable First idea: all the subclasses Circle, Rect, … Doesn’t work! Each element of b has static type Shape, and compareTo isn’t available in Shape partition Shape[] b= … … Shape Object Circle Comparable Use this. Shape must implement Comparable Shape[] b

docsity.com

1 Shape should implement Comparable Shape[] b= … … Arrays.sort(b); Shape Object Circle Rect … Triangle Comparable Shape[]@ … In class java.util.Arrays: public static void sort (Comparable[] a) {…} b (^) Shape[]@ a ?? Comparable[] Shape[] Shape[]@ Cast from Shape[] to Comparable[] happens automatically docsity.com

Beauty of interfaces : Arrays.sort sorts an array or list C[] for any class C, as long as C implements interface Comparable —and thus implements compareTo to say which of two elements is bigger. Java Library static methods: Arrays.sort(Comparable[] a) Collections.sort(List list) Class arrays has many other useful static methods