InterfacestMyn1 Interfaces Sometimes it is helpful to define what a class must do but not how it will do it. We have already seen an example of this: the.

Slides:



Advertisements
Similar presentations
Inheritance Inheritance Reserved word protected Reserved word super
Advertisements

Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Lecture 9 Polymorphism Richard Gesick.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
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.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
Chapter 8 Inheritance Part 1. © 2004 Pearson Addison-Wesley. All rights reserved8-2 Inheritance Inheritance is a fundamental object-oriented design technique.
Inheritance. Inheritance is a fundamental object-oriented design technique used to create and organize reusable classes Chapter 8 focuses on: deriving.
1 Abstract Classes and Interfaces. 2 The abstract Modifier  The abstract class –Cannot be instantiated –Should be extended and implemented in subclasses.
Programming With Java ICS201 University Of Ha’il1 Chapter 7 Inheritance.
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
Interfaces Chapter 9. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined.
Topics Inheritance introduction
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.
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.
Classes, Interfaces and Packages
Programming in java Packages Access Protection Importing packages Java program structure Interfaces Why interface Defining interface Accessing impln thru.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
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)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Lecture 5:Interfaces and Abstract Classes
Modern Programming Tools And Techniques-I
Java Generics.
More About Java and Java How to Program By Deitel & Deitel.
Chapter 15 Abstract Classes and Interfaces
Advanced Programming in Java
Advanced Programming in Java
Classes (Part 1) Lecture 3
Chapter 11 Inheritance and Polymorphism
Final and Abstract Classes
Inheritance and Polymorphism
Interface.
Packages, Interfaces & Exception Handling
Week 8 Lecture -3 Inheritance and Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Chapter 9 Inheritance and Polymorphism
Object-Oriented Programming: Interface
Object-Oriented Programming: Polymorphism
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.
Lecture 22 Inheritance Richard Gesick.
Inheritance, Polymorphism, and Interfaces. Oh My
Packages and Interfaces
Interfaces.
Chapter 9 Objects and Classes
CSC 113: Computer programming II
Java – Inheritance.
Java Inheritance.
Advanced Programming in Java
Fundaments of Game Design
Abstract Classes and Interfaces
Chapter 14 Abstract Classes and Interfaces
Inheritance and Polymorphism
Chapter 8 Class Inheritance and Interfaces
Review of Previous Lesson
2009 Test Key.
Chapter 13 Abstract Classes and Interfaces Part 01
Final and Abstract Classes
Inheritance Lakshmish Ramaswamy.
C++ Object Oriented 1.
Presentation transcript:

InterfacestMyn1 Interfaces Sometimes it is helpful to define what a class must do but not how it will do it. We have already seen an example of this: the abstract method. An abstract method defines the signature for a method but provides no implementation. A derived class must provide its own implementation of each abstract method defined by its base class. Thus, an abstract method specifies the interface to the method but not the implementation.

InterfacestMyn2 While abstract classes and methods are useful, it is possible to take this concept a step further. We can fully separate a class’s interface from its implementation by using the keyword interface. Interfaces are syntactically similar to abstract classes. However, in an interface, no method can include a body. That is, an interface provides no implementation whatsoever. It specifies what must be done, but not how. Once an interface is defined, any number of classes can implement it. Also, one class can implement any number of interfaces.

InterfacestMyn3 To implement an interface, a class must provide bodies (implementations) for the methods described by the interface. A class that does not implement all the methods of the interface (but does implement some of them) is an abstract class and must be declared abstract. Each class is free to determine the details of its own implementation. Thus, two classes might implement the same interface in different ways, but each class still supports the same set of methods. Thus, code that has knowledge of the interface can use objects of either class since the interface to those objects is the same.

InterfacestMyn4 By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods” aspect of polymorphism. Polymorphism will be discussed later. When declaring a method in an interface, choose a method name that describes the method’s purpose in a general manner, because the method may be implemented by many unrelated classes. A general idea of an interface is that unrelated classes implement a set of common methods. All methods declared in an interface are implicitly public abstract methods.

InterfacestMyn5 It is proper style to declare an interface’s methods without keywords public and abstract, because they are redundant in interface method declarations. When an interface is declared public, it must be in a file of the same name. Methods are declared using only their return type and signature. Interfaces cannot be instantiated – they can only be implemented by classes or extended by other interfaces. If you do not specify that the interface is public, your interface will be accessible only to classes defined in the same package as the interface.

InterfacestMyn6 package TimoSoft; public interface ElectricalDevice { void powerOn(); void powerOff(); } package TimoSoft; public interface FrequencyTuner { void setFrequency(); void getFrequency(); }

