Abstract Classes and Interfaces               OOSSE - Programming with Java                         Lecture 8Dec 21, 2012 ...
Objectives In this lecture, we will • Discuss Abstract classes • Build class hierarchies based on abstract classes • Intro...
Introduction • The concept of an abstract class will be introduced   through a simple example • Consider designing a simpl...
Rectangle Class • A simple rectangle class:Dec 21, 2012     OOSSE - Java Lecture 8   4
A FilledRectangle class • A filled rectangle is a specialisation   of the rectangle class • Inheritance is used to derive ...
A Picture class • A picture could be made up of a   collection of rectangles and   filled rectangles     – Each of which a...
Extending the Drawing Package • Suppose it is necessary to extend the drawing package to be   able to draw circles • A cir...
Circles and the Picture class • A Picture holds a collection of rectangles and filled   rectangles • A circle is not a rec...
A Shape class • A shape class is one possibility     – A rectangle IS-A shape     – A circle IS-A shape • What is a Shape?...
Abstract Classes in Java • Java allows abstract classes to be defined using the   keyword abstract     – public abstract c...
Shape as an Abstract base class • In together abstract is indicated by   italic text     – Shape     – drawDec 21, 2012   ...
References to Abstract classes • Although abstract classes cannot be instantiated it is   possible to use variables that h...
Interfaces • A class specifies     – Member variables     – Member methods • The methods contain code that defines how to ...
Interfaces • This allows us to consider each of the following in class   design • Inheritance     – The IS-A relationship ...
Interfaces • In Java an interface is specified using the keyword   interface in place of class     – All the methods in an...
Implementing an Interface • A class can implement an interface • In order to do this the class must provide an   implement...
References to Interfaces • It is possible to have a variable that is a reference to a   Drawable     – Drawable theDrawabl...
SimpleMessage - Another class • Suppose another class is written to manage a simple   message • This class could also impl...
Implementing Multiple Interfaces • A class can extend only one base class in Java     – Single inheritance     – Other lan...
Summary In this lecture we have: • Discussed Abstract classes • Built class hierarchies based on abstract classes • Introd...
Upcoming SlideShare
Loading in …5
×

09 abstract classesandinterfaces

