Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Object Oriented Programming with Java
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
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.
1 Chapter 6: Extending classes and Inheritance. 2 Basics of Inheritance One of the basic objectives of Inheritance is code reuse If you want to extend.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Abstract Classes and Interfaces The objectives of this chapter are: To explore the concept of abstract classes To understand interfaces To understand the.
Chapter 10: Introduction to Inheritance
Advanced Programming in Java
Sadegh Aliakbary Sharif University of Technology Fall 2010.
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.
1 Chapter 6 Inheritance, Interfaces, and Abstract 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.
UML Class Diagram: class Rectangle
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
2.5 OOP Principles Part 1 academy.zariba.com 1. Lecture Content 1.Fundamental Principles of OOP 2.Inheritance 3.Abstraction 4.Encapsulation 2.
Object Oriented Programming: Inheritance Chapter 9.
Multiple Choice Solutions True/False a c b e d   T F.
Classes and Class Members Chapter 3. 3 Public Interface Contract between class and its clients to fulfill certain responsibilities The client is an object.
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.
(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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Java Inheritance.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
Inheritance in the Java programming language J. W. Rider.
Chapter 8 - Additional Inheritance Concepts and Techniques1 Chapter 8 Additional Inheritance Concepts and Techniques.
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.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
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.
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)
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.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
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.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Polymorphism, Abstract Classes & Interfaces
Interfaces.
Inheritance and Polymorphism
One class is an extension of another.
Agenda Warmup AP Exam Review: Litvin A2
Week 4 Object-Oriented Programming (1): Inheritance
UML Class Diagram: class Rectangle
Chapter 10 Thinking in Objects
Interfaces.
Computer Programming.
One class is an extension of another.
Interface.
MSIS 670 Object-Oriented Software Engineering
Abstract Classes AKEEL AHMED.
Interfaces.
Polymorphism, Abstract Classes & Interfaces
Advanced Java Programming
Advanced Programming in Java
Java Inheritance.
Advanced Programming in Java
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Presentation transcript:

Interfaces Chapter 9

9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined methods. Each class implements the methods however it sees fit. A class can implement multiple interfaces.

9 Declaring the Interface Similar to a class declaration but uses the interface keyword The extends keyword can be used to extend interfaces.

9 Interface Restrictions Interfaces: Cannot have member fields Can define constants Cannot have methods with implementation; all methods in an interface are implicitly abstract Cannot be instantiated Cannot define constructors

9 Implementing Multiple Interfaces: The Debuggable Interface Debugging is an important step in the programming cycle. One way to debug a program is to display information about objects and variables to ensure their validity.

9 Debugging an Interface In the ball program, seeing information about each of the objects as the program runs is helpful. Ball and Wall classes do not derive from the same base class (Java does not support multiple inheritance). Ball and Wall can implement common interfaces.

9 The Debuggable Interface The Debuggable interface defines two public methods: displayStatus(String identifier); displayError(String error); The interface does not define how to implement these methods. Implementation details are left to the classes.

9 Interfaces vs. Abstract Classes Use an abstract base class if: You are trying to create an is a relationship: A tree is a plant A fly is an insect You do not want to instantiate the base class.

9 Interfaces vs. Abstract Classes Use an interface if: You are not trying to create an is a relationship. You are stating that your class has these capabilities. A Ball and a Wall have the capability of being colorable and debuggable. You need a way to handle multiple inheritance.

9 Extending Interfaces Interfaces can be extended just like classes. Allows the programmer to provide new functionality without rewriting existing code Use the keyword extends: interface DebugLogging extends Debuggable

9 Polymorphic Interfaces Interfaces can be treated polymorphically, as a type. A method that accepts an object which implements the Debuggable interface will also accept any object which implements any interface derived from the Debuggable interface.