InterfacestMyn7 package TimoSoft; public class ElectricFan implements ElectricalDevice { public ElectricFan() { System.out.println("ElectricFan constructor."); } public void powerOn() { System.out.println("The power is now on, ElectricFan."); } public void powerOff() { System.out.println("The power is now off, ElectricFan."); }

InterfacestMyn8 package TimoSoft; public class MicroWaveOven implements ElectricalDevice { public MicroWaveOven() { System.out.println("MicroWaveOven constructor."); } public void powerOn() { System.out.println("The power is now on, MicroWaveOven."); } public void powerOff() { System.out.println("The power is now off, MicroWaveOven."); }

InterfacestMyn9 package TimoSoft; public class CellPhone implements ElectricalDevice, FrequencyTuner { public CellPhone() { System.out.println("CellPhone constructor."); } public void powerOn() { System.out.println("The power is now on, CellPhone."); } public void powerOff() { System.out.println("The power is now off, CellPhone."); }

InterfacestMyn10 public void setFrequency() { System.out.println("The frequency is now set, CellPhone."); } public void getFrequency() { System.out.println("This is the current frequency, CellPhone."); }

InterfacestMyn11 package TimoSoft; public class Notes implements FrequencyTuner { public Notes() { System.out.println("Notes constructor."); } public void setFrequency() { System.out.println("The frequency is now set to Middle C, Notes."); } public void getFrequency() { System.out.println("This is the current frequency, Notes."); }

InterfacestMyn12 package TimoSoft; public class Test { public static void main(String[] args) { MicroWaveOven first=new MicroWaveOven(); first.powerOn(); CellPhone second=new CellPhone(); second.getFrequency(); Notes third=new Notes(); third.getFrequency(); } run: MicroWaveOven constructor. The power is now on, MicroWaveOven. CellPhone constructor. This is the current frequency, CellPhone. Notes constructor. This is the current frequency, Notes. BUILD SUCCESSFUL (total time: 1 second)

InterfacestMyn13 Once an interface has been defined, one of more classes can implement that interface. To implement an interface, include the implements clause in a class definition and then create the methods defined by the interface. The methods that implement an interface must be declared public, because all members of an interface are implicitly public. It is both permissible and common for classes that implement interfaces to define additional members of their own.

InterfacestMyn14 The UML distinguishes an interface from other classes by placing the word interface in guillemets ( >) above the interface name. The UML expresses the relationship between a class and an interface through a relationship known as realization. A class is said to realize or implement, the methods of an interface. A class diagram models a realization as a dashed arrow with a hollow arrowhead pointing from the implementing class to the interface, Figure 1:

InterfacestMyn15 > FrequencyTuner MicroWaveOven > ElectricalDevice ElectricFanCellPhoneNotes Figure 1. A UML representation of the preceding example.

InterfacestMyn16 Variables can be declared in an interface, but they are implicitly public, static, and final. Large programs typically make use of several constant values that describe such things as array size, various limits, special values, and the like. To define a set of shared constants, simply create an interface that contains only these constants. Let’s have a simple example to demonstrate:

InterfacestMyn17 Automobile engine yields a power output of watts while cruising. Convert that power value into horsepower value. The mechanical horsepower of a sport car is 375 Convert that horsepower value into watts.

InterfacestMyn18 package TimoSoft; public interface ConversionFactors { double HP_TO_WATT=745.7; double WATT_TO_HP=1.0/HP_TO_WATT; }

InterfacestMyn19 package TimoSoft; public class SportCar implements ConversionFactors { public SportCar() { System.out.println("SportCar constructor"); } public double FromHPToWatt(double hpVal) { return HP_TO_WATT*hpVal; } public double FromWattToHP(double wVal) { return WATT_TO_HP*wVal; }

InterfacestMyn20 run: SportCar constructor The power of watts equals to horsepower. The power of 375 horsepower equals to watts. BUILD SUCCESSFUL (total time: 0 seconds)

InterfacestMyn21 From the preceding example: All the constants in an interface ConversionFactors were implicitly public, static, and final, but those modifiers can be omitted – and typically will be omitted. Alternatively you can replace the interface from the preceding example and use the capability to import static members of a class into any class that needs any of those constants:

InterfacestMyn22 package TimoSoft; public class ConversionFactors { public static final double HP_TO_WATT=745.7; public static final double WATT_TO_HP=1.0/HP_TO_WATT; }

InterfacestMyn23 package TimoSoft; import static TimoSoft.ConversionFactors.*; public class SportCar { public SportCar() { System.out.println("SportCar constructor"); } public double FromHPToWatt(double hpVal) { return HP_TO_WATT*hpVal; } public double FromWattToHP(double wVal) { return WATT_TO_HP*wVal; }

InterfacestMyn24 package TimoSoft; public class Test { public static void main(String[] args) { SportCar first=new SportCar(); System.out.println("The power of watts equals to "+ first.FromWattToHP( )+" horsepower."); System.out.println("The power of 375 horsepower equals to "+ first.FromHPToWatt(375.0)+" watts."); } run: SportCar constructor The power of watts equals to horsepower. The power of 375 horsepower equals to watts. BUILD SUCCESSFUL (total time: 2 seconds)

InterfacestMyn25 An interface can extend other interfaces, just as a class can extend another class (in inheritance relationships). However, whereas a class can extend only one other class, an interface can extend any number of interfaces. The interface declaration includes a comma-separated list of all interfaces that it extends: public interface GroupedInterface extends Interface1, Interface2, Interface3 { void doSomething(int a, double b); String doSomethingElse(String s); }

InterfacestMyn26 The interface doing the extending acquires all the methods and constants from the interface it extends. When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.