cc1007ni: further programming week 3 - 4 dhruba sen module leader (islington college)

38
CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Upload: herbert-barnett

Post on 22-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

CC1007NI: Further Programming

Week 3 - 4

Dhruba SenModule Leader (Islington College)

Page 2: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract Classes

Page 3: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

What is Abstration?An abstract class is one that cannot be

instantiated.

All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner.

You just cannot create an instance of the abstract class.

Page 4: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

What is Abstration?If a class is abstract and cannot be instantiated,

the class does not have much use unless it is subclassed (Inheritance).

This is typically how abstract classes come about during the design phase.

A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

Page 5: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Why abstraction?If you want a class to contain a particular method

but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

Page 6: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

‘abstract’ KeywordUse ‘abstract’ keyword to declare a class or

method as abstract.E.g.

public abstract class Animal public abstract void eat()

Page 7: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract methodsAbstract method would have no definition, and its

signature is followed by a semicolon, not curly braces as follows:

E.g.public abstract double computePay();

<no method body, no curly braces, semicolon at the end>

Page 8: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract methodsDeclaring a method as abstract has two results:

The class must also be declared abstract. If a class contains an abstract method, the class must be abstract as well.

Any child class must either override the abstract method or declare itself abstract.

Page 9: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract methodsA child class that inherits an abstract method

must override it. If they do not, they must be abstract, and any of their children must override it.

Eventually, a descendant class has to implement the abstract method; otherwise, you would have a hierarchy of abstract classes that cannot be instantiated.

Page 10: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Simulations

Programs 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

Page 11: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Predator-prey simulationsThere 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 ...

Page 12: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

The foxes-and-rabbits project

Page 13: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Main classes of interestFox

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.

Page 14: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Example of the visualization

Page 15: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Room for improvementFox and Rabbit have strong similarities but do

not have a common superclass.The update step involves similar-looking code.The Simulator is tightly coupled to specific

classes.It ‘knows’ a lot about the behavior of foxes and

rabbits.

Page 16: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

The Animal superclassPlace common fields in Animal:

age, alive, locationMethod renaming to support information hiding:

run and hunt become act.Simulator can now be significantly decoupled.

Page 17: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

The act method of AnimalStatic type checking requires an act method in Animal.

There is no obvious shared implementation.

Define act as abstract:abstract public void act(List<Animal> newAnimals);

Page 18: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract classes and methodsAbstract 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.

Page 19: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

The Animal classpublic 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}

Page 20: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

ReviewAbstract methods allow static type checking

without requiring implementation.Abstract classes function as incomplete

superclasses.No instances.

Abstract classes support polymorphism.

Page 21: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Interfaces

Page 22: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

• Interfaces

•Multiple inheritance

Main concepts to be covered

Page 23: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Abstract classes and methodsAbstract 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.

Page 24: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

The Animal classpublic 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}

Page 25: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Further abstraction

Page 26: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Selective drawing (multiple inheritance)

Page 27: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Multiple inheritanceHaving a class inherit directly from multiple

ancestors.Java forbids it for classes.Java permits it for interfaces.

No competing implementation.

Page 28: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Interfaceinterface

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.

Page 29: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Interfaces as method specifications Interfaces specify method signatures only

(like abstract methods.. The only difference is all the methods of an interface must be like this).

Each method signature is followed by a semi-colon (;)

Page 30: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

An Actor interface public interface Actor{ fields omitted

/** * Perform the actor's daily behavior. */ void act(List<Actor> newActors);  other methods omitted}

Page 31: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

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

Page 32: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Multiple interfacesBecause interfaces simply specify method

signatures, a single class can implement several different interfaces in order to ensure more methods can be implemented.

Page 33: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Classes implement an interfacepublic class Fox extends Animal implements Drawable{ any desired properties // implement required methods [modifiers] returnType methodName1(arguments) { executable code }

any other desired methods}

public class Hunter implements Actor, Drawable{ ...}

Page 34: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

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

Page 35: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Interfaces as typesImplementing classes do not inherit code, but ...... implementing classes are subtypes of the

interface type.So, polymorphism is available with interfaces as

well as classes.

Page 36: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

Example Interfaces

Page 37: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

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

Page 38: CC1007NI: Further Programming Week 3 - 4 Dhruba Sen Module Leader (Islington College)

THANK YOU.