1 / 16

Abstract Classes

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. Coming Next: Java Interfaces. Abstract Classes: A Definition.

karim
Download Presentation

Abstract Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. • Coming Next: Java Interfaces. Abstract Classes

  2. 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. Abstract Classes

  3. Abstract Classes: A Definition (Cont’d) java.lang Abstract superclass Number Implemented subclass Double Integer Character Float 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. Abstract Classes

  4. Declaring Abstract Classes abstract class AbstractClass { } class body To declare that your class is an abstract class, use the keyword abstract before the class keyword in your class declaration: public class AbstractClassTest { public static void main(String args[]) {Liquid ref1 = new Liquid(); } // End of main() method} // End of class AbstractClassTestabstract class Liquid { int x, y; String name;} // End of abstract class Liquid Abstract Class 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 Classes

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

  6. Abstract Methods: An Example Shape Circle Line Rectangle Triangle Let's look at an example of when you might want to create an abstract class with anabstract 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. 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. Abstract Classes

  7. Abstract Methods: An Example (Cont’d) 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! 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 Classes

  8. Abstract Methods: An Example (Cont’d) class Rectangle extends Shape { void draw() { }} class Circle extends Shape { void draw() { }} Method body Method body Overriding Implementation II Overriding Implementation I 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. Abstract Classes

  9. 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. Abstract Classes

  10. Abstract Methods: An Example (Cont’d) • Shape Notice that the shape declared protected so No instances can be created Abstract methods Abstract Classes

  11. Circle and Rectangle subclasses Circle Rectangle 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 Abstract Classes

  12. Abstract Classes and Polymorphism Game AbstractSuperclass Referee Player Ball Coach 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: Abstract Classes

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

  14. Programming Example • getSenderName(). • getDate(). • Sender name. • Sending Date. Methods Fields • Consider the type of the following messages: • TextMessage. • VoiceMessage. • FaxMessage. • EmailMessage. • These are different types of messages with the following common features: 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 Abstract Classes

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

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

More Related