Implement parent class

2

Well, I researched a lot, I even know how it works, but I could not implement it, I would like a light. I have the classes House and Building, I need to create a parent class Imovel, taking House and Building as daughter classes, follow my code:

 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;
    }
}

And here is the class I need to make the "parent":

package br.edu.utfpr.exer05;

import java.util.ArrayList;

public class Imovel {
    String cor;
    ArrayList<Porta> portas = new ArrayList<Porta>();
}

The classes House and Building are made in different ways because the exercise requested, but has the same goal. Well, how do I create a parent class as described?

    
asked by anonymous 24.05.2015 / 07:11

1 answer

2

You must create the Imovel class with the attributes and methods that are common to all child classes, for these attributes and methods to be visible to child classes the access modifier must be protected or public , done you create the child classes of Imovel in your case Casa and Edificio and make them inherit / extend Imovel .

It looks like this:

public class Imovel{
   // Atributos e Métodos comuns às classes filhas
   protected List<Porta> portas;
   protected String cor;
}

public class Casa extends Imovel{
   // Atributos e Métodos específicos de Casa
}

public class Edificio extends Imovel{
   // Atributos e Métodos específicos de Edificio
}
    
24.05.2015 / 07:21