Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?

Slides:



Advertisements
Similar presentations
Object Oriented Programming with Java
Advertisements

Lecture 5: Interfaces.
OOP: Inheritance By: Lamiaa Said.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 1 Object-Oriented.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
UFCE3T-15-M Programming part 1 UFCE3T-15-M Object-oriented Design and Programming Block2: Inheritance, Polymorphism, Abstract Classes and Interfaces Jin.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 10 Classes Continued
Object-Orientated Design and Programming Unit 9: Abstract Classes and Interfaces Jin Sa.
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 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Abstract Classes and Interfaces Lecture 2 – 9/6/2012.
Lecture 3 Casting Abstract Classes and Methods Interfaces.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
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 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Programming in Java CSCI-2220 Object Oriented Programming.
 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 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Object Oriented Programming
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Inheritance-Basics.
Inheritance and Polymorphism
Chapter 13 Abstract Classes and Interfaces
Interface.
Java Programming Language
Packages and Interfaces
Chapter 12 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Final and Abstract Classes
Presentation transcript:

Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?

Inheritance F OOP allows you to derive new classes from existing classes. This is called inheritance. F Inheritance is an important and powerful concept in Java. Every class you define in Java is inherited from an existing class. F Sometimes it is necessary to derive a subclass from several classes, thus inheriting their data and methods. In Java only one parent class is allowed. F With interfaces, you can obtain effect of multiple inheritance.

What is an Interface? F An interface is a classlike construct that contains only constants and abstract methods.  Cannot be instantiated. Only classes that implements interfaces can be instantiated. However, final public static variables can be defined to interface types. F Why not just use abstract classes? Java does not permit multiple inheritance from classes, but permits implementation of multiple interfaces.

What is an Interface? F Protocol for classes that completely separates specification/behaviour from implementation. F One class can implement many interfaces F One interface can be implemented by many classes  By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism.

Why an Interface? F Normally, in order for a method to be called from one class to another, both classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are compatible. F In a system like this, functionality gets pushed up higher and higher in the class hierarchy so that the mechanisms will be available to more and more subclasses. F Interfaces are designed to avoid this problem. F Interfaces are designed to support dynamic method resolution at run time.

Why an Interface? F Interfaces are designed to avoid this problem. F They disconnect the definition of a method or set of methods from the inheritance hierarchy. F Since interfaces are in a different hierarchy from classes, it is possible for classes that are unrelated in terms of the class hierarchy to implement the same interface. F This is where the real power of interfaces is realized.

Creating an Interface File InterfaceName.java: modifier interface InterfaceName { constants declarations; methods signatures; } Modifier is public or not used. File ClassName.java: modifier Class ClassName implements InterfaceName { methods implementation; } override If a class implements an interface, it should override all the abstract methods declared in the interface.

Creating an Interface public interface MyInterface { public void aMethod1(int i); // an abstract methods public void aMethod2(double a);... public void aMethodN(); } public Class MyClass implements MyInterface { public void aMethod1(int i) { // implementaion } public void aMethod2(double a) { // implementaion }... public void aMethodN() { // implementaion } }

Creating a Multiple Interface modifier interface InterfaceName1 { methods1 signatures; } modifier interface InterfaceName2 { methods2 signatures; }... modifier interface InterfaceNameN { methodsN signatures; }

Creating a Multiple Interface If a class implements a multiple interfaces, it should override all the abstract methods declared in all the interfaces. public Class ClassName implements InterfaceName1, InterfaceName2, …, InterfaceNameN { methods1 implementation; methods2 implementation;... methodsN implementation; }

Example of Creating an Interface // This interface is defined in // java. lang package public interface Comparable { public int compareTo(Object obj); }

UML Notation

Comparable Circle public interface Comparable { public int compareTo(Object obj); } public Class Circle extends Shape implements Comparable { public int compareTo(Object o) { if (getRadius() > ((Circle)o).getRadius()) return 1; else if (getRadius()<((Circle)o).getRadius()) return -1; else return 0; }

Comparable Cylinder public Class Cylinder extends Circle implements Comparable { public int compareTo(Object o) { if(findVolume() > ((Cylinder)o).findVolume()) return 1; else if(findVolume()<((Cylinder)o).findVolume()) return -1; else return 0; }

Generic findM ax Method public class Max { // Return the maximum between two objects public static Comparable findMax(Comparable ob1, Comparable ob2) { if (ob1.compareTo(ob2) > 0) return ob1; else return ob2; }

Access via interface reference F You can declare variables as object references that use an interface rather than a class type. Any instance of any class that implements the declared interface can be stored in such a variable. Example: Comparable circle; F When you call a method through one of these references, the correct version will be called based on the actual instance of the interface being referred to. Example: circle = Max.findMax(circle1, circle2);

Using Interface This is one of the key features of interfaces. F The method to be executed is looked up dynamically at run time, allowing classes to be created later than the code which calls methods on them. F The calling code can dispatch through an interface without having to know anything about the "callee."

Using Interface Objective: Use the findMax() method to find a maximum circle between two circles: Circle circle1 = new Circle(5); Circle circle2 = new Circle(10); Comparable circle = Max.findMax(circle1, circle2); System.out.println(((Circle)circle).getRadius()); // 10.0 System.out.println(circle.toString()); // [Circle] radius = 10.0

Interfaces vs. Abstract Classes  In an interface, the data must be constants; an abstract class can have all types of data.  Each method in an interface has only a signature without implementation;  An abstract class can have concrete methods. An abstract class must contain at least one abstract method or inherit from another abstract method.

Interfaces vs. Abstract Classes, cont. Since all the methods defined in an interface are abstract methods, Java does not require you to put the abstract modifier in the methods in an interface, but you must put the abstract modifier before an abstract method in an abstract class.

Interfaces & Classes One interface can inherit another by use of the keyword extends. The syntax is the same as for inheriting classes. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.

Interfaces & Classes, cont. public Interface1_1 { … } public Interface1_2 { … } public Interface2_1 { … } public Interface2_2 { … } public Interface1 extends Interface1_1, Interface1_2 {...} public abstract Class Class1 implements Interface1 {... } public Class Class2 extends Class1 implements Interface2_1, Interface2_2 {...}

Interfaces vs. Absract Classes  A strong is-a relationship that clearly describes a parent-child relationship should be modeled using classes. Example: a staff member is a person.  A weak is-a relationship, also known as a-kind-of relationship, indicates that an object possesses a certain property. Example: all strings are comparable, so the String class implements the Comparable interface.  The interface names are usually adjectives. You can also use interfaces to circumvent single inheritance restriction if multiple inheritance is desired. You have to design one as a superclass, and the others as interfaces.

The Cloneable Interfaces public interface Cloneable { } Marker Interface: An empty interface. A marker interface does not contain constants or methods, but it has a special meaning to the Java system. The Java system requires a class to implement the Cloneable interface to become cloneable.

The Cloneable Interfaces public Class Circle extends Shape implements Cloneable { public Object clone() { try { return super.clone() } catch (CloneNotSupportedException ex) { return null; } Circle c1 = new Circle(1,2,3); Circle c2 = (Circle)c1.clone(); This is shallow copying with super.clone() method. Override the method for deep copying.