Interfaces and Polymorphism CS 162 (Summer 2009).

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Object Oriented Programming
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.
Polymorphism.
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.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Abstract Classes b b An abstract class is a placeholder in a class hierarchy that represents a generic concept b b An abstract class cannot be instantiated.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Polymorphism & Interfaces
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Interfaces Need arising from software engineering –Disparate groups of programmers need to agree to a “contract” that spells out how their software interacts.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
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()
Data Structures and Java CS 105. L7: Java Slide 2 Data structure Data structure defined: A systematic way of organizing and accessing data Examples Dictionary:
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
CS 151: Object-Oriented Design September 26 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
LECTURE 9: INTERFACES & ABSTRACT CLASSES CSC 212 – Data Structures.
Inheritance (Part 4) Polymorphism and Abstract Classes 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
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.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Java Inheritance in Java. Inheritance Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object. The idea.
CH10 Supplementary Material Prepared by Fatimah Alakeel Oct 2010.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Important Annoucement 1  I messed up something in the last class  if a subclass overrides a method that throws an exception then it must either 1. throw.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
BY:- TOPS Technologies
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
Java Programming (By Rama Bhadra Rao M) You can Trace me in Day 2
CSC 243 – Java Programming, Fall, 2008 Tuesday, September 30, end of week 5, Interfaces, Derived Classes, and Abstract Classes.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Interfaces.
Interfaces.
Inheritance and Polymorphism
Interface.
Chapter 11 – Interfaces and Polymorphism
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.
Interface.
Java Programming Language
CS18000: Problem Solving and Object-Oriented Programming
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 9 Carrano Chapter 10 Small Java
Chapter 14 Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topics OOP Review Inheritance Review Abstract Classes
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Presentation transcript:

Interfaces and Polymorphism CS 162 (Summer 2009)

Interfaces A formal contract that specifies the similarities between two or more classes Specifies the methods that a class implementing that interface must implement

Interfaces All methods are abstract (no implementation) All methods are automatically public No instance fields (except public static final constants)

Example public interface OpenableByKids { void Open(); void Find(); void DestroyContents(); }

Interfaces Interface cannot be instantiated: cannot call Openable dresser = new Openable(); Instead, interfaces must be implemented: – public class Dresser implements OpenableByKids { public void Open() {…} public void Find() {…} public void DestroyContents() {…} }

Interfaces Interfaces can be used as a data type: – void OutOfMySight(OpenableByKids furniture); – Can pass any class to this method that implements the OpenableByKids interface

Interfaces Classes can implement more than one interface public class Dresser implements OpenableByKids, OpenableByCats { Need to implement all methods from both interfaces

Interfaces Can convert from class to interface: – Dresser myDresser = new Dresser(); OpenableByKids openableDresser = myDresser; Can go back using casting operator: – if (openableDresser instanceof Dresser) { Dresser someDresser = (Dresser) openableDresser; }

Extending Interfaces Interfaces can be extended with new interfaces: – public interface ExpensiveMistake extends OpenableByMyKids, OpenableByCats { // includes all methods from OpenableByMyKids and OpenableByCats // adds new methods, too } – Multiple inheritance is possible with interfaces, but not with inheritance.

Polymorphism Polymorphic – “having many forms” Accomplished in Java with both interfaces and inheritance allows Java programs to be written more abstractly, and more abstraction allows for more efficiency and less redundancy.

Polymorphism Suppose we have a Player object called p. p.throwWeapon() might mean: – Throw Rock if p is a RockPlayer – Throw Scissors if p is a ScissorsPLayer – Throw something random if p is a Player (the parent class) Depending on what p points to, a different method might be called These references are resolved at run-time

Polymorphism Allows elegant re-use of higher-level code when lower-level code is different: public class Tournament { private ArrayList players; public play() { for( int i = 0; i < players.size(); i++ ) { Player p = players.get(i); p.throwWeapon(); } } }

Implementation public class AnimalGame { public static void main(String[] args) { System.out.println("Choose one of three doors."); int doorNumber = input.nextInt(); Animal animal = null; if (doorNumber == 1) { animal = new Cat(); } else if (doorNumber == 2) { animal = new Dog(); } else if (doorNumber == 3) { animal = new TRex(); } else { System.out.println("Not a valid door number"); System.exit(1); } System.out.println("The animal behind your door says: " + animal.talk()); }

Interfaces and Polymorphism using an interface can stretch the ability of polymorphism over many families of classes. Example: abstract class Electronic – one abstract method: operate(). – Imagine that you want the operate() method to be available to more than just the Electronic family of classes. You can create an interface that contains the method operate(), and tell the Electronic abstract class to implement that interface.

Operator interface Operator { void operate(); } public abstract class Electronic implements Operator {…}

Operator Example Electronic and its subclasses still uses the operate() method in the same way Other families of classes may use the operate() method as well

Surgeon public class Surgeon implements Operator { String selectedOrgan; public Surgeon(String organ) { selectedOrgan = organ; } public void operate() { System.out.println("Removed " + selectedOrgan); } }