Presentation is loading. Please wait.

Presentation is loading. Please wait.

Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes.

Similar presentations


Presentation on theme: "Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes."— Presentation transcript:

1 Abstract Classes and Interfaces Week 17

2  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes and Interfaces CONCEPTS COVERED THIS WEEK

3 Simulations Programs are regularly used to simulate real-world activities They are often only partial simulations They often involve simplifications. –Greater detail has the potential to provide greater accuracy –Greater detail typically requires more resources

4 Benefits of simulations Support useful prediction. –The weather. Allow experimentation. –Safer, cheaper, quicker. Example: –‘How will the wildlife be affected if we cut a highway through the middle of this national park?’

5 Predator-prey simulations There is often a delicate balance between species. –A lot of prey means a lot of food. –A lot of food encourages higher predator numbers. –More predators eat more prey. –Less prey means less food. –Less food means...

6 The foxes-and-rabbits project

7 Main classes of interest Fox –Simple model of a type of predator. Rabbit –Simple model of a type of prey. Simulator –Manages the overall simulation task. –Holds a collection of foxes and rabbits.

8 Example of the visualization

9 The Animal superclass Fox and Rabbit have a superclass called Animal, which contains common fields such as alive and location Fox and Rabbit have different versions of a method called act, which models their behaviour at each time step

10 The act method of Animal Static type checking requires an act method in Animal Define act as abstract: abstract public void act(List newAnimals);

11 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 newAnimals); other methods omitted }

12 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.

13 Further abstraction

14 Selective drawing (multiple inheritance)

15 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.

16 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.

17 Interfaces as method specifications Interfaces specify method signatures only Each method signature is followed by a semi-colon (;)

18 An Actor interface public interface Actor { fields omitted /** * Perform the actor's daily behavior. */ void act(List newActors); other methods omitted }

19 Features of interfaces All methods are abstract. There are no constructors. All methods are public. All fields are public, static and final.

20 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.

21 Classes implement an interface public class Fox extends Animal implements Drawable {... } public class Hunter implements Actor, Drawable {... }

22 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

23 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.

24 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


Download ppt "Abstract Classes and Interfaces Week 17.  Computer simulations  Abstract methods  Abstract classes  Interfaces  Multiple inheritance Abstract Classes."

Similar presentations


Ads by Google