Download presentation
Presentation is loading. Please wait.
Published byJemimah Fox Modified over 8 years ago
1
Further Abstraction Techniques Chapter 10
2
Abstract Classes There are times when you do not want someone to be able to make an object of your class. For example, a class that is only supposed to be sub-classed, like the item from the last example. An abstract class is a class that cannot be created and can only be sub- classed.
3
Why? If you remember dynamic typing, we are allowed to use any object that matches the static type or any subclass of that type. That means we can write code that expects the base class and really have subclass object live there during execution. Then we can use method overriding to make those objects do what we want.
4
Abstract Method An abstract method is a method that contains only the signature and no method body. Ex –abstract public void act( Field currentField, Field updatedField, List newAnimals ); Notice the keyword abstract, that indicates that this method will have no body Any class that has at least one abstract method, must be declared as abstract.
5
Abstract Class Example public abstract class Animal { //fields omitted abstract public void act( Field currentField, Field updatedField, List newAnimals ); //other stuff left out } Again notice the keyword abstract
6
Sub-classing Abstract Classes When you sub-class an abstract class, if you intend to allow people to create objects of your class, you must override the abstract method(s) This makes sense if you think about it, because an abstract class is intended to be sub-classed because it doesn’t know how the sub-class will handle the abstract method So your subclass has to tell the computer what it means for that object to perform the method
7
Interfaces There is another type of class that is like abstract classes are interfaces An interface defines a type, just like classes do. They tell the computer what classes that implement the interface will do. They do not tell the computer how those classes will perform those tasks.
8
Example of Interface public interface SimulatorView { void setColor( Class cl, Color color ); boolean isVisible( Field field ); void showStatus( int step, Field field ); } Notice the keyword interface and complete lack of method bodies and access modifiers. All methods are assumed to be public.
9
Implementing an interface To implement an interface you need to let the computer know that you want to do so. You add some information to the class declaration public class AnimatedView extends JFrame implements SimulatorView { … }
10
Abstract Class vs. Interface It may seem that an abstract class and an interface are the same thing They are not An abstract class can have fields and other methods that have complete methods including bodies. Interface cannot have either of those.
11
Which to Choose So you might ask yourself, which should I choose for a class where I want to do this. The answer is it depends. If you can choose either an interface or an abstract class, then choose the interface Why? Because Java will allow you to implement more than one interface, but only extend one class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.