SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Inheritance and Polymorphism CS180 Fall Definitions Inheritance – object oriented way to form new classes from pre-existing ones –Superclass The.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Using interfaces Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling How would you find the maximum.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
Week 4 Recap CSE 115 Spring Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
CS 106 Introduction to Computer Science I 04 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 28 / 2010 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 17 Animated Version Generics and Type Safety.
Multiple Choice Solutions True/False a c b e d   T F.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
COP 3003 Object-Oriented Programming - Polymorphism Dr. Janusz Zalewski, Fall 2013 Prepared by Dr Dahai Guo.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Polymorphism & Interfaces
Object Oriented Programming: Java Edition By: Samuel Robinson.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Final Exam Review Closed book Closed laptop One sheet of notes permitted SE-0010 Dr. Mark L. Hornick 1.
UML Review – class diagrams SE 2030 Dr. Rob Hasker 1 Based on slides written by Dr. Mark L. Hornick Used with permission.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
1 Advanced Polymorphism Polymorphism Review Comparator Interface Sorting with Comparators Selection Sort Insertion Sort.
SE-1020 Dr. Mark L. Hornick 1 Composition, Aggregation, and Inheritance - Introduction.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Class and Sequence diagrams UML notation SE-2030 Dr. Mark L. Hornick 1.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
29-July-2002cse Inheritance © 2002 University of Washington1 Inheritance CSE 142, Summer 2002 Computer Programming 1
UML Class Diagram notation Indicating relationships between classes SE-2030 Dr. Mark L. Hornick 1.
Interfaces and Inner Classes
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces and Polymorphism CS 162 (Summer 2009).
Topic 7 Interfaces I once attended a Java user group meeting where James Gosling (one of Java's creators) was the featured speaker. During the memorable.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
// Java2101.java This program tests the features of the class. public class Java2101 { public static void main (String args[]) { System.out.println("\nJAVA2101.JAVA\n");
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
BY:- TOPS Technologies
2/23- Interfaces Reference: Java 6.5 and 9.3. PA-4 Review Requirements PA3 – Grading Key PA4 Requirements Doc.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
UML Review – class diagrams SE-2030 Dr. Mark L. Hornick 1.
Interface, Subclass, and Abstract Class Review
Interfaces.
Interfaces I once attended a Java user group meeting where James Gosling (Java's inventor) was the featured speaker. During the memorable Q&A session,
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Interfaces.
Interface.
CMSC 202 Interfaces.
Java Programming Language
Interfaces.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Arrays and Collections
Chapter 14 Abstract Classes and Interfaces
2009 Test Key.
Chapter 13 Abstract Classes and Interfaces Part 01
Topics OOP Review Inheritance Review Abstract Classes
CMSC 202 Interfaces.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces

SE-1020 Dr. Mark L. Hornick 2 Sometimes, multiple classes implement similar behavior (via similar methods), even if the details of the behavior are different from class. That is, even though the method names and parameters are the same between the classes, the details of the method’s implementation differs from class to class Example: Every “animal-type” class exhibits a “speak” behavior, although each specific behavior is different

SE-1020 Dr. Mark L. Hornick 3 The common behaviors that must be implemented in various classes can be declared in a special type of “class” called an interface When a class declares that it implements an interface, it contractually guarantees that it actually implements (defines) those behaviors declared by the interface In this way, each class implementing the interface can appear to be the same to a user of that class, because each class contains the exact methods declared in the interface

SE-1020 Dr. Mark L. Hornick 4 The Java interface is used to declare a behavior (only method headers) that will be defined in classes implementing that interface public interface Animal { public static final int FURRY=1; // attr defn public static final int SCALY=2; // attr defn … public void eat(); // method decl public void speak(); // method decl … } Remember: Interfaces are not classes: They cannot be instantiated They declare public, non-static methods, but do not define them They can only define public static attributes (constants)

SE-1020 Dr. Mark L. Hornick 5 Implementation of an interface public class Dog implements Animal{ public void eat() { System.out.println(“Yum! A bone!”); } … more code here } public class Cat implements Animal{ public void eat() { System.out.println(“Yum! A mouse!”); } … more code here } Note the distinction here between overriding a base class method and implementing an interface method

In UML the relationship between an interface and the classes that implement it is illustrated as follows: SE-1020 Dr. Mark L. Hornick 6 Realization is a type of inheritance relationship that implies that a class implements the behavior defined in another class The Realization connector is a dotted line with a triangle (not arrow) pointing at the Interface

SE-1020 Dr. Mark L. Hornick 7 A regular class can extend only one class, but can implement more than one interface public class Dog extends Pet implements Mammal, Animal { public void shedFur() { // defn of Mammal method System.out.println(“Woof. I’m shedding.”); } public void eat() { // defn of Animal method System.out.println(“Yum! A bone!”); } public void speak() { // defn of Animal method System.out.println(“Bark!”); } }

SE-1020 Dr. Mark L. Hornick 8 Polymorphism allows an interface variable to refer to objects from different classes that implement the interface For example, if Cat and Dog both implement an interface called Animal, then the following statements are valid Animal myDog, myCat; myCat = new Dog();... myCat = new Cat(); A Dog or a Cat can be used anyplace that expects a Animal as a method argument: public void feed( Animal p ) In a collection (e.g. ArrayList )

SE-1020 Dr. Mark L. Hornick 9 Recall: The instanceof operator can be used to dermine the actual class of an object. int dogCount=0, catCount=0; for (Animal a: kennel ) {//kennel is ArrayList if ( a instanceof Dog ) { dogCount++; } else if ( a instanceof Cat ) { catCount++; }

SE-1020 Dr. Mark L. Hornick 10 The ArrayList and LinkedList classes both implement the List interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); for( Animal a: room) { // iterate the ArrayList a.eat(); } for( Animal a: cage) { // iterate the LinkedList a.eat(); } The foreach loop can be used on any collection that implements the List interface

SE-1020 Dr. Mark L. Hornick 11 The Collections class can perform various operations to a collection that implements the List interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); Collections.shuffle(room); // reorders the List using Collections.shuffle(cage); // the static shuffle method See the Sun Javadoc for the Collections class

SE-1020 Dr. Mark L. Hornick 12 The Comparable interface defines a single method that is used to compare two objects public interface Comparable { /** Compares this object with the specified other object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified other object. */ public int compareTo(T otherObject); } See the Sun Javadoc for the Comparable interface

SE-1020 Dr. Mark L. Hornick 13 The Collections class can sort a collection that implements the List interface, provided that the elements of the List implement the Comparable interface // We can use List to declare a variable that can // refer to either an ArrayList or LinkedList List room = new ArrayList (); List cage = new LinkedList (); Collections.sort(room); // reorders the List using Collections.sort(cage); // the static sort method Note: Animal must extend the Comparable interface: public interface Animal extends Comparable