29
©The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

© The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

  • View
    218

  • Download
    2

Embed Size (px)

Citation preview

Page 1: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Chapter 14

Abstraction, inheritance and interfaces

Page 2: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Abstraction• the idea of focussing on what an object does, without worrying

about the detail of how it does it. • it is particularly relevant at the analysis stage when we are

trying to determine exactly what it is that we want our system to do;

• the more abstract a specification, the more likely we are to build a system that is flexible and maintainable, because we do not tie ourselves down to one particular design;

• a class template is often referred to as an abstract data type;• object-oriented programming languages differ from earlier

languages in that the principal data types that they manipulate are abstract data types;

• in OO development it is possible to broadly define a class, specifying its fundamental behaviour, and concentrating on the important details that make the class what it is.

Page 3: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Run-time versus compile-time binding

Take a look back at program 8.5• when the program is compiled it is not known

whether, on each iteration of the loop, the employee will be full-time or part-time;

• this is decided by the user each time the program is run;

• the technique which makes it possible for this decision to be made is known as run-time binding or dynamic binding.

Page 4: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Compile-time (static) binding

• when the code for a class is compiled, the code for each of its methods is compiled alongside it;

• the compiler ensures that every time an object of that class receives a message to invoke that method, the control of the program would jump to the place where the code for the method was stored.

Page 5: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Run-time (dynamic) binding• in the case of this line:

• System.out.println("Status: " + employeeList[i].getStatus());• the compiler can't write the instruction to jump to a particular place

because there is more than one possible place for it to go.• every time a new object is created, it must hold information about

where its methods are stored;• this is the technique that constitutes run-time binding.• Java normally uses dynamic binding;• if you know that a method is not going to be overridden you can use

the final modifier with a method;• this means that the method cannot be overridden - static binding will

be used.

Page 6: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Abstract classes and interfaces • an interface is a class in which all methods are abstract; • for example, ActionListener, which is provided as part of

the java.awt.event package; • it is perfectly possible to create our own interfaces.

Example• we wish to place the organization's logo somewhere on

the screen;• it would be useful if we could produce a class that we

could customize later with a particular logo;• that way the class is re-usable, because we just have to

attach the right logo at the time. • to make a class attachable it would need to have an attach

method.

Page 7: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

• implementing an interface is very much like inheriting a class;

• the class that implements Attachable becomes a kind of Attachable;

• it will "inherit" and redefine the attach method.

Page 8: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Example

A CAndKLogo class must be attachable so it could look like this:

Page 9: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Analysis of the CAndKLogo class

• implements is used with interfaces whereas extends is used with classes.

• the class now defines its own version of the attach method, as required by the Attachable interface;

• we need to generate a Graphics object which will contain information about the component to be painted;

• we refer to this process as getting the graphics context. • now this logo, or any other Attachable, can be attached to

a JFrame:

Page 10: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The LogoFrame class • the class has three attributes:

• an Attachable object;• two integers representing the position of that object.

• the constructor uses the setLocation method of Component (inherited by JFrame) to position the frame on the screen:

• the particular Attachable object to be added doesn't have to be specified here - it is passed in as a parameter;

• because it is Attachable we know that it will have an attach method - and we are therefore able to use it to paint the component:

Page 11: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

In program 14.1 the Charatan and Kans logo is added at co-ordinates (10,5), roughly in the top left-hand corner.

Page 12: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

it would now be a simple matter to change our logo; all we would have to do is send in a different logo as a

parameter.

Page 13: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

More on interfaces

The RedCircle program• a red circle always moves away from the cursor so you can

never click on it;

• if you click the mouse, the words "Keep Trying" flash onto the screen.

Page 14: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The RedCircle class

• we are implementing two interface classes, MouseListener and MouseMotionListener;

• the syntax is to separate them by a comma. • we are not allowed to extend more than one class but it is

perfectly possible to implement as many interfaces as we wish.

Page 15: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Page 16: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The last three of the methods in the table above are not usedin this application so we have just left them blank.

Page 17: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The attributes

Page 18: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The constructor

Page 19: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

the paint method

• the circle is drawn at position (xPos, yPos);• this method gets called every time the program encounters a

repaint command;• this happens every time the mouse moves; • each time the screen gets repainted, xPos and yPos will have

changed.

Page 20: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The event-handling routines

The methods of MouseMotionListener (mouseMoved and MouseDragged)

Page 21: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

• continually invoked while the mouse is moving; • each time it is invoked xPos and yPos are assigned new

values;• the value assigned to each of them is always the value of

the current coordinate of the cursor minus 5;• after every assignment the window is repainted; • thus, as the cursor moves, the circle moves too – always

staying just north-west of it;• notice that the method is automatically sent an object of the

MouseEvent class;• we use the getX and getY methods of this class to obtain

the current coordinates of the cursor.• the other method of the MouseMotionListener interface,

mouseDragged, determines what happens when the mouse is moved with the button held down (dragged);

• it has been coded it in exactly the same way, so that dragging the mouse has the same effect as above.

Page 22: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The methods of MouseListener (mousePressed and mouseReleased)

The MouseListener interface also insists that we implement the mouseClicked, mouseEntered and mouseExited methods;These are not actually going to be used here, so they have been left blank.

Page 23: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Adapters and inner classes• using an interface means that we have to code all the

interface methods, even those we are not interested in;• there is a way around this, which is to use an adapter;• an adapter is a special class that acts as an intermediary

between our class and the interface, making it unnecessary to code all the methods because the class can be extended in the normal way using inheritance;

• an adapter is provided for every interface that comes with the standard Java packages;

• for example, the adapter equivalent for MouseListener is called MouseAdapter.

• we cannot use adapters all the time because very often the class that we want to extend is already extending another class - Java does not allow multiple inheritance.

Page 24: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

• there is a way we could use this adapter in the RedCircle class.

• the following line in the constructor

• indicates the class where the program can find the instructions for processing the event.

• in the RedCircle class this code will be found in the class itself (this class);

• instead, we could write a class (eg RedCircleAdapter) for this express purpose;

• this class would extend MouseAdapter and would code the mousePressed and mouseReleased methods;

• the above line would then have to be changed to:

Page 25: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

• we could write this as an inner class:

• this has been done in the RedCircleWithAdapter class

• notice how it is possible to refer to attributes of the outer class (in this case mouseDown) in the inner class.

• this class would extend MouseAdapter and would code the mousePressed and mouseReleased methods;

• the above line would then have to be changed to:

Page 26: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

The toString method

• in Java, every class is inherited from a "super superclass" called Object;

• the Object class has a method called toString that returns a String, and which can be overridden by any other class;

• methods of other classes can be set up to use this method;

• for example there are versions of print and println which take an Object object as a parameter and use its toString method;

• we could also write such methods in our own classes.

Page 27: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Adding (i.e. overriding) a toString method to the BankAccount class

Page 28: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

• in each case the parameter to the println method is just the name of the object; this is possible because there is a version of println provided

• that accepts an object and outputs the return value of the object's toString method;

• since we have overridden the toString method in our BankAccount class as shown, the output from this program will be:

Page 29: © The McGraw-Hill Companies, 2006 Chapter 14 Abstraction, inheritance and interfaces

©The McGraw-Hill Companies, 2006

Output

Account Number: 001

Account Name: Sarah Patel

Current Balance: 0.0

Account Number: 002

Account Name: Robinder Grewel

Current Balance: 0.0