Baixe o app para aproveitar ainda mais
Prévia do material em texto
Projeto de Software / P3 Classes Abstratas Márcio Ribeiro marcio@ic.ufal.br twitter.com/marciomribeiro 2 Specific capabili+es § What if we know that all subclasses should have some capability, but we don’t know how those subclasses will implement it? § There would be no code that could be wri?en for that capability that would apply to every subclass 3 Different taxes for each product… § Discounts § TV = 20% § Refrigerator = 35% § Stove = 15% Remember? Change requests all the +me! Abstract classes Product! - brand : String" - price : double" + getBrand() : String" + getPrice() : double" TV! - inches : int" + getInches(): int" Refrigerator! - size : int" + getSize(): int" Stove! - burners : int" + getBurners(): int" Now the getPrice implementation is not common anymore! 6 What about new products? § How to force they will have getPrice? 7 Abstract classes public abstract class Product { private String brand; protected double price; public String getBrand() { return brand; } public String toString() { return "Brand = " + brand + "\nPrice = " + price; } public abstract double getPrice(); } 8 Abstract classes § Methods for which no implementa+on in the superclass makes sense should be declared abstract § Abstract means the method has no defini+on, only declara+on in the superclass § So, we cannot instan+ate an abstract class! § To ensure that a subclass does, indeed, override a method Abstract classes Product! - brand : String" - price : double" + getBrand() : String" + getPrice() : double! TV! - inches : int" + getInches(): int" + getPrice() : double" Refrigerator! - size : int" + getSize(): int" + getPrice() : double" Stove! - burners : int" + getBurners(): int" + getPrice() : double" Exercises 11 Shape § Suppose we want to design a system that draws shapes in the screen. Consider three shapes: triangle, rectangle, and circle. All shapes must have capabili+es to print themselves and to calculate their areas. In addi+on, each shape should have an specific point. This point is important to start drawing the shape in the screen.
Compartilhar