1 interface &implements. 2 an interface is a classlike construct that contains only constants...

27
1 Interface &Implements

Upload: wilfrid-bradley

Post on 18-Jan-2018

231 views

Category:

Documents


0 download

DESCRIPTION

3 Declaration: public interface InterfaceName { /* constant declaration */ /* method declarations (without implementations.) */ }  Constant declaration  public, static and final  Method  abstract,public Interface declaration

TRANSCRIPT

Page 1: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

1

Interface &Implements

Page 2: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

2

Interface & Implements

An interface is a classlike construct that contains only constants variables and abstract methods definition.

An interface cannot contain instance variable and solid methods.

An interface specifies what methods will be implemented by classes that implements an interface.

This is another way to design polymorphic methods.

Page 3: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

3

Declaration:public interface InterfaceName

{ /* constant declaration */ /* method declarations (without

implementations.) */ }

Constant declarationpublic, static and final

Methodabstract,public

Interface declaration

Page 4: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

4

Ex: Interface declaration(constant variables)

public interface Conversionfactors{ double inToMm = 25.3; double ounToGram = 28.34952; double poundToGram = 453.5924 ;}• Save as ConversionFactors.java

Page 5: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

5

public interface Conversions {public double InToMm();public double OunceToGram();public double PoundToGram();

}• Save as Conversions.java

Ex: Interface declaration (Abstract Methods)

Page 6: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

6

public interface MyInterface { public final int aConstant = 32; public final double pi = 3.14159; public void methodA( int x ); double methodB(); }

Ex: Interface declaration (variables and Methods)

Page 7: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

7

Implementing an Interface A class may extends one parent (superclass), but it

can implement zero or more interfaces A class that implements an interface:

Can access directly all the variables declared in the interface

Have to redefined all the methods declared in the interface

Declaration: class SomeClass extends Parent implements SomeInterface

{ ordinary class definition body }

Page 8: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

8

The body of the class definition is the same as always.

If any of the abstract methods (in the interface) are not defined in the class that implements the interface, the class will become an abstract class

Page 9: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

9

public class myClass implements Conversionfactors, Conversions

public void papar() {System.out.println ("Aturcara tukar ukuran kepada metrik.");System.out.println ("1 pound = " + poundToGram + " gram");System.out.println ("1 ounce = " + ounToGram + " gram.");System.out.println ("1 inci = " + inToMm + " milimeter.");}

Page 10: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

10

Interfaces in UML

GradedActivity

RelatableFinalExam3

A dashed line with an arrow indicates implementation of an interface.

Page 11: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

11

public interface Speakable{ public String speak();}

public class Animal{ protected String kind; public Animal() { }; public String toString(){ return "I am a " + kind + " and I go " + ((Speakable)this).speak(); }}

Example 1

Page 12: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

12

public class Cat extends Animal implements Speakable { public Cat() { kind ="cat"; } public String speak() { return "meow"; }}

public class Cow extends Animal implements Speakable { public Cow() { kind ="cow"; } public String speak() { return "moo"; }}

Example 1

Page 13: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

13

Example

Bookauthor

Constructor(parameter)

TaxabletaxRate

double calculateTax( )

Goodsdescriptionprice

abstract void display( )Constructor(parameter)

ToyminAge

Constructor(parameter)

Foodcalories

Constructor(parameter)

interface

Toy and Book implements Taxable

Page 14: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

14

public interface Taxable { final double taxRate = 0.06 ; double calculateTax() ; }

abstract class Goods {

String description;

double price;

Goods( String des, double pr ) {

description = des;

price = pr; }

abstract void display();

}

interface

abstract class

Page 15: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

15

class Toy extends Goods implements Taxable { int minimumAge; Toy( String des, double pr, int min) { super( des, pr ); minimumAge = min ; } void display() { System.out.println( "item: " + description + " price: " + price ); System.out.println( "minimum age: " + minimumAge ); System.out.println( "Cukai: " + calculateTax() ); } public double calculateTax() { return price * taxRate ; } }

