Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)"— Presentation transcript:

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

2

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

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

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

6 ‘abstract’ Keyword Use ‘abstract’ keyword to declare a class or method as abstract. E.g. public abstract class Animal public abstract void eat()

7 Abstract methods Abstract method would have no definition, and its signature is followed by a semicolon, not curly braces as follows: E.g. public abstract double computePay();

8 Abstract methods Declaring 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.

9 Abstract methods A 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.

10 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

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

12 The foxes-and-rabbits project

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

14 Example of the visualization

15 Room for improvement Fox 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.

16 The Animal superclass Place common fields in Animal : age, alive, location Method renaming to support information hiding: run and hunt become act. Simulator can now be significantly decoupled.

17 The act method of Animal Static type checking requires an act method in Animal. There is no obvious shared implementation. Define act as abstract: abstract public void act(List newAnimals);

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

19 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 }

20 Review Abstract methods allow static type checking without requiring implementation. Abstract classes function as incomplete superclasses. No instances. Abstract classes support polymorphism.

21 THANK YOU.


Download ppt "CC1007NI: Further Programming Week 3 Dhruba Sen Module Leader (Islington College)"

Similar presentations


Ads by Google