Kali San
Kali San

Reputation: 1

Inhetirance java, how can I do this?

I need to ask u some help about some problem that I have.

I need to create a superclass called "Cuadrilatero" and a sub class called "Paralelogramo".

Paralelogramo is going to extend Cuadrilatero. Cuadrilatero have 4 points with coords for X and Y that I need to inherit to the subclass Paralelogramo.

Here's the code.

public class Cuadrilatero {

private double P1;
private double P2;
private double P3;
private double P4;

private double x1,x2,x3,x4,y1,y2,y3,y4;


public Cuadrilatero() {
}

public Cuadrilatero(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
    setP1(x1, y1);
    setP2(x2, y2);
    setP3(x3, y3);
    setP4(x4, y4);
}

//creación de los puntos

public void setP1(double x1, double y1){
    this.x1=x1;
    this.y1=y1;
}

public double getP1(){
    return P1;
}


public void setP2(double x2, double y2){
    this.x2=x2;
    this.y2=y2;
} 

public double getP2(){
    return P2;
}

public void setP3(double x3, double y3){
    this.x3=x3;
    this.y3=y3;
}

public double getP3(){
    return P3;
}


public void setP4(double x4, double y4){
    this.x4=x4;
    this.y4=y4;
}

public double getP4(){
    return P4;
}

}  

and the Paralelogramo class

class Paralelogramo extends Cuadrilatero{

private double altura;
private double base;

public Paralelogramo(){
}

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
 super(P1,P2,P3,P4);
 }

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4,double altura, double base){
    setAltura(altura);
    setBase(base);
}

public void setAltura(double altura){
    this.altura=altura;

}

public double getAltura(){
    altura=y2-y1;
    return altura;
}

public void setBase(double base){
    this.base=base;
}

public double getBase(){
    base=x4-x1;
    return base;
}

public double getArea(){
    double area;

    area= getBase() * getAltura();

    return area;

}

public String toString(){
    return "El area del [Paralelogramo] es: " + getArea() + " cuadrados";
    }
}

When I compile Cuadrilatero theres not a problem but when I compile Cuadrilatero showns an error with the variable P1,P2,P3,P4 and the X and Y. "Has private Access"

How Can I solve this?

Upvotes: 0

Views: 82

Answers (3)

Jul10
Jul10

Reputation: 503

As still exposed by previous answers the problem that cause the error is in the call of the base class costructor.
In the first definition of Paralelogramo costructor

 public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
 super(P1,P2,P3,P4);
 }

the problem is in the number of parameters: super (i.e the costructor of Quadrilatero), as you defined it, needs 8 parameters but when you call it you provide only 4 parameters.
The second definition of Paralogramo costructor

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4,double altura, double base){
        //here there is a hidden call to super() i.e.
        //in this case Quadrilatero()
        setAltura(altura);
        setBase(base);
    }

calls the base class costructor without parameters but it does nothing when instead it should be initialize the attributes to some values.

But in my opinion the error is in the initial concept: if Quad is a base class extended by a subclass Parallelogram, the attributes in common beetween Quad and Parallogram should be protected to make these accessible to subclass, and not private that make these accessible only from Quad itself.

Upvotes: 0

drewdles
drewdles

Reputation: 244

In your question, I assume you meant that you are getting errors when compiling the Paralelogramo class. One of the constructors of that class:

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
  super(P1, P2, P3, P4);
}

This line is using variables that are available in the super class, but they have the private modifier and hence it won't work. You need to use the protected access modifier to allow access by sub-classes.

Upvotes: 0

Luai Ghunim
Luai Ghunim

Reputation: 976

First mistakes you have here:

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
 super(P1,P2,P3,P4);
 }

You should change above to this:

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4){
 super(x1,y1,x2,y2,x3,y3,x4,y4);

}

public Paralelogramo(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4,double altura, double base){
 super(x1,y1,x2,y2,x3,y3,x4,y4);
    setAltura(altura);
    setBase(base);
}

But there are still other problems. Your variable P1 or P2 has type double so when you say

`public double getP1(){
    return P1;
}`

It will return 0.

Possible fix: Create a new class `Point which takes an x and y then work further with the instances of that Point class.

Upvotes: 1

Related Questions