Definition of interface’s method

Page 16: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

16

class Book extends Goods implements Taxable

{ String author;

Book( String des, double pr, String auth)

{ super( des, pr );

author = auth ;

}

void display()

{System.out.println( "item: " + description +

" price: " + price );

System.out.println( "author: " + author );

System.out.println( "Cukai: " + calculateTax() );

}

public double calculateTax()

{ return price * taxRate ; }

}

Page 17: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

17

class Food extends Goods

{ double calories;

Food( String des, double pr, double cal)

{ super( des, pr );

calories = cal ;

}

void display() {

System.out.println( "item: " + description + " price: " + price );

System.out.println( "calories: " + calories ); }

}

Page 18: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

18

public class Store2

{

public static void main ( String[] args )

{ Goods[] inventory = new Goods[10];

inventory[0] = new Food( "Cadbury", 1.40,2000 ); inventory[1] = new Food ( "Mushroom", 4.45,1500);

inventory[2] = new Book ("Emma", 4.95,"Austin" );

inventory[3] = new Toy ( "Leggos", 54.45, 8 );

for (int i=0;i<inventory.length;i++)

inventory[i].display(); }

}

Page 19: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

19

Interface vs Abstract• In many ways, an interface is similar to an

abstract class, but an abstract class can contain variables and concrete methods as well as constants and abstract methods.

• In an interface, the data must be constants; an abstract class can have all types of data.

• Each method in an interface has only a signature without implementation; an abstract class can have concrete methods.

Page 20: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

20

Variables Constructors Methods

Abstract class

No restrictions Constructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator.

No restrictions.

Interface All variables must be public static final

No constructors. An interface cannot be instantiated using the new operator.

All methods must be public abstract instance methods

Interface vs Abstract

Page 21: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

21

All data fields are public final static and all methods are public abstract in an interface. For this reason, these modifiers can be omitted, as shown below:

public interface T1 { public static final int K = 1; public abstract void p(); }

Equivalent public interface T1 { int K = 1; void p(); }

A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K).

Interface vs Abstract

Page 22: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

22

All classes share a single root, the Object class, but there is no single root for interfaces.

Like a class, an interface also defines a type.

A variable of an interface type can reference any instance of the class that implements the interface.

If a class extends an interface, this interface plays the same role as a superclass. You can use an interface as a data type and cast a variable of an interface type to its subclass, and vice versa.

Interface vs Abstract

Page 23: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

23

Object Class1

Interface1 Interface1_1

Interface1_2

Class2

Interface2_1

Interface2_2

Suppose that c is an instance of Class2. c is also an instance of Object, Class1, Interface1, Interface1_1, Interface1_2, Interface2_1, and Interface2_2.

Interface vs Abstract

Page 24: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

24

Classes Interfaces

A superclass provides a secondary data type to objects of its subclasses.

An abstract class cannot be instantiated.

An interface provides a secondary data type to objects of classes that implement that interface.

An interface cannot be instantiated.

Similarities

Page 25: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

25

Classes Interfaces

A concrete subclass of an abstract class must define all the inherited abstract methods.

A class can extend another class. A subclass can add methods and override some of its superclass’s methods.

A concrete class that implements an interface must define all the methods specified by the interface.

An interface can extend another interface (called its superinterface) by adding declarations of abstract methods.

Similarities

Page 26: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

26

Classes Interfaces

• A class can extend only one class.

• A class can have fields.

• A class defines its own constructors (or gets a default constructor).

• A class can implement any number of interfaces.

• An interface cannot have fields (except, possibly, some public static final constants).

• An interface has no constructors.

Differences

Page 27: 1 Interface &Implements. 2 An interface is a classlike construct that contains only constants variables and abstract methods definition. An interface

27

Classes Interfaces

A concrete class has all its methods defined. An abstract class usually has one or more abstract methods.

Every class is a part of a hierarchy of classes with Object at the top.

All methods declared in an interface are abstract.

An interface may belong to a small hierarchy of interfaces, but this is not as common.

Differences