Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.

Similar presentations


Presentation on theme: "1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A."— Presentation transcript:

1 1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A Definition.  Abstract Methods: An Example.  Abstract Classes and Polymorphism.  A programming Example.  Summary.  Preview: Interfaces.

2 2 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes: A Definition An abstract class represents a generic concept in a class hierarchy. It has NO implementation. Question: Why do we need abstract classes? Answer: Sometimes, a class that you define represents an abstract concept and, as such, should not be instantiated (no class objects created). Take, for example, liquid in the real world. Have you ever seen an instance of liquid? No. What you see instead are instances of water, milk, and (our favorite) honey. Liquid represents the abstract concept of things that we all can drink (and more!). It doesn't make sense for an instance of liquid to exist.

3 3 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes: A Definition (Cont’d) Similarly in Java, you may want to model an abstract concept without being able to create an instance of it. For example, the Number class in the java.lang package represents the abstract concept of numbers. It makes sense to model numbers in a program, but it doesn't make sense to create a generic number object. Instead, the Number class makes sense only as a superclass to classes like Integer and Float, both of which implement specific kinds of numbers (see diagram below). A class such as Number, which represents an abstract concept and should not be instantiated, is called an abstract class. An abstract class is a class that can only be inherited (subclassed). It cannot be instantiated. java.lang Number Integer Float Double Character Abstract superclass Implemented subclass

4 4 Lecture 06(Abstract Classes)Lecture 9 Declaring Abstract Classes To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration: If one attempts to create an instance of an abstract class, say AbstractClass, the compiler refuses to compile the program and displays an error similar to the following It is illegal to create an instance of an abstract class C:\ics_courses\semester002\Ghouti_Slides\Lecture9_Programs\AbstractClassTest.java:3: class Liquid is an abstract class. It can't be instantiated. Liquid ref1 = new Liquid(); ^1 error abstract class AbstractClass { } class body public class AbstractClassTest { public static void main(String args[]) { Liquid ref1 = new Liquid(); } // End of main() method } // End of class AbstractClassTest abstract class Liquid { int x, y; String name; } // End of abstract class Liquid Abstract Class

5 5 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: A Definition Another definition of abstract classes can be stated as follows: An abstract class is any class with at least one abstract method. Question: What is an abstract method? Answer: An abstract method is a method that does not contain an implementation. However, an abstract class can have instance data and non-abstract methods. The implementation details of the abstract methods are left to the inheriting classes (subclasses). Abstract classes have three distinguishing characteristics: 1- The declaration of an abstract class must include the word abstract, which is usually placed just before the word class. 2- Some of the methods in an abstract class may be abstract methods. An abstract method is a “dummy” method that has no body. Ex.public abstract double doubleValues(); 3- It is illegal to create an instance of an abstract class

6 6 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example Let's look at an example of when you might want to create an abstract class with an abstract method in it. In any drawing application, you can draw circles, rectangles, lines, triangles, and so on. Each of these graphic objects share certain states (position, bounding box) and behavior (move, resize, draw). You can take advantage of these similarities and declare them all to inherit from the same parent object, say abstract class Shape. Shape Circle Line Rectangle Triangle However, the graphic objects are also substantially different in many ways: drawing a circle is quite different from drawing a rectangle. The graphics objects cannot share these types of states or behavior. On the other hand, all the Shape objects must know how to draw themselves; they just differ in how they are drawn. This is a perfect situation for an abstract superclass.

