10-1 An Interface is a special kind of class which defines a specification of a set of methods. provides a guarantee that any class that implements this.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Introduction to Java.
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.
COMP 121 Week 2: Interfaces and Polymorphism. Objectives To learn about interfaces To be able to convert between class and interface references To understand.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
Java Keywords CSE 115 Spring Purpose This set of slides contains a running list of the keywords that we have talked about so far this semester and.
Java Programming Chapter 3 More about classes. Why?  To make classes easier to find and to use  to avoid naming conflicts  to control access  programmers.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
Java Interfaces Overview Java Interfaces: A Definition.
Chapter 10 Classes Continued
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
PACKAGES Mimi OpkinsCECS277. What We’ll Cover  Packages and Import Statements  The Package java.lang  Package Names and Directories  The Default Package.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 12 Inheritance and Exceptions Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Inheritance in the Java programming language J. W. Rider.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Agenda Object Oriented Programming Reading: Chapter 14.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization  that can improve reusability and system elegance.
10-1 Lecture 10 Inheritance, Polymorphism, Interfaces, and Packages.
Simple Classes. ADTs A specification for a real world data item –defines types and valid ranges –defines valid operations on the data. Specification is.
Some Important topics. Working with Files Working with a file involves three steps: – Open the file – Perform operations on the file – Close the file.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Lecture 9 Enhanced Class Design Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 5 Methods.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Classes, Interfaces and Packages
Methods. Introducing Methods A method is a collection of statements that are grouped together to perform an operation.
UNIT-3 Interfaces & Packages. What is an Interface? An interface is similar to an abstract class with the following exceptions: All methods defined in.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.
Inheritance & Method Overriding BCIS 3680 Enterprise Programming.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Interfaces CMSC 202. Public Interfaces Objects define their interaction with the outside world through the their public interface. A class' public interface.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Lecture 5:Interfaces and Abstract Classes
More About Objects and Methods
Final and Abstract Classes
Packages, Interfaces & Exception Handling
Corresponds with Chapter 7
Packages and Interfaces
Interfaces.
Implementing Classes Chapter 3.
Object Oriented Programming in java
Final and Abstract Classes
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Presentation transcript:

10-1 An Interface is a special kind of class which defines a specification of a set of methods. provides a guarantee that any class that implements this interface will provide these methods. is similar to setting "standards" and any class implementing it will get a "stamp of approval". A contract!!!!

10-2 Inheritance vs. interfaces Is different from inheritance since interfaces do not inherit state or behaviour, instead they just define a specification!!!!!!

10-3 Syntax interface interfaceName { /* Method specifications */ } class className implements interfaceName { /* Bodies for the interface methods */ /* Own data and methods. */ }

10-4 Example: MovableObject interface MovableObject { boolean start(); void stop(); boolean turn(int degrees); double fuelRemaining(); boolean changeSpeed(double kmPerHour); }

10-5 aPlane class Plane implements MovableObject { boolean start() { //Do what's necessary to start the plane. Return true if it actually started. } void stop() { //Do whatever is necessary to stop the plane. } boolean turn(int degrees) { // Do what's necessary to turn the plane. Return true if it worked. } double fuelRemaining() { //Return the amount of plane fuel remaining. } boolean changeSpeed(double kmPerHour) { //Do what's necessary to accelerate or decelerate by the given amount }

10-6 How does having an interface help us? It helps us extract common behaviour among similar objects (kind of like inheritance). It makes a system more robust and easier to understand in that it ensures that the similar objects have at least certain behavours.

10-7 A handheld remote control for MovableObjects. class RemoteControl { private MovableObject machine; // The constructor RemoteControl(MovableObject m) { machine = m; }... //When the start button is pressed on the remote Boolean okay = machine.start(); if (!okay) display("No Response on start");... } Works for Plane, Car, Train, Ship, Lawnmower,....

10-8 Using it!!! Can create remote controls for more than one class Each will operate correctly using its own implementation of the MovableObject interface Plane aPlane = new Plane(); Ship aShip = new Ship(); RemoteControl planeRemote = new RemoteControl(plane); RemoteControl shipRemote = new RemoteControl(ship);

10-9 Topics Inheritance Polymorphism Interfaces Packages

10-10 Packages A Java package is a collection of classes May or may not be related by inheritance Groups similar and interdependent classes together The Java API is composed of multiple packages The import statement is used to assert that a particular program will use classes from a particular package

10-11 Packages A programmer can define a package and add classes to it The package statement is used to specify that all classes defined in a file belong to a particular package The syntax of the package statement is: package package-name ; It must be located at the top of a file, and there can be only one package statement per file

10-12 Packages The classes must be organized in the directory structure such that they can be found when referenced by an import statement There is a CLASSPATH environment variable on each computer system that determines where to look for classes when referenced See Simple_IO and Simple_IO_Test.java

10-13 Packages The import statement specifies particular classes, or an entire package of classes, that can be used in that program Import statements are not necessary; a class can always be referenced by its fully qualified name in- line See Simple_IO_Test2.java If two classes from two packages have the same name and are used in the same program, they must be referenced by their fully qualified name

10-14 Packages As a rule of thumb, if you will use only one class from a package, import that class specifically If two or more classes will be used, use the * wildcard character in the import statement to provide access to all classes in the package See Simple_IO_Test3.java