Download presentation
Presentation is loading. Please wait.
Published byWilfrid Bradley Modified over 9 years ago
1
1 Interface &Implements
2
2 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.
3
3 Declaration: public interface InterfaceName { /* constant declaration */ /* method declarations (without implementations.) */ } Constant declaration public, static and final Method abstract,public Interface declaration
4
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
5
5 public interface Conversions { public double InToMm(); public double OunceToGram(); public double PoundToGram(); } Save as Conversions.java Ex: Interface declaration (Abstract Methods)
6
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)
7
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 }
8
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
9
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."); }
10
10 Interfaces in UML GradedActivity RelatableFinalExam3 A dashed line with an arrow indicates implementation of an interface.
11
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
12
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
13
13 Example interface Toy and Book implements Taxable
14
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
15
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
16
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 ; } }
17
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 ); } }
18
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(); } }
19
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.
20
20 VariablesConstructorsMethods Abstract class No restrictionsConstructors are invoked by subclasses through constructor chaining. An abstract class cannot be instantiated using the new operator. No restrictions. InterfaceAll 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
21
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: A constant defined in an interface can be accessed using syntax InterfaceName.CONSTANT_NAME (e.g., T1.K). Interface vs Abstract
22
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
23
23 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
24
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
25
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
26
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
27
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.