7 7 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) First you would declare an abstract class, Shape, to provide member variables and methods that were wholly shared by all subclasses, such as the coordinates (x and y) and moveTo() method. Shape also declares abstract methods, such as draw(), that need to be implemented by all subclasses, but in entirely different ways (no default implementation in the superclass makes sense). The Shape class would look something like this: abstract class Shape { int x, y; // member variable... void moveTo(int newX, int newY) {... } // End of moveTo method abstract void draw(); } // End of abstract class Shape Notice the semicolon “;” No curly brackets!

8 8 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) Note that if the class Shape is not declared abstract, the compiler will not compile the program and an error message will be issued as follows: C:\ics_courses\semester002\Ghouti_Slides\Lecture9_Programs\Shape.java:1: class Shape must be declared abstract. It does not define void draw() from class Shape. class Shape { ^ 1 error Each non-abstract subclass of Shape, such as Circle and Rectangle, would have to provide an implementation for the draw() method. Remember that an abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. class Rectangle extends Shape { void draw() { } } Method body Overriding Implementation II class Circle extends Shape { void draw() { } } Method body Overriding Implementation I

9 9 Lecture 06(Abstract Classes)Lecture 9 l To illustrate abstract classes, suppose that we need to develop a series of classes that represents specific geometric shapes, such as Circle and Rectangle.. The Shape class will serve solely as a starting point for defining more-specific shapes classes. Every shape has a location and a color, so Shape class will need instance variables that store x and y coordinates and a Color object. Behaviors like: shape can be displayed, draw method., A shape can be moved so we need a move method.

10 10 Lecture 06(Abstract Classes)Lecture 9 Abstract Methods: An Example (Cont’d) l Shape // Represents a geometric shape that can be displayed in a // graphics context import java.awt.*; public abstract class Shape { // Instance variables private int x; private int y; private Color color; // Constructor protected Shape(int x, int y, Color color) { this.x = x; this.y = y; this.color = color; } // Abstract methods public abstract void draw(Graphics g); public abstract int getHeight(); public abstract int getWidth(); // Other instance methods public Color getColor() { return color; } public int getX() { return x; } public int getY() { return y; } public void move(int dx, int dy) { x += dx; y += dy; } public void setColor(Color color) { this.color = color; } Notice that the shape declared protected so No instances can be created Abstract methods

11 11 Lecture 06(Abstract Classes)Lecture 9 Circle and Rectangle subclasses Circle Rectangle import java.awt.*; public class Circle extends Shape { // Instance variables private int diameter; // Constructor public Circle(int x, int y, Color color, int diameter) { super(x, y, color); this.diameter = diameter; } // Instance methods public void draw(Graphics g) { g.setColor(getColor()); g.fillOval(getX(), getY(), diameter, diameter); } public int getHeight() { return diameter; } public int getWidth() { return diameter; } import java.awt.*; public class Rectangle extends Shape { // Instance variables private int width; private int height; // Constructor public Rectangle(int x, int y, Color color, int width, int height) { super(x, y, color); this.width = width; this.height = height; } // Instance methods public void draw(Graphics g) { g.setColor(getColor()); g.fillRect(getX(), getY(), width, height); } public int getHeight() { return height; } public int getWidth() { return width; } } Because x, y, and color are declared private in the Shape class, the draw method can’t access these variables directly. Instead, it calls the getColor, getX and getY methods

12 12 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes and Polymorphism References of abstract class can be declared, but they should refer to an instance of the non-abstract subclasses as shown below: Shape shp = new Rectangle(); In this way, abstract classes and polymorphism provide a natural way of managing similar objects with a compact code. Consider the following inheritance diagram: Game Referee Player Ball Coach Abstract Superclass

13 13 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes and Polymorphism (Cont’d) abstract class Game { // fields for location, color, etc. // other methods public abstract void draw(); } // End of abstract class Game class Player extends Game {... public void draw() { method body } // End of draw() method } // End of class Player class Ball extends Game {... public void draw() { method body } // End of draw() method } // End of class Ball public class TestGame { public static void main(String[] args) { Game[] ahly = new Game[4]; ahly[0] = new Player(); ahly[1] = new Ball(); ahly[2] = new Referee(); ahly[3] = new Coach(); for (i = 0; i < 4; i ++) ahly[i].draw(); } // End of main() method } // End of class TestGame

14 14 Lecture 06(Abstract Classes)Lecture 9 Programming Example Consider the type of the following messages: TextMessage. VoiceMessage. FaxMessage. EmailMessage. These are different types of messages with the following common features: Sender name. Sending Date. Fields getSenderName(). getDate(). Methods However, each type of these messages has its own way of readMessage() the sent message. So, the implementation details of the readMessage() is left to the inheriting subclasses (TextMessage, VoiceMessage, FaxMessage, and EmailMessage). Methods will be implemented differently in each subclass

15 15 Lecture 06(Abstract Classes)Lecture 9 Programming Example (Cont’d) abstract class Message { private String sender; private Date date; public Message (String from) { sender = from; } // End of constructor public Date getDate() { return Date; } // End of getDate() method public String getSenderName() { return sender; } // End of getSenderName() method public abstract void readMessage(); } // End of class Message class TextMessage extends Message { private String text; public TextMessage(String from, String txt) { super(from); text = txt; } // End of constructor public void readMessage () { System.out.println(text); } // End of readMessage () method } // End of class TextMessage

16 16 Lecture 06(Abstract Classes)Lecture 9 Summary  An abstract class represents a generic concept in a class hierarchy.  An abstract method is a method that does not contain an implementation.  An abstract class is any class with at least one abstract method.  An abstract class cannot be instantiated: the presence of abstract method means that it is incomplete.  An abstract class can have instance data and non-abstract methods.  Abstract methods act as placeholders that should be implemented in subclasses.  A subclass should implement all abstract methods or itself declared as abstract.  References of an abstract class can be declared, but they should refer to an instance of the non-abstract subclass.


Download ppt "1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A."

Similar presentations


Ads by Google