Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Advertisements

Programming With Java ICS 201 University Of Ha’il1 Chapter 8 Abstract Class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Abstract Classes. Lecture Objectives To learn about abstract classes To understand how to inherit abstract classes To understand how to override abstract.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
1 Inheritance and Polymorphism. 2 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common.
Chapter 8 – Sections covered
Chapter 10: Inheritance and Polymorphism
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 9 - Object-Oriented Programming
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Abstract Class There are some situations in which it is useful to define base classes that are never instantiated. Such classes are called abstract classes.
 2002 Prentice Hall. All rights reserved. Page 1 Chapter 9 - Object-Oriented Programming 9.10 Introduction to Polymorphism 9.11 Type Fields and switch.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
 2002 Prentice Hall. All rights reserved. Chapter 9 - Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3 protected.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Inheritance and Polymorphism.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
1 OOP : main concepts Polymorphism. 2 OOP : main concepts  The main concepts:  In a superclass –public members Accessible anywhere program has a reference.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Chapter 12 Advanced Inheritance
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
1 Chapter 5 - Object-Oriented Programming 1 What is inheritance? Object-oriented systems allow classes to be defined in terms of other classes. Classes.
Inheritance & Polymorphism1. 2 Introduction Besides composition, another form of reuse is inheritance. With inheritance, an object can inherit behavior.
Review –Inheritance Inheritance is a relationship between a more general class (the superclass) and a more specialized class (the subclass). The “is-a”
 2002 Prentice Hall. All rights reserved. Modifed by Haytham Allos, NYU CSD V Week 2 - Object-Oriented Programming Outline 2.1 Introduction 2.2.
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
O O P Polymorphism Object Oriented Programming Prepared & Presented by: dr.Ismail Farahat Chapter 4.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Advanced Programming Inheritance (2). 2 Inheritance Inheritance allows a software developer to derive a new class from an existing one The existing.
Interfaces and Polymorphism CS 162 (Summer 2009).
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 10 Inheritance and Polymorphism.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 11 Inheritance and Polymorphism
Inheritance and Polymorphism
Chapter 5 Hierarchies IS-A associations superclasses subclasses
Object-Oriented Programming: Polymorphism
Chapter 9 - Object-Oriented Programming
Chapter 9 Inheritance and Polymorphism
More inheritance, Abstract Classes and Interfaces
Object-Oriented Programming: Polymorphism
Java Inheritance.
Chapter 9 Carrano Chapter 10 Small Java
Java Programming: From the Ground Up
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts –Write your own code without seeing the implementation of other classes (by other programmers) –An Interface is such a “contract” An interface declares a type of object by specifying what the object does –Doesn’t care about the implementation –Only what functionality is provided –Specifies common operations

Example Car manufacturers Car: start, stop, accelerate, turn right, … Guidance manufacturers Invoke the car methods for navigation There has to be an industry standard as to which methods should be implemented by all car manufacturers

Another motivation Multiple inheritance –A class inherits from more than one parents A Dog can be both an Animal and a Speaker Jack can be both a Person and an Employee –In C++, a class can inheritance from more than one classes –In Java, multiple inheritance is done through multiple interfaces

