Buscar

Aula 07 - Composição e Herança (2)

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 3, do total de 28 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 6, do total de 28 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Você viu 9, do total de 28 páginas

Faça como milhares de estudantes: teste grátis o Passei Direto

Esse e outros conteúdos desbloqueados

16 milhões de materiais de várias disciplinas

Impressão de materiais

Agora você pode testar o

Passei Direto grátis

Prévia do material em texto

Projeto de Software / P3 
Composição e Herança 
Márcio Ribeiro 
marcio@ic.ufal.br 
twitter.com/marciomribeiro 
Last	
  class:	
  
sca)ered	
  change	
  requests	
  
3 
Change	
  request	
  sca)ered	
  throughout	
  the	
  classes…	
  
TV!
- brand : String!
- price : double!
- inches : int!
+ getBrand() : String!
+ getPrice() : double!
+ getInches(): int!
Refrigerator!
- brand : String!
- price : double!
- size : int!
+ getBrand() : String!
+ getPrice() : double!
+ getSize(): int!
Stove!
- brand : String!
- price : double!
- burners : int!
+ getBrand() : String!
+ getPrice() : double!
+ getBurners(): int!
Common	
  a)ributes	
  in	
  the	
  Product	
  class…	
  
Product!
- brand : String!
- price : double!
+ getBrand() : String!
+ getPrice() : double!
TV!
- inches : int!
+ getInches(): int!
Refrigerator!
- size : int!
+ getSize(): int!
Stove!
- burners : int!
+ getBurners(): int!
Inheritance! 
Prin9ng	
  objects	
  
6 
We	
  can	
  use	
  the	
  println	
  method	
  	
  
public static void main(String[] args) {	
	 		
 TV samsungTV = new TV("Samsung", 3999.99, 42);	
 Refrigerator consulFridge = new Refrigerator("Consul", 2999.99, 60);	
 Stove brastempStove = new Stove("Brastemp", 399.99, 4);	
	 		
 System.out.println(samsungTV.toString());	
 System.out.println(consulFridge.toString());	
 System.out.println(brastempStove.toString());	
	
}	
Product!
- brand : String!
- price : double!
+ getBrand() : String!
+ getPrice() : double!
TV!
- inches : int!
+ getInches(): int!
Where is “toString”? 
I didn’t implement 
this method! 
TV samsungTV = new TV(	
 "Samsung", 3999.99, 42);	
	 		
System.out.println(	
 samsungTV.toString());	
This	
  happens	
  due	
  to	
  inheritance!	
  
Therefore,	
  let’s	
  be)er	
  study	
  
this	
  important	
  concept	
  
9 
Object	
  class	
  
“Class Object is the root of the class hierarchy. 
Every class has Object as a superclass. 
All objects, including arrays, implement 
the methods of this class.” 
From	
  http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html	
  
Common	
  a)ributes	
  in	
  the	
  Product	
  class…	
  
Class A! Class B! Class N!
Any class inherit Object methods! 
Object!
… 
From	
  http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html	
  
So, we can call these methods… 
12 
Let’s	
  call	
  toString…	
  
TV samsungTV = new TV("Samsung", 3999.99, 42);	
	 		
System.out.println(samsungTV.toString());	
br.ufal.ic.ecommerce.product.TV@2c6f7ce9	
We	
  are	
  not	
  happy	
  about	
  toString!	
  
We	
  cannot	
  change	
  Object...	
  
How	
  to	
  deal	
  with	
  this	
  problem?	
  
14 
When	
  using	
  inheritance	
  we	
  can…	
  
§  …	
  add	
  new	
  methods	
  
§  We	
  did:	
  getInches,	
  for	
  example	
  
§  …	
  use	
  methods	
  from	
  superclass	
  
§  We	
  did:	
  we	
  called	
  toString	
  
§  …	
  override	
  methods!	
  
Let’s	
  override	
  toString!	
  
15 
Overriding	
  the	
  toString	
  method:	
  same	
  signature!	
  
public class TV extends Product {	
	
 …	
		
 public String toString() {	
 String result = super.toString();	
 result += "Brand = " + brand +	
	 "Price = " + super.price +	
	 "Inches = " + inches;	
 return result;	
 }	
}	
public class Product {	
	
