Sei sulla pagina 1di 13
UNIT2 |67 Objec-oreted features in asses 2.5 An Abstract Blueprint — abstract Class Objectives 1. Define abstract classes and methods. 2. Manipulate an abstract class for a problem given, Introduction You learnt that a class definition defines and creates the behaviours and attributes for a group of objects at runtime. Sometimes, a programmer might find that he ‘or she knows how an object must behave but has no idea how the object performs some of the behaviours. That is, for some methods, the programmer can figure out the method name and the parameter list, but the statements in the method body cannot be determined for che time being, [Abstract classes are superclasses which contain abstract methods. It defines a general type and what behaviours such a general type can perform (or the message types it can receive), and some of the behaviours can be undefined. This leaves room for its specific types (thats, the subelasses) to realise or define chose behaviours. Therefore, an absteact class acts a a blueprint ofits subclasses. ‘When specifying an abstract class, a programmer is referring to a class which has clements that are meant to be implemented by inheritance. The abstraction of the class methods to be implemented by the sub-classes is meant to simplify software development. In Java, the keyword abstract is used to allow the programmer to specify which classes are considered abstract and will not allow these to be instantiated. This also enables the programmer to focus on planning and design. The actual implementation of course is to be done in the derived classes. Ascenario for an abstract class For example, an international software company is developing a tax calculation program that can be used by people in different countries for tax calculation. ‘Among all the classes involved, it is determined that the software needs a class ‘Taxcalculator with the method findTax ) for tax calculation as shown below: TaxCaleulator ‘Hindtax () 68] WAWASAN OPEN UNIVERSITY EC 102 Computing t However, the rales of tax calculations vary in different countries, and it is almost impossible for a programmer to incorporate the tax calculations for all countries in one method. Inheritance in the object-oriented paradigm can be a powerful tool to handle such a problem. For example, iis possible to define a general valaysiaTaxCalculator class with the method £indax ). Then, the regional office in each state can write a specific subclass of the superclass walaysiaTaxcalculator — for example PerecnalTaxcalculator — that defines its own £Snd'tax () method as visualised as below. MalaysiaTaxcalculator { Personal TaxCalculator Finarax () Findrax () ‘Then, by adding the class PersonaiTaxCalculator to the sofiware applicat the software can be used in Malaysia by creatinga Personal taxCaLcuLator object and used as iit is a NaLayeiataxcalcutator object, such as: MalaysiaTaxCaleulater caloulator = new PersonalTaxCalculator (); calculator. £indTax(.--17 Asa result, all statements other than the statement that creates the object can be kept unchanged because of polymorphism. However, there is no general global cax caleulation method, so it is unreasonable to define a findTax () method in the MelaysiataxCalculator class. However, if the class Malaysiataxcal culacor does not define the finatax () method, the calculator. findtax( will cause a compilation error. Java supports abstract classes and abstract methods to resolve the above problem. Briefly, an abstract cassis written as a superclass that defines common attributes and, concrete (or non-abstract) methods that its subclass must possess and some abstract methods its subclasses must define. Please use the following reading co learn what an abstract class and an absteact method are, and their uses. UNIT2 |69) ‘Object-oriented features in lasses. | ® Reading Malik, D S, Java Programming: From Problem Analysis to Program Design, ‘Abstract Methods and Classes.’ p. 714~716. From the reading, you know that an abstract class enables you to model some conceptual ideas that are actually non-existent. The ways it performs such operations are unknown until you really know what ies. What are abstract methods and why do we need them? ‘Therefore, from the programming point of view, an abstract method is a method definition marked abstract, and the method body, which includes the pair of curly braces, is replaced by a semi-colon, The general format is: public abeteact return-type mesbod-name(panameter-list); For example, let us look at a shape class that defines the abstract methods below: public abstract void draw(6raphics 4): public abstract int getHeight ()7 public abstract int getWideh() For a generic shape to be drawn on the screen, it must have attributes including ‘coordinates and colour. However, we know nothing about the way itis drawn on the screen and how to determine its dimensions. The baseline is that we know a specific real or concrete Shape must have Height and Width behaviours. Therefore, these methods are defined to be abstract methods and are left to be defined in its specific types or subclasses Ifa class defines one or more abstract methods, the class has to be marked abstract as well. This confirms that you are intentionally definingan abstract class. Furthermore, because abstract classes model a conceptual idea or a general cype, itis not possible to create an object of an abstract class. Therefore, with the definition of class shape mentioned earlier, the following statement new shaped) will eause a compile-time error. ‘The outcome is that if an object can be considered a shape object, its actual class ‘must not be Shape but a non-abstract subclass of the Shape class. The reason is that ir is nor possible to ereate an object of an abstract class. If an object is referred to by a variable of type shape, it must be an object of a non-abstract subclass of the Shape class. Moreover, all abstract methods must be defined in the non- abstract subclasses. The reason is that when an object receives « message that is

Potrebbero piacerti anche