Interfaces.

Slides:



Advertisements
Similar presentations
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism: Abstract Classes The “not quite” classes.
Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Inheritance and Polymorphism Recitation 04/10/2009 CS 180 Department of Computer Science, Purdue University.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
UML Class Diagram: class Rectangle
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.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Day Class Definitions and Methods Local & global variables, parameters & arguments,
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
MIT AITI 2004 – Lecture 13 Abstract Classes and Interfaces.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Side effects A side effect is anything that happens in a method other than computing and/or returning a value. Example: public class hello { public int.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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.
Inheritance and Polymorphism
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
(c) University of Washington05-1 CSC 143 Java Abstract Classes and Frameworks Reading: Ch. 11.
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.
Software Construction Lab 05 Abstraction, Inheritance, and Polymorphism in Java.
9.1 CLASS (STATIC) VARIABLES AND METHODS Defining classes is only one aspect of object-oriented programming. The real power of object-oriented programming.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Modern Programming Tools And Techniques-I
Chapter 5: Enhancing Classes
More About Java and Java How to Program By Deitel & Deitel.
Web Design & Development Lecture 9
Advanced Programming in Java
Advanced Programming in Java
Sections Inheritance and Abstract Classes
Lecture 12 Inheritance.
Inheritance and Polymorphism
One class is an extension of another.
Agenda Warmup AP Exam Review: Litvin A2
Chapter 3: Using Methods, Classes, and Objects
Section 11.1 Class Variables and Methods
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.
UML Class Diagram: class Rectangle
Object-Oriented Programming: Polymorphism
One class is an extension of another.
Class Inheritance (Cont.)
Object-Oriented Programming: Interface
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
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.
Week 6 Object-Oriented Programming (2): Polymorphism
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Inheritance and Polymorphism
CS18000: Problem Solving and Object-Oriented Programming
Advanced Programming in Java
Inheritance Inheritance is a fundamental Object Oriented concept
Java Inheritance.
Advanced Programming in Java
Chapter 9 Carrano Chapter 10 Small Java
Abstract Classes and Interfaces
2009 Test Key.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

Interfaces

9.2 Java Interfaces The Client Perspective The term interface is used in two different ways. An interface is the part of a software system that interacts with human users. An interface is a list of a class's public methods.

9.2 Java Interfaces The Client Perspective The code in an interface consists of method signatures followed by semicolons An interface provides programmers with the ability to know what an implementing class can do It is important to realize that an interface is not a class, and that the methods of an interface have no implementation

Sample Interface – Animal Interface public interface Animal{ public String getName(); public void setName(String s); public int getAge(); public void setAge(int ag); public boolean hasFur(); public void setHasFur(boolean b); }

Interfaces A class can implement an interface with the Java reserved word implements implements must be placed in the class header Example) public class Dog implements Animal{ // All methods of the Animal interface must // be defined within the Dog class }

Interfaces If a class implements an interface, it MUST define all of the methods that are listed in an interface Eclipse provides a shortcut that allows programmers to add unimplemented methods to a class Simply click on the error message next to the class header and select: Add unimplemented methods

Interfaces and Instantiation Interfaces CANNOT be instantiated However, it is possible to declare variables of Interface types Examples) Animal a1 = new Animal(); // Not Valid Animal a1; // Valid Animal a1 = new Dog(); // Valid, but a1 can only use methods // from the Animal interface

9.3 Java Interfaces - The Implementation Perspective Summary An interface contains only methods, never variables. The methods in an interface are usually public. If more than one class implements an interface, its methods are polymorphic. A class can implement methods in addition to those listed in the interface. A class can implement more than one interface. Interfaces can be organized in an inheritance hierarchy.

9.5 Inheritance and Abstract Classes A relationship between a class and an interface is represented with a dotted-line

9.6 Some Observations About Interfaces and Inheritance A Java interface has a name and consists of a list of method headers. One or more classes can implement the same interface. If a variable is declared to be of an interface type, then it can be associated with an object from any class that implements the interface. If a class implements an interface, then all its subclasses do so implicitly.