SlideShare uma empresa Scribd logo
1 de 14
herança PROF. DOUGLAS FRARI douglas.frari@gmail.com PEDRO FELIPE DE LIMA pedroo_felipe@hotmail.com JAMERSON SANTOS jamersonsantos41@yahoo.com.br JOEL LUIS www.bid3@yahoo.com.br
herança Mecanismo pelo qual elementos mais específicos (subclasses) incorporam estrutura e comportamento de elementos mais gerais (superclasses). // SuperClass.java publicclassSuperClass { ... } // SubClass.java public class SubClass extends SuperClass { ... } ,[object Object]
Herança é na verdade, uma classe derivada de outra classe. Processo no qual uma classe herda características de outra.,[object Object],[object Object]
NA PRÁTICA ,[object Object],01.   package heranca; 02.  03.   publicclassEletrodomestico { 04.       privateboolean ligado; 05.       privateint voltagem; 06.       privateint consumo; 07.  08.       publicEletrodomestico(boolean ligado, int voltagem, int consumo) { 09.           this.ligado = ligado; 10.           this.voltagem = voltagem; 11.           this.consumo = consumo; 12.       } 13.   }
NA PRÁTICA ,[object Object],01.   package heranca; 02.  03.   publicclass TV extendsEletrodomestico { 04.       privateint canal; 05.       privateint volume; 06.       privateint tamanho; 07.  08.     public TV(int voltagem, int consumo, int canal, int volume, int tamanho) { 09.         super(false, voltagem, consumo); 10.         this.canal = canal; 11.         this.volume = volume; 12.         this.tamanho = tamanho; 13.     } 14.  }
[object Object],01.   package heranca; 02.  03.   publicclassExemploHeranca { 04.       publicstaticvoidmostrarCaracteristicas(TV obj) { 05.           System.out.print("Esta TV tem as seguintes características:" 06.                + "Tamanho: " + obj.getTamanho() + "quot;" 07.                + "Voltagem Atual: "+ obj.getVoltagem() + "V" 08.                + "Consumo/h: " + obj.getConsumo() + "W"); 09.           if (obj.isLigado()) { 10.                System.out.println("Ligado: Sim" 11.                    + "Canal: " + obj.getCanal() + "" 12.                    + "Volume: " + obj.getVolume()+""); 13.           } else { 14.               System.out.println("Ligado: Não"); 15.           } 16.       } 17.  18.       publicstaticvoidmain(String args[]) { 19.           TV tv1 = new TV(110, 95, 0, 0, 21); 20.           TV tv2 = new TV(220, 127, 0, 0, 29); 21.            tv2.setLigado(true); 22.            tv2.setCanal(3); 23.            tv2.setVolume(25); 24.            mostrarCaracteristicas(tv1); 25.            mostrarCaracteristicas(tv2); 26.       } 27.   }
herança Resultado do código:    Esta TV tem as seguintes características:Tamanho: 21"Voltagem Atual: 110VConsumo/h: 95WLigado: NãoEsta TV tem as seguintes características:Tamanho: 29"Voltagem Atual: 220VConsumo/h: 127WLigado: SimCanal: 3Volume: 25
EXERCÍCIO Crie um código para representar Liquidificadores. Sendo o mesmo tendo o controle de Velocidade Analógico e Digital. ,[object Object],[object Object]
Classes: Liquidificador // LiquidificadorGenerico.java publicclassLiquidificadorGenerico { // atributos protectedint velocidade; protectedintvelocidadeMaxima; // construtores publicLiquidificadorGenerico() { velocidade = 0; velocidadeMaxima = 2; } publicLiquidificadorGenerico(int v) { this() ajustarVelocidadeMaxima(v); } // metodos protectedvoidajustarVelocidadeMaxima(int v) { if (v>0) velocidadeMaxima = v; } protectedvoidajustarVelocidade(int v) { if (v>=0 && v<=velocidadeMaxima) velocidade = v; } publicintobterVelocidadeMaxima() { returnvelocidadeMaxima; } } publicintobterVelocidade() { return velocidade; } }
ClasseLiquidificadorAnalogico // LiquidificadorAnalogico.java publicclassLiquidificadorAnalogicoextends LiquidificadorGenerico { // construtor publicLiquidificadorAnalogico() { velocidade = 0; } // metodos publicvoidaumentarVelocidade() { ajustarVelocidade(velocidade + 1); } publicvoiddiminuirVelocidade() { diminuirVelocidade(velocidade - 1); } }
Classe LiquidificadorDigital // LiquidificadorDigital.java publicclassLiquidificadorDigitalextends LiquidificadorGenerico{ // construtor publicLiquidificadorDigital() { velocidade = 0; } // metodo publicvoidtrocarVelocidade(int v) { // aciona método protegido para troca de velocidade ajustarVelocidade(v); } }
referências Tiexpert http://www.tiexpert.net/programacao/java/heranca.php Wikipedia http://pt.wikipedia.org/wiki/Herança_(programação) Imasters http://imasters.uol.com.br/artigo/3444/java/heranca_em_java/ DCA - Department of Computer Engineering and Industrial Automation  http://www.dca.fee.unicamp.br/cursos/PooJava/heranca/ PUCRS | Faculdade de Informática http://www.inf.pucrs.br/~flash/lapro2/lapro2_heranca.pdf Núcleo de Educação a Distância - Universidade São Francisco Introdução ao Java, Apostila. - Prof. Peter Jandl Junior - 1999
Herança
Herança

