Bom, pesquisei bastante, até tenho noção de como funciona, mas não consegui implementar, gostaria de uma luz. Tenho as classes Casa e Edificio, preciso criar uma classe pai Imovel, tendo Casa e Edificio como classes filhas, segue meu código:
package br.edu.utfpr.exer05;
public class Porta {
boolean aberta;
String cor;
double dimensaoX, dimensaoY, dimensaoZ;
void abre() {
this.aberta = true;
}
void fecha() {
this.aberta = false;
}
void pinta(String cor) {
this.cor = cor;
}
boolean estaAberta() {
boolean estaAberta = false;
if(aberta == true) estaAberta = true;
return estaAberta;
}
}
package br.edu.utfpr.exer05;
import java.util.ArrayList;
public class Edificio {
String cor;
int totalDePortas;
int totalDeAndares;
ArrayList<Porta> portas = new ArrayList<Porta>();
void pinta(String cor) {
this.cor = cor;
}
int quantasPortasEstaoAbertas() {
int qtdPortasAbertas = 0;
for(int i=0; i<portas.size(); i++) {
portas.get(i);
if(portas.get(i).estaAberta() == true) {
qtdPortasAbertas++;
}
}
return qtdPortasAbertas;
}
void adicionaPorta(Porta porta) {
this.portas.add(porta);
}
int totalDePortas() {
return this.portas.size();
}
void adicionaAndar() {
this.totalDeAndares = totalDeAndares += 1;
}
int totalDeAndares() {
return this.totalDeAndares;
}
}
package br.edu.utfpr.exer05;
public class Casa {
String cor;
Porta porta1, porta2, porta3;
void pinta(String cor) {
this.cor = cor;
}
int quantasPortasEstaoAbertas() {
int qtdPortasAbertas=0;
if(porta1.estaAberta() == true) qtdPortasAbertas++;
if(porta2.estaAberta() == true) qtdPortasAbertas++;
if(porta3.estaAberta() == true) qtdPortasAbertas++;
return qtdPortasAbertas;
}
int totalDePortas() {
return 3;
}
}
E aqui a classe que preciso tornar a "pai":
package br.edu.utfpr.exer05;
import java.util.ArrayList;
public class Imovel {
String cor;
ArrayList<Porta> portas = new ArrayList<Porta>();
}
As classes Casa e Edificio estao feitas de formas diferentes pois o exercício pedia, porém tem o mesmo objetivo. Bom, como proceder para criar uma classe pai nos moldes descritos?
extends
após o nome das classes assim:public class Edificio extends Imovel