Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.

Similar presentations


Presentation on theme: "CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer."— Presentation transcript:

1 CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer

2 Topics Interfaces (Textbook: Chapter 10.8)

3 Review of Inheritance Advantages of Inheritance Code reuse Superclass encapsulates the common methods and variables Subclass inherits the public/protected methods Specialization: Subclass can add new methods or override superclass methods Force a design Abstract class force its subclass to implement abstract methods

4 An example of Inheritance: A Graphics program (from textbook) Racer +ID: int +X: int +Y: int +Constructors +move() +draw() Tortoise speed: int +Constructors +move() +draw() *Abstract methods and classes are underlined in the figure See text Examples 10.21, 10.22, 10.23, & 10.24

5 An example of Inheritance: A Graphics program (from textbook) //abstract superclass public abstract class Racer { public abstract void move(); public abstract void draw( Graphics g ); } //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public abstract void draw( Graphics g ){ //code to draw the racer at the new position }

6 An example of Inheritance: A Graphics program (from textbook) //abstract superclass public abstract class Racer { int x; int y; //other variables public abstract void move(); public abstract void draw( Graphics g ); //other methods } //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public abstract void draw( Graphics g ){ //code to draw the racer at the new position } //other methods }

7 An example of Inheritance: A Graphics program (from textbook) //subclass public class Tortoise extends Racer { public void move(){ //code to calculates the new position for the racer } public void draw( Graphics g ){ //code to draw the racer at the new position } public void draw( Graphics g ) { int startX = getX( ); int startY = getY( ); g.setColor( new Color( 34, 139, 34 ) ); // dark green //body g.fillOval( startX - 30, startY, 25, 15 ); //head g.fillOval( startX - 10, startY + 5, 15, 10 ); //flatten bottom g.clearRect( startX - 30, startY + 11, 35, 4 ); //more code }

8 An example of Inheritance: A Graphics program (from textbook) We now ALSO have tortoise figures which are static. They don’t need the move() method Static and Dynamic tortoise figures have common methods (draw()) Also an additional method (move()) for the dynamic tortoise figure See text Examples 10.21, 10.22, 10.23, & 10.24 Racer +ID: int +X: int +Y: int +Constructors +move() +draw() Tortoise speed: int +Constructors +move() +draw()

9 How about this design? Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseNonRacer speed: int +Constructors +draw() Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable +Constructors +move() inheritance from multiple superclasses not allowed!!! Modify Racer and have only abstract method draw()

10 Interfaces To allow a class to inherit behavior from multiple sources, Java provides the interface. The intent of an interface is to specify a set of methods that a concrete class will honor. A class can inherit directly from only one class, that is, a class can extend only one class. An interface typically specifies behavior that a class will implement. Interface members can be any of the following:  Abstract methods  Classes  Constants  other interfaces 10

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. 11

12 An example of Inheritance: A Graphics program (from textbook) Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable (interface) +move() Create an interface that has the abstract method move()

13 A Graphics program (from textbook) public interface Moveable { //variables void move( ); // abstract method } public class TortoiseRacer extends Animal implements Moveable { //variables //constructors public void move(){ //code to calculates the new position for the racer } public void draw( Graphics g ){ //code to draw the racer at the new position } Animal +ID: int +X: int +Y: int +Constructors +draw() TortoiseRacer speed: int +Constructors +move() +draw() Moveable (interface) +move()

14 Finer Points of Interfaces An interface's fields (variables) are public, static, and final. These keywords can be specified or omitted. When you define a field (variable) 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. 14

15 Inheriting from Multiple Interfaces To inherit from multiple interface, a class declares that it implements the interfaces 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). 15

16 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. 16

17 Differences between Abstract class and Interface Abstract classInterface 1) Abstract class can have abstract and non- abstract methods. Interface can have only abstract methods. 2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance. 3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 4) Abstract class can have constructor.Interface can't have constructor. 5) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.

18 Multiple Inheritance (Student Employee Example) 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!!!!!!! 18

19 Student +ID: int +Constructors +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() Suppose we have abstract class Student. This class has abstract method getGPA() which returns the gpa. Suppose we also have abstract class Employee 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. Inheritance from multiple superclasses not allowed!!! Multiple Inheritance (Student Employee Example)

20 StudentInterface (interface) +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() How about creating an interface that has the abstract getGPA() method and have the Employee class implement it? Won’t work? Why?

21 Multiple Inheritance (Student Employee Example) StudentInterface (interface) +getGPA) StudentEmployee +Constructors +getGPA() +getSalary() Employee +Constructors +getSalary() getGPA() method in class studentEmployee needs details from the class student Solution: have a class attribute of type student or a subclass of student

22 Multiple Inheritance Let us 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. 22 Student +ID: int +Constructors StudentInterface (interface) +getGPA() StudentImpl +Constructors +getGPA()

23 Multiple Inheritance 23 Abstract Employee class Student class Class StudentEmployee Uses StudentImpl StudentImpl Interface StudentInterface implements extends

24 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. 24


Download ppt "CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer."

Similar presentations


Ads by Google