1 interfaces Professor Evan Korth. review Based on the GeometricObject -> Circle -> Cylinder hierarchy from last class, which of these are legal? GeometricObject.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

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.
AP Computer Science TOPICS TO DISCUSS.equals() == instanceof operator compareTo Interfaces Abstract Classes.
Topic 5a – Interfaces Friends share all things. CISC370/Object Oriented Programming with Java.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
CS 106 Introduction to Computer Science I 11 / 20 / 2006 Instructor: Michael Eckmann.
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.
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 :::
Multiple Choice Solutions True/False a c b e d   T F.
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.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
Polymorphism & Interfaces
CISC6795: Spring Object-Oriented Programming: Polymorphism.
CS 106 Introduction to Computer Science I 04 / 20 / 2007 Instructor: Michael Eckmann.
Recitation 4 Abstract classes, Interfaces. A Little More Geometry! Abstract Classes Shape x ____ y ____ Triangle area() base____ height ____ Circle area()
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.
Programming With Java ICS201 University Of Hail1 Chapter 13 Interfaces.
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.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
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.
1 Interface Design. 2 concept An interface is a way to describe what classes should do, without specifying how they should do it. It’s not a class but.
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.
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.
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
Sections Inheritance and Abstract Classes
Interface.
Agenda Warmup AP Exam Review: Litvin A2
Interfaces Professor Evan Korth.
Chapter 13 Abstract Classes and Interfaces
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
Lecture 4: Interface Design
Chapter 19 Generics Dr. Clincy - Lecture.
Interface.
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.
Chapter 12 Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Chapter 13 Abstract Classes and Interfaces Part 01
Presentation transcript:

1 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”

InterfaceAbstract class FieldsOnly constantsConstants and variable data MethodsNo implementation allowed (no abstract modifier necessary) Abstract or concrete Interface vs. abstract class

Interface vs. abstract class (cont) InterfaceAbstract class InheritanceA 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) InterfaceAbstract class RootnoneObject (of all classes) namesAdjective or NounsNouns

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.compareToObject From java.sun.com documentation