1 / 6

Abstract Class and Method

Abstract Class and Method. Abstract Classes. Any class containing an abstract method is an abstract class. You must declare the class with the keyword abstract. abstract class MyClass { … }; An abstract class is incomplete. Its method bodies have “missing” implementations.

etenia
Download Presentation

Abstract Class and Method

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 Class and Method

  2. Abstract Classes • Any class containing an abstract method is an abstract class. • You must declare the class with the keyword abstract. abstract class MyClass { … }; • An abstract class is incomplete. Its method bodies have “missing” implementations. • You can not instantiate (create a new instance of) an abstract class.

  3. The child of an abstract class must override the abstract methods of the parent, or it too will be considered abstract An abstract method cannot be defined as final (because it must be overridden) or static (because it has no definition yet) The use of abstract classes is a design decision; it helps us establish common elements in a class that is to general to instantiate Usually concrete classes extend abstract ones, but the opposite is also possible. Can an abstract method be private? Abstract Classes

  4. Why have Abstract Classes? • Suppose you wanted to create a class Shape with subclasses Oval, Rectangle, Triangle . • You do not want to allow the creation of a Shape • Only particular shapes make sense, not generic ones. • If Shape is abstract, you can’t create a new Shape . • You can create a new Oval, new Rectangle . • Abstract classes are used in providing a general category containing specific, “concrete” classes.

  5. Abstract methods • You can declare an object without defining it: Employee john; • Similarly you can declare a method without defining it: public abstract void draw(Graphics g); Notice that the body of the method is missing ! • A method that has been declared but not defined is an abstract method.

  6. Example Program abstract class Graphic Object //abstract class { int x, y; void move To(int newX, int newY) { ... } abstract void draw(); //abstract method declaration abstract void resize(); } class Circle extends Graphic Object { void draw() { ... } //abstract method definition void resize() { ... } } class Rectangle extends Graphic Object { void draw() { ... } void resize() { ... } }

More Related