Implement parent class

Asked

Viewed 250 times

2

Well, I’ve done a lot of research, I have a sense of how it works, but I couldn’t implement it, I’d like a light. I have the classes House and Building, I need to create a father class Immovable, having House and Building as daughter classes, follows 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’s the class I need to make the "father":

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 asked, but has the same objective. Good, how to proceed to create a parent class as described?

  • 1

    If to create the inheritance, only the extends after the class names so: public class Edificio extends Imovel

  • Okay, thanks... I could show you an example of what the immovable class would look like?

1 answer

2


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

Staying 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
}
  • That’s what I needed, now I get it. Thanks a lot for the help.

  • I’m in the same trouble

Browser other questions tagged

You are not signed in. Login or sign up in order to post.