Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.

Slides:



Advertisements
Similar presentations
Abstract Class and Interface
Advertisements

SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
Exceptions and Exception Handling Carl Alphonce CSE116.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
COMP 121 Week 2: Interfaces and Polymorphism. Objectives To learn about interfaces To be able to convert between class and interface references To understand.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 11 – Interfaces and Polymorphism. Chapter Goals Learn about interfaces Learn about interfaces Convert between class and interface references Convert.
Interfaces and polymorphism Chapter 9. Interfaces  Used to express operations common to more than one purpose.  Example: You want to find the maximum.
Overloaded Constructors constructors can be overloaded, like other methods – i.e., a class can define several constructors all constructors must have the.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
Interfaces. Lecture Objectives To learn about interfaces To be able to convert between class and interface references To appreciate how interfaces can.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
CPSC150 Abstract Classes and Interfaces Chapter 10.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Abstract Classes and Interfaces
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Multiple Choice Solutions True/False a c b e d   T F.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Introduction to the “this” reserved word Java - Supplemented Learning By: Keenan Ratushniak.
Java Implementation: Part 3 Software Construction Lecture 8.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 9 - Inheritance.
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Class Example - Rationals Rational numbers are represented by the ratio of two integers, a numerator and a denominator, e.g., 2/3. This is opposed to irrational.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Chapter 9 Interfaces and Polymorphism. Chapter Goals To learn about interfaces To be able to convert between class and interface references To understand.
ECE122 Feb. 22, Any question on Vehicle sample code?
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 10 - Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
MCS 270 Spring 2014 Object-Oriented Software Development.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Drew University1 Interfaces and Polymorphism 9.1 Developing Reusable Solutions 9.2 Converting between Types 9.3 Polymorphism Common Error 9.1 Advanced.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
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)
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.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
More Sophisticated Behavior
Interfaces Unit 08.
Review What is an object? What is a class?
Inheritance and Polymorphism
Agenda Warmup AP Exam Review: Litvin A2
Methods Attributes Method Modifiers ‘static’
Chapter 3: Using Methods, Classes, and Objects
Chapter 11 – Interfaces and Polymorphism
Student Book An Introduction
Introduction interface in Java is a blueprint of a class. It has static constants and abstract methods only. An interface is a way to describe what classes.
Conditional Statements
Group Status Project Status.
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 ,
IFS410 Advanced Analysis and Design
Object Oriented Programming in java
Chapter 14 Abstract Classes and Interfaces
Interfaces.
Corresponds with Chapter 5
Presentation transcript:

Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer of client classes interfaces don't contain any code other than that, an interface is similar to a class – important distinction: methods are declared without bodies programming with interfaces is becoming more popular – rightly so

Interface Declaration an interface if declared like a class – only interface replaces class, e.g. interface Arithmetic { // field declarations // method declarations } only signatures of methods – only modifier, return type, name and parameter list – no body, only ; – e.g. interface Arithmetic { public Arithmetic add (Arithmetic number); public Arithmetic subtract (Arithmetic number); } fields are typically used only for constants

Interfaces and Classes interfaces are useless without classes – someone must implement their methods – interfaces cannot be instantiated a class can implement an interface – it must supply all the methods defined in the interface – e.g. class Complex implements Arithmetic { public Arithmetic add (Arithmetic number) {…} public Arithmetic subtract (Arithmetic number) {…} } – several classes typically implement an interface interface is a contract between a set of classes and their clients the class must fulfill the contract defined in the interface – i.e. implement all the methods defined in the interface

Classes Implementing Interfaces a class can implement several interfaces – it must supply all the methods defined in all the interfaces class Complex implements Arithmetic, Prompting { public Arithmetic add (Arithmetic number) {…} public void promptFor (String prompt) {…} } it can use all fields declared in the interfaces it implements it can have other fields and methods

Interface Variables a variable can be declared using an interface type but it's value is always an object of an implementing class – e.g. Arithmetic number = new Complex (); only methods defined in the interface can be called on it e.g. if Complex defines vector() and Arithmetic doesn't then calling number.vector(); is illegal way out: casting – convert interface variable into the class type and before the call e.g. ((Complex) number).vector(); – this is frowned upon – its dangerous: compiler can't check whether types correspond possible run-time error, if the actual value isn't the class casted

Interface Variables (cont.) a variable declared using an interface type can be assigned – various objects of any classes implementing the interface – any variable of the same interface e.g. suppose Complex and Fraction both implement Arithmetic then the following code is ok: Arithmetic number1 = new Complex (); Arithmetic number2 = number1; number2 = new Fraction (); Arithmetic number3 = number2.add(number1); but who knows what happens when we add a Complex number to a Fraction !