Interfaces A collection of constants and abstract methods –without any implementation! Different from abstract classes –Abstract class can also have variables and non-abstract methods (with implementation). public interface Animal{ int LEGS=4; // constants public void sleep(); // abstract methods... } Note: No {} in the method declaration – no implementation The word “abstract” is not needed

Use an Interface Writing a class the implements an interface –Provides implementation for each method defined in the interface An interface cannot be instantiated public class Dog implements Animal{ public void sleep(){ System.out.println(“Snorrrr”); } not extends !

Multiple Inheritance interface Speaker{ public void speak(); } interface Animal{ final int LEGS=4; public void sleep(); } class Dog implements Speaker, Animal { public void speak(){ System.out.println("woof"); } public void sleep(){ System.out.println("snorrrr"); } public String toString(){ String s="This Dog has " + LEGS + " legs."; return s; }

class Teacher implements Speaker{ private String thoughts; public Teacher (String t) { thoughts=t; } public void speak () { System.out.println(thoughts); } public void repeat() { for (int i=1; i<=3; i++) System.out.println(thoughts); } public class ExInterfaces { public static void main(String[] args){ Dog little = new Dog(); little.speak(); little.sleep(); System.out.println( little.toString() ); Teacher tom = new Teacher("Do your homework"); tom.speak(); tom.repeat(); }

Converting Between Class and Interface Types Very similar to the case of converting between superclass and subclass references –Converting from class to interface reference Teacher t = new Teacher(); Speaker sp = t; //OK … method: public void test(Speaker speaker){…} You can call this method by test(t); sp.repeat(); //Error –Converting from interface reference to class reference Teacher teacher = (Teacher) sp; t.repeat();

Some Useful Interfaces Comparable –the compareTo() method: public interface Comparable{ public int compareTo(Object o); } –String implements Comparable –Why is it useful? Ex: Arrays.sort(Object[] a) Listener –Useful for applications that handle events –Will be discussed next week

Introduction to Polymorphism Polymorphism –“having many forms” –Helps build extensible systems –Programs generically process objects as superclass objects Can add classes to systems easily –Classes must be part of generically processed hierarchy –Can be done through interfaces and abstract classes

Example public class ExInterfaces { public static void main(String[] args){ Speaker current; current = new Dog(); current.speak(); current = new Teacher("Do your homework"); current.speak(); } Different speak() method will be called depending on what type of objects the current reference points to. The decision as to which one to call cannot be done at compile time. It has to wait until execution time.

Using if-else Statements? if-else -based system –Determine appropriate action for object Based on object’s type –Error prone Programmer can forget to make appropriate type test Adding and deleting if-else statements

class Animal { //... } class Dog extends Animal { public void woof() { System.out.println("Woof!"); } //... } class Cat extends Animal { public void meow() { System.out.println("Meow!"); } //... } class Example5 { public static void main(String[] args) { makeItTalk(new Cat()); makeItTalk(new Dog()); } public static void makeItTalk(Animal animal) { if (animal instanceof Cat) { Cat cat = (Cat) animal; cat.meow(); } else if (animal instanceof Dog) { Dog dog = (Dog) animal; dog.woof(); } Example: using if-else and instanceof

Dynamic Method Binding Dynamic method binding –Also called “late binding” –Implements polymorphic processing of objects –Use superclass reference to refer to subclass object –Program chooses “correct” method in subclass at execution

import java.util.Scanner; public class AnimalGame { public static void main(String[] args) { System.out.println("Choose one of three doors by entering the number of the door you choose."); Scanner input = new Scanner(System.in); int doorNumber = input.nextInt(); Animal animal = null; if (doorNumber == 1){ animal = new Cat(); } else if (doorNumber == 2){ animal = new Dog(); } else { System.out.println("Not a valid door number"); System.exit(1); } System.out.println("The animal behind your door says: " + animal.talk()); } } Animal is an interface Dog and Cat implement Animal Example

Dynamic Method Binding (cont.) Example –Superclass Shape –Subclasses Circle, Rectangle and Square –Each class draws itself according to type of class Shape has method draw Each subclass overrides method draw Call method draw of superclass Shape –Program determines dynamically which subclass draw method to invoke

Late Binding vs. Early Binding Early Binding –Overloaded methods/constructors –E.g., two constructors for BankAccount class BankAccount() and BankAccount(double) The compiler selects the correct constructor when compiling the program by looking at the parameters. account = new BankAccount(); //compiler selects BankAccount(); account = new BankAccount(1.00);//BankAccount(double) Early Binding – decisions are made at compilation time by the compiler Late Binding – decision are made at run time by the java virtual machine

Case Study: A Payroll System Using Polymorphism Abstract methods and polymorphism –Abstract superclass Employee Method earnings applies to all employees Person’s earnings dependent on type of Employee –Concrete Employee subclasses declared final Boss CommissionWorker PieceWorker HourlyWorker Employee BossCommissionWorkerPieceWorkerHourlyWorker

Employee.java Line 4 abstract class cannot be instantiated Lines 5-6 and abstract class can have instance data and non abstract methods for subclasses Lines 9-13 abstract class can have constructors for subclasses to initialize inherited data 1 // Employee.java 2 // Abstract base class Employee. 3 4 public abstract class Employee { 5 private String firstName; 6 private String lastName; 7 8 // constructor 9 public Employee( String first, String last ) 10 { 11 firstName = first; 12 lastName = last; 13 } // get first name 16 public String getFirstName() 17 { 18 return firstName; 19 } // get last name 22 public String getLastName() 23 { 24 return lastName; 25 } public String toString() 28 { 29 return firstName + ' ' + lastName; 30 } 31 abstract class cannot be instantiated abstract class can have instance data and non abstract methods for subclasses abstract class can have constructors for subclasses to initialize inherited data

Employee.java Line 35 Subclasses must implement abstract method 32 // Abstract method that must be implemented for each 33 // derived class of Employee from which objects 34 // are instantiated. 35 public abstract double earnings(); } // end class Employee Subclasses must implement abstract method

Boss.java Line 4 Boss is an Employee subclass Line 4 Boss inherits Employee ’s public methods (except for constuctor) Line 10 Explicit call to Employee constructor using super Lines Required to implement Employee ’s method earnings (polymorphism) 1 // Boss.java 2 // Boss class derived from Employee. 3 4 public final class Boss extends Employee { 5 private double weeklySalary; 6 7 // constructor for class Boss 8 public Boss( String first, String last, double salary ) 9 { 10 super( first, last ); // call superclass constructor 11 setWeeklySalary( salary ); 12 } // set Boss's salary 15 public void setWeeklySalary( double salary ) 16 { 17 weeklySalary = ( salary > 0 ? salary : 0 ); 18 } // get Boss's pay 21 public double earnings() 22 { 23 return weeklySalary; 24 } // get String representation of Boss's name 27 public String toString() 28 { 29 return "Boss: " + super.toString(); 30 } } // end class Boss Boss is an Employee subclass Boss inherits Employee ’s public methods (except for constuctor) Explicit call to Employee constructor using super Required to implement Employee ’s method earnings (polymorphism)

CommissionWorker.java Line 4 CommissionWorker is an Employee subclass Line 13 Explicit call to Employee constructor using super 1 // CommissionWorker.java 2 // CommissionWorker class derived from Employee 3 4 public final class CommissionWorker extends Employee { 5 private double salary; // base salary per week 6 private double commission; // amount per item sold 7 private int quantity; // total items sold for week 8 9 // constructor for class CommissionWorker 10 public CommissionWorker( String first, String last, 11 double salary, double commission, int quantity ) 12 { 13 super( first, last ); // call superclass constructor 14 setSalary( salary ); 15 setCommission( commission ); 16 setQuantity( quantity ); 17 } // set CommissionWorker's weekly base salary 20 public void setSalary( double weeklySalary ) 21 { 22 salary = ( weeklySalary > 0 ? weeklySalary : 0 ); 23 } // set CommissionWorker's commission 26 public void setCommission( double itemCommission ) 27 { 28 commission = ( itemCommission > 0 ? itemCommission : 0 ); 29 } 30 CommissionWorker is an Employee subclass Explicit call to Employee constructor using super

CommissionWorker.java Lines Required to implement Employee ’s method earnings ; this implementation differs from that in Boss 31 // set CommissionWorker's quantity sold 32 public void setQuantity( int totalSold ) 33 { 34 quantity = ( totalSold > 0 ? totalSold : 0 ); 35 } // determine CommissionWorker's earnings 38 public double earnings() 39 { 40 return salary + commission * quantity; 41 } // get String representation of CommissionWorker's name 44 public String toString() 45 { 46 return "Commission worker: " + super.toString(); 47 } } // end class CommissionWorker Required to implement Employee ’s method earnings ; this implementation differs from that in Boss

PieceWorker.java Line 4 PieceWorker is an Employee subclass Line 12 Explicit call to Employee constructor using super Lines Implementation of Employee ’s method earnings ; differs from that of Boss and CommissionWorker 1 // PieceWorker.java 2 // PieceWorker class derived from Employee 3 4 public final class PieceWorker extends Employee { 5 private double wagePerPiece; // wage per piece output 6 private int quantity; // output for week 7 8 // constructor for class PieceWorker 9 public PieceWorker( String first, String last, 10 double wage, int numberOfItems ) 11 { 12 super( first, last ); // call superclass constructor 13 setWage( wage ); 14 setQuantity( numberOfItems); 15 } // set PieceWorker's wage 18 public void setWage( double wage ) 19 { 20 wagePerPiece = ( wage > 0 ? wage : 0 ); 21 } // set number of items output 24 public void setQuantity( int numberOfItems ) 25 { 26 quantity = ( numberOfItems > 0 ? numberOfItems : 0 ); 27 } // determine PieceWorker's earnings 30 public double earnings() 31 { 32 return quantity * wagePerPiece; 33 } 34 PieceWorker is an Employee subclass Explicit call to Employee constructor using super Implementation of Employee ’s method earnings ; differs from that of Boss and CommissionWorker

PieceWorker.java 35 public String toString() 36 { 37 return "Piece worker: " + super.toString(); 38 } } // end class PieceWorker

HourlyWorker.jav a Line 4 PieceWorker is an Employee subclass Line 12 Explicit call to Employee constructor using super Line 31 Implementation of Employee ’s method earnings ; differs from that of other Employee subclasses 1 // HourlyWorker.java 2 // Definition of class HourlyWorker 3 4 public final class HourlyWorker extends Employee { 5 private double wage; // wage per hour 6 private double hours; // hours worked for week 7 8 // constructor for class HourlyWorker 9 public HourlyWorker( String first, String last, 10 double wagePerHour, double hoursWorked ) 11 { 12 super( first, last ); // call superclass constructor 13 setWage( wagePerHour ); 14 setHours( hoursWorked ); 15 } // Set the wage 18 public void setWage( double wagePerHour ) 19 { 20 wage = ( wagePerHour > 0 ? wagePerHour : 0 ); 21 } // Set the hours worked 24 public void setHours( double hoursWorked ) 25 { 26 hours = ( hoursWorked >= 0 && hoursWorked < 168 ? 27 hoursWorked : 0 ); 28 } // Get the HourlyWorker's pay 31 public double earnings() { return wage * hours; } 32 HourlyWorker is an Employee subclass Explicit call to Employee constructor using super Implementation of Employee ’s method earnings ; differs from that of other Employee subclasses

HourlyWorker.jav a 33 public String toString() 34 { 35 return "Hourly worker: " + super.toString(); 36 } } // end class HourlyWorker

Test.java Line 15 Test cannot instantiate Employee but can reference one Lines Instantiate one instance each of Employee subclasses 1 // Test.java 2 // Driver for Employee hierarchy 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 public class Test { // test Employee hierarchy 13 public static void main( String args[] ) 14 { 15 Employee employee; // superclass reference 16 String output = ""; Boss boss = new Boss( "John", "Smith", ); CommissionWorker commisionWorker = 21 new CommissionWorker( 22 "Sue", "Jones", 400.0, 3.0, 150 ); PieceWorker pieceWorker = 25 new PieceWorker( "Bob", "Lewis", 2.5, 200 ); HourlyWorker hourlyWorker = 28 new HourlyWorker( "Karen", "Price", 12.5, 40 ); DecimalFormat precision2 = new DecimalFormat( "0.00" ); 31 Instantiate one instance each of Employee subclasses Test cannot instantiate Employee but can reference one

Test.java Line 33 Use Employee to reference Boss Line 36 Method employee.earning s dynamically binds to method boss.earnings Lines Do same for CommissionWorker and PieceWorker 32 // Employee reference to a Boss 33 employee = boss; output += employee.toString() + " earned $" + 36 precision2.format( employee.earnings() ) + "\n" + 37 boss.toString() + " earned $" + 38 precision2.format( boss.earnings() ) + "\n"; // Employee reference to a CommissionWorker 41 employee = commissionWorker; output += employee.toString() + " earned $" + 44 precision2.format( employee.earnings() ) + "\n" + 45 commissionWorker.toString() + " earned $" + 46 precision2.format( 47 commissionWorker.earnings() ) + "\n"; // Employee reference to a PieceWorker 50 employee = pieceWorker; output += employee.toString() + " earned $" + 53 precision2.format( employee.earnings() ) + "\n" + 54 pieceWorker.toString() + " earned $" + 55 precision2.format( pieceWorker.earnings() ) + "\n"; 56 Use Employee to reference Boss Method employee.earnings dynamically binds to method boss.earnings Do same for CommissionWorker and PieceWorker

Test.java Lines Repeat for HourlyWorker Line 59 Casting is required for accessing subclass methods 57 // Employee reference to an HourlyWorker 58 employee = hourlyWorker; 59 ((HourlyWorker)employee).setWage(13.75); 60 output += employee.toString() + " earned $" + 61 precision2.format( employee.earnings() ) + "\n" + 62 hourlyWorker.toString() + " earned $" + 63 precision2.format( hourlyWorker.earnings() ) + "\n"; JOptionPane.showMessageDialog( null, output, 66 "Demonstrating Polymorphism", 67 JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); 70 } } // end class Test Repeat for HourlyWorker Casting required for accessing subclass methods

New Classes and Dynamic Binding Dynamic binding (late binding) –Object’s type need not be known at compile time –At run time, call is matched with method of called object –Easy to add new classes to the class hierarchy Read the lecture notes on the course Web site for another example on polymorphism.

Recap - Interfaces Interfaces –A collection of constants and abstract methods –No implementation details defined public interface Car{ final int WHEELS = 4; public void start(); public void stop();... } –Variables are implicitly defined as public static final Implementing interfaces public class Ford implements Car { public void start(){... } public void stop(){... }... }

Interfaces True or false? –An interface can have private instance attributes –A concrete class implementing an interface can implement selected methods defined in the interface –A class can implement more than one interface –One need to use abstract to indicate that a method is abstract in an interface –You can create an instance (object) of an interface, just like creating an instance of a class

interface Silly { public void narf(); public void poit(Silly s); } public class Bird implements Silly { public static void main(String args[]) { System.out.println("zero"); Silly s = new SillyBird(1); Silly s2 = new Loony(); s.poit(s2); s2.poit(s); System.out.println("zymurgy"); } public Bird() { this(0); System.out.println("zircon"); } public Bird(int i) { System.out.println("zanzibar"); } public void narf() { System.out.println("zort"); } public void poit(Silly s) { s.narf(); } class SillyBird extends Bird { public SillyBird(){ System.out.println("duchess"); } public SillyBird(int i) { super(i); } public void narf() { System.out.println("drum"); super.narf(); } class Loony extends SillyBird { public Loony() { System.out.println("stupendous"); } public void narf() { System.out.println("snark"); } What’s the output?