Polymorphism & Interfaces

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Interfaces.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
ABSTRACT CLASSES AND INTERFACES. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Polymorphism and Inheritance. Overview  Understanding Inheritance  Designing an Inheritance Tree  What does inheritance really help with  Polymorphism.
CS 106 Introduction to Computer Science I 11 / 26 / 2007 Instructor: Michael Eckmann.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
CS 106 Introduction to Computer Science I 11 / 15 / 2006 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 / 21 / 2008 Instructor: Michael Eckmann.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
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.
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Advanced Inheritance Concepts. In this chapter, we will cover: Creating and using abstract classes Using dynamic method binding Creating arrays of subclass.
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.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Dec Abstract Classes and Interfaces. Eclipse trick CTRL + D will remove lines Organization Bookmarks TODOs – marking something as //TODO allows.
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.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
These materials where developed by Martin Schray. Please feel free to use and modify them for non-commercial purposes. If you find them useful or would.
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Inheritance and Access Control CS 162 (Summer 2009)
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
CS 106 Introduction to Computer Science I 04 / 18 / 2008 Instructor: Michael Eckmann.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Coming up A quick word about abstract How a class interfaces
Polymorphism in Methods
A Concrete Presentation on Abstract Classes and Methods, Interfaces, and Polymorphism CSC 202.
Abstract Classes and Interfaces
Abstract Classes and Interfaces
Interfaces Professor Evan Korth.
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.
CMSC 202 Interfaces.
Java Programming Language
Abstract Classes Page
Java Inheritance.
CMSC 202 Interfaces.
2009 Test Key.
Abstract Classes and Interfaces
CMSC 202 Interfaces.
Presentation transcript:

Polymorphism & Interfaces Mimi Opkins CECS 277

Example class hierarchy Animal Cat Dog Horse

Polymorphism Normally we have this when we create an object: Dog dog = new Dog(); Polymorphism allows us to also do this: Animal pet = new Dog(); The object reference variable can be a super class of the actual object type! (Does NOT work the other way around: Dog is an Animal but Animal is not necessarily a Dog)

Where Polymorphism is Helpful Arrays Passing parameters Returning values from a method

Polymorphic Array Example Animal[] myPets = new Animal[5]; myPets[0] = new Cat(); myPets[1] = new Cat(); myPets[3] = new Dog(); for (int i = 0; i < myPets.length; i++) { myPets.feed(); } You can put any subclass of Animal in the Animal array!

Polymorphic Arguments public class Vet { public void giveShot(Animal pet) { pet.makeNoise(); } public class PetOwner { Vet vet = new Vet(); Dog dog = new Dog(); Cat cat = new Cat(); vet.giveShot(dog); vet.giveShot(cat);

Abstract Classes Sometimes we don’t want to allow an object to be created of a certain type. What exactly would an Animal object be? We use the keyword abstract to prevent a class from ever being instantiated. abstract public class Animal

Abstract Classes Can still use abstract classes as a reference variable, for the purposes of polymorphism. An abstract class has no use until it is extended! A class that is not abstract is called concrete.

Abstract Methods An abstract method has no body and is marked with the keyword abstract. public abstract void eat(); If a method is abstract, the class it is contained in must also be abstract. Abstract methods help the programmer to provide a protocol for a group of subclasses. The first concrete class in the inheritance hierarchy must implement the abstract method (i.e. override it and provide it a body)

Side Effects of Polymorphism ArrayList pets = new ArrayList(); Dog dog = new Dog(); pets.add(dog); int index = pets.indexOf(dog); Dog dog1 = pets.get(index); // won’t work Object dog2 = pets.get(index); dog2.bark(); // won’t work ((Dog)dog2).bark(); // works because of casting if (dog2 instanceof Dog) { // being careful ((Dog)dog2).bark(); } Dog dog3 = (Dog) pets.get(index); // works because of casting Dog dog4 = (Dog) dog2;

Pet Animal Canine Feline Dog Wolf Cat Tiger Lion But remember we said that Java does not support multiple inheritance. There is a solution however: interfaces.

Interfaces Interface: A collection of constants and abstract methods that cannot be instantiated. A class implements an interface by providing method implementations for each of the abstract methods defined in the interface. public class Dog extends Canine implements Pet

Interfaces Explicitly typing in public and abstract is not necessary since they MUST be public and abstract public interface Pet { public abstract void beFriendly(); public abstract void play(); } public class Dog extends Canine implements Pet { public void beFriendly() { wagTail(); public void play() { chaseBall(); . . . all the other Dog methods . . . Must implement these methods since they are in Pet

Interfaces vs. Subclasses Make a subclass only when you want to make a more specific version of a class. Use an interface when you want to define a role that other classes can play, regardless of where those classes are in the inheritance tree.

Polymorphism via Interfaces An interface reference variable can be used to refer to any object of any class that implements that interface. This works the same with superclasses. Pet myPet = new Dog(); The same side effects of polymorphism occur with interfaces as with inheritance.

Comparable Interface Defined in the java.lang package Only contains one method: compareTo which takes an object as a parameter and returns an integer. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. Provides a common mechanism for comparing one object to another. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ Comparable.html