cs 116 object oriented programming ii lecture 9 george koutsogiannakis copyright: 2015 illinois...

24
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS ight: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Upload: mae-haynes

Post on 18-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

CS 116

OBJECT ORIENTED PROGRAMMING IILECTURE 9

GEORGE KOUTSOGIANNAKIS

Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis1

Page 2: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Last week’ s Topics

• Adding Specialization to the Subclass• Abstract Classes and Methods

2

Page 3: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

New Topics

• Polymorphism– New format for “for loop” .

• Interfaces• Multiple Inheritance

3

Page 4: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Polymorphism

An important concept in inheritance is that an object of a subclass is also an object of any of its super classes.

• That concept is the basis for an important OOP feature, called polymorphism.

• Polymorphism simplifies the processing of various objects in the same class hierarchy because we can use the same method call for any object in the hierarchy using a super class object reference.

4

Page 5: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Polymorphism Requirements

To use polymorphism, these conditions must be true:– the classes are in the same hierarchy– all subclasses override the same method– a subclass object reference is assigned to a

superclass object reference – the superclass object reference is used to call

the method

5

Page 6: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Example

•Suppose Figure is abstract and has an abstract method draw.•Suppose that Circle and Square inherit from Figure and implement Figure ‘s abstract method draw.

6

Page 7: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Example• Suppose we have a class FigureClient. • This class uses the previous classes to actually draw figures.i.e. (assuming that all classes are in the same folder)public class FigureClient { public static void main(String [] args) {

Circle circle=new Circle(); Square square=new Square(); Figure figure; //just declaration, no instantiation is allowed figure=circle;

figure.draw(); // a circle will be drawn figure=square; figure.draw(); // a square will be drawn now.

//other code } ……………………}7

Page 8: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Polymorphism Conditions

conditions for polymorphism:– The Figure, Circle, and Square classes are in the

same hierarchy.– The non-abstract Circle and Square classes

implement the draw method.– We assigned the Circle and Square objects to

Figure references.– We called the draw method using Figure

references.

8

Page 9: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

ExampleExample 10.19 shows how we can simplify the

drawing of Circle and Square objects.We instantiate a Figure ArrayList and add Circle and

Square objects to it. ArrayList<Figure> figuresList

= new ArrayList<Figure>( ); figuresList.add( new Square( 150, 100,

Color.BLACK, 40 ) ); figuresList.add( new Circle( 160, 110,

Color.RED, 10 ) ); …

In the paint method, we call draw for each Figure: for ( Figure f : figuresList ) f.draw( g ); //Note: iterates though the list and assigns each list

item to the reference f.

9

Page 10: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Reminder of for loop format for ArrayList

• Assume that we have an ArrayList of VehicleA objects called vehicleArrayList

• VehicleA va; for(int i=0; i<vehicleArrayList.size(); i++) {

va=vehicleArrayList.get(i); }• Alternate format: for( VehicleA va: vehicleArrayList) System.out.println(va); /invokes toString//iterates through all the VehicleA objects stored in vehicleArrayList

10

Page 11: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Interfaces

A class can inherit directly from only one class, that is, a class can extend only one class.

To allow a class to inherit behavior from multiple sources, Java provides the interface.

• An interface typically specifies behavior that a class will implement. Interface members can be any of the following:

classes constants abstract methods other interfaces

11

Page 12: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Interface Syntax

To define an interface, use the following syntax:

accessModifier interface InterfaceName { // body of interface}

All interfaces are abstract; thus, they cannot be instantiated. The abstract keyword, however, can be omitted in the interface definition.

12

Page 13: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Finer Points of Interfaces

• An interface's fields are public, static, and final. These keywords can be specified or omitted.

• When you define a field in an interface, you must assign a value to the field.

• All methods within an interface must be abstract, so the method definition must consist of only a method header and a semicolon. The abstract keyword also can be omitted from the method definition.

13

Page 14: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Inheriting from an Interface

To inherit from an interface, a class declares that it implements the interface in the class definition, using the following syntax:

accessModifier class ClassName

extends SuperclassName implements Interface1, Interface2, …

• The extends clause is optional. • A class can implement 0, 1, or more interfaces.• When a class implements an interface, the class must

provide an implementation for each method in the interface (if the implementing class is a concrete class).

14

Page 15: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

INTERFACES

• NOTE: An abstract class can indicate that it implements an interfaces without actually implementing the methods of the interface.– It would be left up to the class that inherits the

abstract class to implement the methods of the interface.

15

Page 16: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Example

We define an abstract class Animal with one abstract method (See Example 10.22):

public abstract void draw( Graphics g );

We define a Moveable interface with one abstract method:

public interface Moveable { int FAST = 5; // static constant int SLOW = 1; // static constant void move( ); // abstract method }

16

Page 17: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Derived Classes• TortoiseRacer class– extends Animal class– implements Moveable interface– implements draw and move methods

• TortoiseNonRacer class– extends Animal class– (does not implement Moveable interface)– implements draw method only

See text Examples 10.21, 10.22, 10.23, & 10.2417

Page 18: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance

• We know that Java does not allow direct multiple inheritance.

• By using interfaces we can have multiple inheritance.

• Class TortoiseRacer inherits both the class Animal via the extends keyword and the interface that it implements .

18

Page 19: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance

• Class TortoiseNonRacer has single inheritance only (it does not implement the interface)

19

Page 20: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance• Suppose we have abstract class Student (template class).

– This class has abstract method getGPA which returns the gpa.• Suppose we also have abstract class Employee (template class)

– This class has abstract method getSalary which returns the salary.• Class StudentEmployee wants to inherit both classes and implement both

the gpa and the salary. • It can not do that because multiple inheritance is not allowed public class StudentEmployee extends Student extends Employee

ABOVE IS NOT LEGAL!!!!!!!

20

Page 21: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance

• Let us leave Employee intact and instead remove the abstract method getGPA from the Student class.– Thus there is no need for Student to be abstract any more.

• Let us create an interface :public interface StudentInterface defines the method getGPA

• Let us create a class StudentImpl which inherits Student and implements interface StudentInterface. It implements the method getGPA which calculates the gpa.

21

Page 22: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance• Now class StudentEmployee extends Employee and it also implements

the interface StudentInterface. Therefore it implements the method getGPA but kind of indirectly (via the implementation of the method in the StudentImpl class):

StudentImpl st=new StudentImpl(); public float getGPA() {

return st.getGPA(); } StudentEmployee object can invoke other methods of class Student since

StudentImpl also inherits student. It also can use the class Employee since it inherited that class directly.

22

Page 23: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Multiple Inheritance

23

Abstract Employee class

Student class

Class StudentEmployee

Uses StudentImpl

StudentImpl

Interface StudentInterface

implements

implements

extendsextends

Page 24: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1

Study Guide

• Chapter 10– Sections 10.6, 10.7, 10.8

24