 private String brand;	
 private double price;	
 	
 public Product(String brand, double price) {	
 this.brand = brand;	
 this.price = price;	
 }	
 …	
}	
16 
Visibility	
  
§  Private	
  
§  Nobody	
  but	
  the	
  own	
  class	
  
§  Public	
  
§  Everybody	
  that	
  can	
  access	
  the	
  class	
  
§  Protected	
  
§  Intermediary	
  level	
  protec9on	
  
•  Same	
  hierarchy	
  
•  Same	
  package	
  
Product!
# brand : String!
# price : double!
+ getBrand() : String!
+ getPrice() : double!
TV!
- inches : int!
+ getInches(): int!
17 
Overriding	
  the	
  toString	
  method:	
  same	
  signature!	
  
public class TV extends Product {	
	
 …	
		
 public String toString() {	
 String result = super.toString();	
 result = "Brand = " + brand +	
	 "Price = " + super.price +	
	 "Inches = " + inches;	
 return result;	
 }	
}	
public class Product {	
	
 protected String brand;	
 protected double price;	
 	
 public Product(String brand, double price) {	
 this.brand = brand;	
 this.price = price;	
 }	
 …	
}	
Using	
  the	
  superclass	
  type	
  
19 
Using	
  the	
  superclass	
  type	
  
TV samsungTV = new TV("Samsung", 3999.99, 42);	
Product samsungTV = new TV("Samsung", 3999.99, 42);	
	
System.out.println(samsungTV.getInches());	
TV tv = (TV) samsungTV;	
	 		
System.out.println(tv.getInches());	
What	
  if	
  we	
  don’t	
  know	
  
the	
  product	
  for	
  sure?!	
  
We	
  can	
  make	
  mistakes	
  
when	
  cas9ng…	
  
22 
Using	
  the	
  superclass	
  type	
  
Product product = new Stove("Brastemp", 399.99, 4);	
	
//Many LOC...	
		
TV tv = (TV) product;	
	 		
System.out.println(tv.getInches());	
Exception in thread "main" java.lang.ClassCastException:	
 br.ufal.ic.ecommerce.product.Stove cannot be cast	
 to br.ufal.ic.ecommerce.product.TV	
	at br.ufal.ic.ecommerce.Main.computeCosts(Main.java:21)	
	at br.ufal.ic.ecommerce.Main.main(Main.java:47)	
23 
Instanceof	
  
Product product = new Stove("Brastemp", 399.99, 4);	
	 		
if (product instanceof TV) {	
	
 TV tv = (TV) product;	
	
 System.out.println(tv.getInches());	
	
} else if (product instanceof Stove) {	
	
 Stove stove = (Stove) product;	
	
 System.out.println(stove.getBurners());	
	
}	
Exercises	
  
25 
Exercises	
  
§  Implement	
  the	
  toString	
  method	
  for	
  
§  TV	
  
§  Stove	
  
§  Refrigerator	
  
§  Create	
  a	
  Liquida9on	
  class	
  
§  For	
  each	
  product	
  
•  Set	
  50%	
  off	
  for	
  all	
  42’’	
  TVs	
  
Liquidation!
+ startLiquidation() : void!
Inventary!
+ getAllProducts : ArrayList!
Be	
  aware	
  of	
  code	
  clones!	
  
Do	
  not	
  use	
  brand	
  and	
  price	
  
in	
  TV,	
  Refrigerator,	
  and	
  Stove!	
  
If	
  you	
  do	
  that,	
  
you’re	
  duplica9ng	
  code!	
  
28 
Be	
  aware	
  of	
  code	
  clones!	
  
§  Use	
  brand	
  and	
  price	
  in	
  the	
  toString	
  of	
  the	
  Product	
  class!	
  
public class TV extends Product {	
	
 …	
		
 public String toString() {	
 return super.toString() + "\nInches = " + inches;	
 }	
}	
public class Product {	
	
 …	
		
 public String toString() {	
 return "Brand = " + brand + "\nPrice = " + price;	
 }	
}

Outros materiais