Interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any.

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Inheritance Inheritance Reserved word protected Reserved word super
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
UML Class Diagram: class Rectangle
Chapter 10: Inheritance and Polymorphism
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
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.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Java Interfaces. Interfaces An interface is something like an extreme case of an abstract class – However, an interface is not a class – It is a type.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
 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.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object Oriented Programming
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
An Advanced Code Pattern: Inner Classes CSE301 University of Sunderland Harry R. Erwin, PhD Half Lecture.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
Interfaces and Inner Classes
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
CS 116 Object Oriented Programming II Lecture 9 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
More About Java and Java How to Program By Deitel & Deitel.
Web Design & Development Lecture 9
Inheritance and Polymorphism
Interface.
Chapter 11: Inheritance and Polymorphism
UML Class Diagram: class Rectangle
Comp 249 Programming Methodology
Computer Science II Exam 1 Review.
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
Interface.
CMSC 202 Interfaces.
Polymorphism, Abstract Classes & Interfaces
CSE 1030: Implementing GUI Mark Shtern.
CSC 113: Computer programming II
Java Inheritance.
CMSC 202 Interfaces.
Abstract Classes and Interfaces
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Presentation transcript:

interfaces An interface is like an extreme case of an abstract class – However, an interface is not a class – It is a type that can be satisfied by any class that implements the interface The syntax for defining an interface is similar to that of defining a class – Except the word interface is used in place of class – public interface Person An interface specifies a set of methods that any class that implements the interface must have – It contains method headings and constant definitions only – It contains no instance variables nor any complete method definitions

2 Interfaces An interface serves a function similar to a base class, though it is not a base class A class that contains only abstract methods and/or named constants Some languages allow one class to be derived from two or more different base classes This multiple inheritance is not allowed in Java Instead, Java's way of approximating multiple inheritance is through interfaces To be able to handle a variety of events, Java allows a class to implement more than one interface

Implementing an interface is like signing a contract Interface is typically used when disparate or unrelated classes share common methods and constants Allows objects to be processed polymorphically Interface is used in place of an abstract class when there is no default implementation to inherit, no fields or default method implementation

public interface Payable { public double getPaymentAmount(); } // end interface Person

An interface and all of its method headings should be declared public They cannot be given private, protected Interfaces are public and abstract by default When a class implements an interface, it must make all the methods in the interface public Because an interface is a type, a method may be written with a parameter of an interface type That parameter will accept as an argument any class that implements the interface

To implement an interface: a concrete class must do two things: 1.It must include the phrase implements Interface_Name at the start of the class definition public class Invoice implements Payable – If more than one interface is implemented, each is listed, separated by commas 2. It must implement all the method headings listed in the definition(s) of the interface(s)

Implementations of an Interface public class Invoice implements Payable { private String partNumber; private String partDescription; …… public double getPaymentAmount() { return (getQuantity() * getPricePerItem(); }

Abstract Classes Implementing Interfaces Abstract classes may implement one or more interfaces – Any method headings given in the interface that are not given definitions are made into abstract methods A concrete class must give definitions for all the method headings given in the abstract class and the interface

Abstract Classes Implementing Interfaces > Payable Person Invoice Employee Salaried Employee

Payable & Person Class implementation // Payable interface declaration public interface Payable { double getPaymentAmount(); } // Person class public class Person { protected String address; public Person (String ad) { address = new String (ad); } } // e nd Person class

Derived Interfaces (Extending an Interface) Like classes, an interface may be derived from a base interface – This is called extending the interface – The derived interface must include the phrase extends BaseInterfaceName A concrete class that implements a derived interface must have definitions for any methods in the derived interface as well as any methods in the base interface public interface X extends Y

Inheritance and interfaces are both is-a relationships Allows polymorphic use of arrays Allows polymorphic use of methods

Defined Constants in Interfaces An interface can contain defined constants in addition to or instead of method headings – Any variables defined in an interface must be public, static, and final – Because this is understood, Java allows these modifiers to be omitted Any class that implements the interface has access to these defined constants

public Interface Constants { int ONE = 1; int TWO = 2; int THREE = 3; }

Java Programming: Program Design Including Data Structures 15 Composition Another way to relate two classes One or more members of a class are objects of another class type “has-a” relation between classes – For example, “every person has a date of birth” Containment Mechanism by which the internal data of one class includes an object of another class

public class Student { private String name; private String id; public void copyStudent(Student st) { name= st.name; id= st.id ; } //... } /** Section.java -*/ public class Section { private String sectionName; private int capacity; private int currentNbStudents; private Student[ ] stud; …. public void addStudent(Student s) { stud[currentNbStudents]=s; currentNbStudents++; } //... }

public class Course { private String courseName; private int nbSection; private Section[ ] sect; //... } public class Teacher { private String teacherName; private String Id; private Section[3] sect; //... }

public class Department { private String departName; private Student[ ] stud; private Course[ ] csc; private Teacher[ ] teach; //... }

Composition Example Class PersonalInfo Includes id (string), name (class Person), birthdate (class Date)

Java Programming: Program Design Including Data Structures 20 Composition Example (continued)

public class PersonalInfo { private Person name; private Date bDay; private int personID; public PersonalInfo() { name = new Person(); bDay = new Date(); personID = 0; } public PersonalInfo(String first, String last, int month,int day, int year, int ID) { name = new Person(first,last); bDay = new Date(month,day,year); personID = ID; } public void setpersonalInfo(String first, String last, int month,int day, int year, int ID) { name.setName(first,last); bDay.setDate(month,day,year); personID = ID; } //Method to return the string containing personal information public String toString() { return ("Name: " + name.toString() + "\n" + "Date of birth: " + bDay.toString() + "\n" + "Personal ID: " + personID); }

public class Point { private int x; private int y; public Point(int x, int y) { // constructor this.x = x; this.y = y; } public Point() { // no-arg constructor x = 0; y = 0; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } }

public class Line { Point begin; Point end; // begin and end Points public Line(int x1, int y1, int x2, int y2) { // constructor 1 begin = new Point(x1, y1); end = new Point(x2, y2); } public Line(Point begin, Point end) { // constructor 2 this.begin = begin; this.end = end; } public String toString() { return "Line from " + begin + " to " + end; } }

OOD and OOP Encapsulation – combine data and operations on the data in a single unit Inheritance – ability to create new objects from existing classes Polymorphism – ability to use the same expression to denote different operations Rule of Thumb: Use composition if possible, before considering inheritance