Interfaces Professor Evan Korth.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Abstract Class and Interface
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.
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.
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, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Unit 031 Interfaces What is an Interface? Interface Declaration Syntax Implementing Interfaces Using Interfaces as Types Interfaces and Inheritance Interfaces.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
Chapter 10 Classes Continued
Programming 2 LAB TA: Nouf Al-Harbi NoufNaief.net :::
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.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
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.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
AP Computer Science A – Healdsburg High School 1 Interfaces, Abstract Classes and the DanceStudio - Similarities and Differences between Abstact Classes.
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.
CS 61B Data Structures and Programming Methodology July 2, 2008 David Sun.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 1 Chapter 13 Abstract Classes and Interfaces.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Object Oriented programming Instructor: Dr. Essam H. Houssein.
Inheritance Inheritance is the process of extending the functionality of a class by defining a new class that inherit,all the features of extending class.
CS2 Module 26 Category: OO Concepts Topic: Interfaces Objectives –Interfaces.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
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)
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Interfaces Are used to model weak inheritance relationships Object-inheritance.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 7 : Interfaces King Fahd University of Petroleum & Minerals College of Computer.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 5:Interfaces and Abstract Classes
Chapter 13 Abstract Classes and Interfaces
Chapter 13 Abstract Classes and Interfaces
Web Design & Development Lecture 9
Chapter 15 Abstract Classes and Interfaces
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
A tree set Our SearchTree class is essentially a set.
Chapter 13 Abstract Classes and Interfaces
Road Map Inheritance Class hierarchy Overriding methods Constructors
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.
Chapter 13 Abstract Classes and Interfaces
Chapter 19 Generics Dr. Clincy - Lecture.
Interface.
A tree set Our SearchTree class is essentially a set.
Lecture 16 - Interfaces Professor Adams.
Chapter 12 Abstract Classes and Interfaces
Advanced Programming in Java
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

interfaces Professor Evan Korth

review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject g = new Circle(); Circle circle = new Circle(); Cylinder cylinder = new Circle(); What is the purpose of an abstract class? If you write a class to extend an abstract class, what must you do? There are two possible answers to this question.

Interfaces In Java, only single inheritance is permitted. However, Java provides a construct called an interface which can be implemented by a class. Interfaces are similar to abstract classes (we will compare the two soon). A class can implement any number of interfaces. In effect using interfaces gives us the benefit of multiple inheritance without many of it’s problems. Interfaces are compiled into bytecode just like classes. Interfaces cannot be instantiated. Can use interface as a data type for variables. Can also use an interface as the result of a cast operation. Interfaces can contain only abstract methods and constants.

Interfaces (cont) An interface is created with the following syntax: modifier interface interfaceID { //constants/method signatures }

syntax public class Circle extends GeometricObject implements Comparable { /* define class here make sure to implement all the abstract methods contained in the interface(s)*/ }

Interfaces (cont) An interface can extend other interfaces with the following syntax: modifier interface interfaceID extends comma-delimited-list-of-interfaces { //constants/method signatures } Obviously, any class which implements a “sub-interface” will have to implement each of the methods contained in it’s “super-interfaces”

Interface vs. abstract class Fields Only constants Constants and variable data Methods No implementation allowed (no abstract modifier necessary) Abstract or concrete

Interface vs. abstract class (cont) Inheritance A subclass can implement many interfaces A subclass can inherit only one class Can extend numerous interfaces Can implement numerous interfaces Cannot extend a class Extends one class

Interface vs. abstract class (cont) Root none Object (of all classes) names Adjective or Nouns Nouns

Comparable interface This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.  int compareTo (Object o) Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. From java.sun.com documentation