cs 116 object oriented programming ii lecture 9 george koutsogiannakis copyright: 2015 illinois...
TRANSCRIPT
CS 116
OBJECT ORIENTED PROGRAMMING IILECTURE 9
GEORGE KOUTSOGIANNAKIS
Copyright: 2015 Illinois Institute of Technology/George Koutsogiannakis1
Last week’ s Topics
• Adding Specialization to the Subclass• Abstract Classes and Methods
2
New Topics
• Polymorphism– New format for “for loop” .
• Interfaces• Multiple Inheritance
3
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Multiple Inheritance
• Class TortoiseNonRacer has single inheritance only (it does not implement the interface)
19
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
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
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
Multiple Inheritance
23
Abstract Employee class
Student class
Class StudentEmployee
Uses StudentImpl
StudentImpl
Interface StudentInterface
implements
implements
extendsextends
Study Guide
• Chapter 10– Sections 10.6, 10.7, 10.8
24