303 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
303
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Part of this lecture will be reserved for working through solutions to selected exercises from last week. Notes relating to this do not appear in the slides.
  • 09 abstract classesandinterfaces

    1. 1. Abstract Classes and Interfaces OOSSE - Programming with Java Lecture 8Dec 21, 2012 OOSSE - Java Lecture 8 1
    2. 2. Objectives In this lecture, we will • Discuss Abstract classes • Build class hierarchies based on abstract classes • Introduce interfaces • Discuss implementing and extending within a single classDec 21, 2012 OOSSE - Java Lecture 8 2
    3. 3. Introduction • The concept of an abstract class will be introduced through a simple example • Consider designing a simple drawing package • Initially the package must be able to draw simple objects such as – Rectangles – Filled rectangles • An obvious place to start is identifying candidates for classes and corresponding attributes and methods – a rectangle class – a size, a position – a constructor, a draw methodDec 21, 2012 OOSSE - Java Lecture 8 3
    4. 4. Rectangle Class • A simple rectangle class:Dec 21, 2012 OOSSE - Java Lecture 8 4
    5. 5. A FilledRectangle class • A filled rectangle is a specialisation of the rectangle class • Inheritance is used to derive a FilledRectangle class from the Rectangle class • The derived class – adds a colour attribute – overrides the draw methodDec 21, 2012 OOSSE - Java Lecture 8 5
    6. 6. A Picture class • A picture could be made up of a collection of rectangles and filled rectangles – Each of which are Rectangles – The draw method of picture would ask each object in the vector to draw itself • A collection of base class objects is used – A derived object IS-A base class object – Polymorphism takes place when a rectangle object draws itselfDec 21, 2012 OOSSE - Java Lecture 8 6
    7. 7. Extending the Drawing Package • Suppose it is necessary to extend the drawing package to be able to draw circles • A circle class can be written – A circle has a radius – A circle has a position – A circle needs a draw methodDec 21, 2012 OOSSE - Java Lecture 8 7
    8. 8. Circles and the Picture class • A Picture holds a collection of rectangles and filled rectangles • A circle is not a rectangle therefore is not easy to work with a combination of circles, rectangles and filled rectangles • However, circles and rectangles have some common features – Each has an xPostion and a yPosition – Each knows how to draw itself • These common features could be abstracted out to form a new class – A base class for both rectangles and circlesDec 21, 2012 OOSSE - Java Lecture 8 8
    9. 9. A Shape class • A shape class is one possibility – A rectangle IS-A shape – A circle IS-A shape • What is a Shape? • How would you draw a Shape? – How is draw implemented? • A Shape never exists in its own right • A Shape is used as a base class for a Rectangle or as a base class for a Circle • A Shape is an abstract class – A class that is never instantiatedDec 21, 2012 OOSSE - Java Lecture 8 9
    10. 10. Abstract Classes in Java • Java allows abstract classes to be defined using the keyword abstract – public abstract class Shape …. • An abstract class – Cannot be directly instantiated – Contains at least one abstract method • An abstract method is defined with the keyword abstract – public abstract void draw() ; • An abstract method must be overridden in derived classes – Implemented in the derived class – Unless the derived class is itself abstractDec 21, 2012 OOSSE - Java Lecture 8 10
    11. 11. Shape as an Abstract base class • In together abstract is indicated by italic text – Shape – drawDec 21, 2012 OOSSE - Java Lecture 8 11
    12. 12. References to Abstract classes • Although abstract classes cannot be instantiated it is possible to use variables that hold references to abstract classes • For example: – Shape theShape; • What types of objects can theShape refer to? – Any instances of classes derived from Shape • For example: – Circle myCircle = new Circle ( 40, 50, 50 ); – theShape = myCircle; • What affect does the following code have? – theShape.draw( );Dec 21, 2012 OOSSE - Java Lecture 8 12
    13. 13. Interfaces • A class specifies – Member variables – Member methods • The methods contain code that defines how to carry out the operations defined by the methods – Implementation • Sometimes it is useful to separate what an object can do from how it does it • An interface specifies what an object should be able to do without specifying how or what the object isDec 21, 2012 OOSSE - Java Lecture 8 13
    14. 14. Interfaces • This allows us to consider each of the following in class design • Inheritance – The IS-A relationship • Containment – The HAS-A relationship • Interface – Used to specify that an object is able to do somethingDec 21, 2012 OOSSE - Java Lecture 8 14
    15. 15. Interfaces • In Java an interface is specified using the keyword interface in place of class – All the methods in an interface must be abstract – Only final static member variables are allowed • For example: interface Drawable { public void draw (); }Dec 21, 2012 OOSSE - Java Lecture 8 15
    16. 16. Implementing an Interface • A class can implement an interface • In order to do this the class must provide an implementation of the abstract methods specified in the interface • For example a Circle knows how to draw so it could implement the Drawable interface: public class Circle extends Shape implements Drawable {… • An instance of Circle IS-A Shape • An instance of Circle is able to do the operations specified in the Drawable interfaceDec 21, 2012 OOSSE - Java Lecture 8 16
    17. 17. References to Interfaces • It is possible to have a variable that is a reference to a Drawable – Drawable theDrawable; • What type of object can theDrawable refer to? • Any object that is an instance of a class that implements the Drawable interface – Circle c = new Circle (40, 50, 50); – theDrawable = c; • What methods can be called against theDrawable? – The methods specified in the interface Drawable – In this example only drawDec 21, 2012 OOSSE - Java Lecture 8 17
    18. 18. SimpleMessage - Another class • Suppose another class is written to manage a simple message • This class could also implement the Drawable interface: public class SimpleMessage implements Drawable { private String message; public SimpleMessage( String s) { message = s; } public void draw ( ) { // display message in the status bar …. } }Dec 21, 2012 OOSSE - Java Lecture 8 18
    19. 19. Implementing Multiple Interfaces • A class can extend only one base class in Java – Single inheritance – Other languages allow multiple inheritance • However, a class in Java can implement many interfaces: public class MyClass extends MyBase implements Drawable, Printable • This class has to implement all the methods specified in: – the Drawable interface – and the Printable interface • An instance of MyClass could be used as a Drawable object or as a Printable object – Fit in with systems that expect either type of interfaceDec 21, 2012 OOSSE - Java Lecture 8 19
    20. 20. Summary In this lecture we have: • Discussed Abstract classes • Built class hierarchies based on abstract classes • Introduced interfaces • Discussed implementing and extending within a single classDec 21, 2012 OOSSE - Java Lecture 8 20

    ×