Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Lecture 5: Interfaces.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Interface & Abstract Class. Interface Definition All method in an interface are abstract methods. Methods are declared without the implementation part.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Abstract Classes and Interfaces.
Object-Oriented PHP (1)
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
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.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
Abstract Classes and Interfaces. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method without.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
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 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
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.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
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,
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Coming up: Inheritance
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
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.
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.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 1 Chapter 15 Abstract Classes and Interfaces.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Abstract classes and interfaces
Summary prepared by Kirk Scott
Chapter 13 Abstract Classes and Interfaces
Chapter 12 Abstract Classes and Interfaces
Advanced Java Programming
Chapter 14 Abstract Classes and Interfaces
Chapter 8 Class Inheritance and Interfaces
Final and Abstract Classes
Presentation transcript:

Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott

A class’s (public) interface is the collection of methods that can be called on it (If you declare instance variables public, they also become part of the interface) You can think of the interface as a contract or commitment to do what a descriptive method name implies The implementation of the interface consists of the code for the methods

As you know, Java elevates the idea of an interface to a separate concept A Java interface consists of the specification of the methods A class which implements the interface is committed to providing code which performs the desired actions for those methods Separating the interface specifications into a separate construct makes two things possible:

1. Two different classes can be declared to implement the same interface This means that instances of those two classes can play the same role in various pieces of code In other words, not only can you have a superclass reference You can also have an interface reference in code

2. A given class can implement more than one interface As pointed out in CSCE 202, interfaces are a substitute for multiple inheritance in Java More generally, you can observe this: It means that an object of a given class can play different roles in different pieces of software, depending on its interface reference

Interfaces allow for great flexibility in code design and implementation The simple idea of an interface reference can be extremely useful Interfaces also form the foundation for a number of different design patterns

Interfaces and Abstract Classes C++ and Smalltalk do not have interfaces They have abstract classes Java interfaces can be described as completely abstract classes Some authors don’t like that comparison, but in this book it is considered acceptable

Challenge 2.1 Write down three differences between abstract classes and interfaces in Java

Solution 2.1 An abstract class with no nonabstract methods is similar to an interface in terms of its utility. However, note the following. A class can implement any number of interfaces but can subclass at most one abstract class

An abstract class can have nonabstract methods; al the methods of an interface are effectively abstract. An abstract class can declare and use fields; an interface cannot, although it can create static final constants

An abstract class can have methods whose access is public, protected, private, or none (package). An interface’s methods are implicitly public. An abstract class can define constructors; an interface cannot.

It is possible to write object-oriented software without interfaces, using a language like C++ However, the existence of interfaces in Java is a net plus The book asserts that they are particularly useful in n-tier (multi-tier, client-server) development

The book next gives a moderately garbled interface definition in order to pose the questions in the next challenge This may not be the best way to emphasize the points they want to bring out, but the answers to the challenge do include some new information Consider the interface definition on the following overhead:

package com.oozinoz.simulation; public interface RocketSim { abstract double getMass(); public double getThrust(); void setSimTime(double t); }

Challenge 2.2 Which of the following statements are true? [Rather than putting all of the answers at the end, the challenge is presented in the form question/answer.]

A. All three methods of the RocketSim interface are abstract, although only getMass() declares this explicitly. True. Interface methods are always abstract, whether or not they declare it.

B. All three methods of the interface are public, although only getThrust() declares this explicitly. True. Interface methods are public, whether or not they declare it.

C. The interface is declared “public interface”, but it would be public even if the public keyword were omitted. False! An interface’s visibility may be limited to the package in which it resides. In this case, it is marked public, so classes outside com.oozinoz.simulation can access it.

D. It is possible to create another interface, say, RocketSimSolid, that extends RocketSim. True. For example, the List and Set interfaces both extend the Collection interface in java.util.

E. Every interface must have at least one method. False. An interface with no methods is known as a marker interface. Sometimes, a method high in a class hierarchy, such as Object.clone(), is not appropriate for every subclass.

You can create a marker interface that requires subclasses to opt in or opt out of participation in such a scheme. The clone() method on Object requires subclasses to opt in, by declaring that they implement the Cloneable marker interface.

F. An interface can declare instance fields that an implementing class must also declare. False. An interface cannot declare instance fields, although it can create constants by declaring fields that are static and final.

G. Although you can’t instantiate an interface, an interface definition can declare constructor methods that require an implementing class to provide constructors with given signatures. False. It might be a good idea, but there is no way for a Java interface to require that implementing classes provide a particular constructor.

Interfaces and Obligations The utility of interfaces, both generic public interfaces, and interfaces separately defined, comes from the idea of encapsulation On the one hand, code that uses a class cannot mess with the contents of the class On the other hand, code that uses a class doesn’t care how methods are implemented

These are constraints that limit the interaction between classes But the limitation is very helpful One class only needs to know the following: What is the interface for the other class What does the interface support (hopefully the method names are descriptive) How is the interface used (accessibility, parameters, return types, etc.)

In general, say with RocketSim, the developer is obligated to implement methods that fulfill the “contract” implied by the interface In other words, getMass() and getThrust() really ought to return correct values for these quantities for a given object of a class that implements the simulation interface

There is another, special case, where the implementer of an interface is not providing a service to the caller It turns out that implementing the interface makes it possible, in effect, for the caller to provide a service to the code that’s called

You will discover that you are already familiar with this situation The book introduces it as a challenge Because you’ve probably never thought about it in this way, as usual, you can’t predict what the answer to the challenge will be

Challenge 2.3 Give an example of an interface with methods that do not imply responsibility on the part of the implementing class to return a value or even to take any action on behalf of the caller at all.

Solution 2.3 One example occurs when classes may be registered as listeners for events; the classes receive notification for their own benefit, not the caller’s. For example, we may want to take action on MouseListener.mouseDragged() but have an empty body for MouseListener.mouseMoved() for the same listener.

You are familiar with the Java API approach to making this simpler In addition to a MouseListener interface, there is a MouseAdapter class Instead of implementing the interface and providing bogus method implementations, you can extend the class, inherit bogus implementations, and override only those of interest

The book returns to the idea of constants in interfaces, giving a concrete example: public interface ClassificationConstants { static final int CONSUMER = 1; static final int DISPLAY = 2; }

Summary [Direct quote from book.] The power of interfaces is that they delineate what is and isn’t expected in how classes collaborate. Interfaces are similar to purely abstract classes, defining expectations but not implementing them.

Mastering both the concepts and details of applying Java interfaces is well worth the investment of your time. This powerful construct is at the heart of many strong designs and several design patterns.

The End