Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Interfaces Week 4 Main concepts to be covered •Interfaces •Multiple inheritance Abstract classes and methods • Abstract methods have abstract in the signature. • Abstract methods have no body. • The presence of at least one abstract method makes the class abstract. • Abstract classes cannot be instantiated. • Concrete (i.e. non-abstract) subclasses complete the implementation. The Animal class public abstract class Animal { fields omitted /** * Make this animal act - that is: make it do * whatever it wants/needs to do. */ abstract public void act(List<Animal> newAnimals); other methods omitted } Further abstraction Selective drawing (multiple inheritance) Multiple inheritance • Having a class inherit directly from multiple ancestors. • Each language has its own rules. – How to resolve competing definitions? • Java forbids it for classes. • Java permits it for interfaces. – No competing implementation. Interface • interface – A Java programming language keyword used to define a collection of method definitions and constant values. It can later be implemented by classes by means of the "implements" keyword. Interfaces as method specifications • Interfaces specify method signatures only • Each method signature is followed by a semi-colon (;) An Actor interface public interface Actor { fields omitted /** * Perform the actor's daily behavior. */ void act(List<Actor> newActors); other methods omitted } Features of interfaces • • • • All methods are abstract. There are no constructors. All methods are public. All fields are public, static and final. Multiple interfaces • Because interfaces simply specify method signatures, a single class can implement several different interfaces in order to ensure more methods can be implemented. Classes implement an interface public class Fox extends Animal implements Drawable { ... } public class Hunter implements Actor, Drawable { ... } Implementing an Interface • When a class implements an interface, it is essentially signing a contract. – Either the class must implement all the methods declared in the interface and its superinterfaces, or the class must be declared abstract. – The method signature in the class must match the method signature as it appears in the interface. A class that implements the ActionListener interface must contain the method ActionPerformed Interfaces as types • Implementing classes do not inherit code, but ... • ... implementing classes are subtypes of the interface type. • So, polymorphism is available with interfaces as well as classes. Interfaces are useful for the following: • Capturing similarities among unrelated classes without artificially forcing a class relationship • Declaring methods that one or more classes are expected to implement • Modelling multiple inheritance, a feature of some object-oriented languages that allows a class to have more than one superclass