Mais conteúdo relacionado

Destaque

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Destaque (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Herança

  • 1. herança PROF. DOUGLAS FRARI douglas.frari@gmail.com PEDRO FELIPE DE LIMA pedroo_felipe@hotmail.com JAMERSON SANTOS jamersonsantos41@yahoo.com.br JOEL LUIS www.bid3@yahoo.com.br
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. herança Resultado do código: Esta TV tem as seguintes características:Tamanho: 21"Voltagem Atual: 110VConsumo/h: 95WLigado: NãoEsta TV tem as seguintes características:Tamanho: 29"Voltagem Atual: 220VConsumo/h: 127WLigado: SimCanal: 3Volume: 25
  • 8.
  • 9. Classes: Liquidificador // LiquidificadorGenerico.java publicclassLiquidificadorGenerico { // atributos protectedint velocidade; protectedintvelocidadeMaxima; // construtores publicLiquidificadorGenerico() { velocidade = 0; velocidadeMaxima = 2; } publicLiquidificadorGenerico(int v) { this() ajustarVelocidadeMaxima(v); } // metodos protectedvoidajustarVelocidadeMaxima(int v) { if (v>0) velocidadeMaxima = v; } protectedvoidajustarVelocidade(int v) { if (v>=0 && v<=velocidadeMaxima) velocidade = v; } publicintobterVelocidadeMaxima() { returnvelocidadeMaxima; } } publicintobterVelocidade() { return velocidade; } }
  • 10. ClasseLiquidificadorAnalogico // LiquidificadorAnalogico.java publicclassLiquidificadorAnalogicoextends LiquidificadorGenerico { // construtor publicLiquidificadorAnalogico() { velocidade = 0; } // metodos publicvoidaumentarVelocidade() { ajustarVelocidade(velocidade + 1); } publicvoiddiminuirVelocidade() { diminuirVelocidade(velocidade - 1); } }
  • 11. Classe LiquidificadorDigital // LiquidificadorDigital.java publicclassLiquidificadorDigitalextends LiquidificadorGenerico{ // construtor publicLiquidificadorDigital() { velocidade = 0; } // metodo publicvoidtrocarVelocidade(int v) { // aciona método protegido para troca de velocidade ajustarVelocidade(v); } }
  • 12. referências Tiexpert http://www.tiexpert.net/programacao/java/heranca.php Wikipedia http://pt.wikipedia.org/wiki/Herança_(programação) Imasters http://imasters.uol.com.br/artigo/3444/java/heranca_em_java/ DCA - Department of Computer Engineering and Industrial Automation http://www.dca.fee.unicamp.br/cursos/PooJava/heranca/ PUCRS | Faculdade de Informática http://www.inf.pucrs.br/~flash/lapro2/lapro2_heranca.pdf Núcleo de Educação a Distância - Universidade São Francisco Introdução ao Java, Apostila. - Prof. Peter Jandl Junior - 1999