CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois...
-
Upload
mae-haynes -
Category
Documents
-
view
215 -
download
1
Transcript of CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois...
![Page 1: CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 9 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis 1.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/1.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/2.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/3.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/4.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/5.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/6.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/7.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/8.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/9.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/10.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/11.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/12.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/13.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/14.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/15.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/16.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/17.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/18.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/19.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/20.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/21.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/22.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/23.jpg)
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.](https://reader030.fdocuments.net/reader030/viewer/2022032723/56649d0b5503460f949deda3/html5/thumbnails/24.jpg)
Study Guide
• Chapter 10– Sections 10.6, 10.7, 10.8
24