Considerando a especificação da classe Point
apresentada, quais problemas poderiam ocorrer se ela fosse herdada? Como esses problemas poderiam ser evitados?
Sintes, Anthony. (2002). Aprenda Programação Orientada a Objetos em 21 Dias. São Paulo: Pearson Education do Brasil. 693 páginas.
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* A point representing a location in (x,y) coordinate space,
* specified in integer precision
*/
public class Point
{
/**
* The X coordinate of this Point
*/
public int x;
/**
* The Y coordinate of this Point
*/
public int y;
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Returns the location of this point
*
* @return location of this point
*/
public Point getLocation()
{
return new Point(x, y);
}
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
public void setLocation(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
public void setLocation(Point p)
{
x = p.x;
y = p.y;
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
/**
* A point representing a location in (x,y) coordinate space,
* specified in integer precision
*/
class Point
{
public:
/**
* The X coordinate of this Point
*/
int x;
/**
* The Y coordinate of this Point
*/
int y;
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
Point(int x, int y);
/**
* Returns the location of this point
*
* @return location of this point
*/
Point* getLocation();
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
void setLocation(int x, int y);
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
void setLocation(Point* p);
};
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
#include "Point.h"
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
Point::Point(int x, int y)
{
Point::x = x;
Point::y = y;
}
/**
* Returns the location of this point
*
* @return location of this point
*/
Point* Point::getLocation()
{
return new Point(x, y);
}
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
void Point::setLocation(int x, int y)
{
Point::x = x;
Point::y = y;
}
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
void Point::setLocation(Point* p)
{
x = p->x;
y = p->y;
}
Da forma como a classe Point
foi implementada, toda subclasse terá acesso direto à representação interna de Point. Tal acesso irrestrito destrói o encapsulamento.
Remediar a situação é tão fácil quanto tornar x
e y
privados. Nesse caso, é necessário adicionar os métodos acessores para os atributos da classe.
Note que essa classe Point
é modelada de acordo com java.awt.Point
.
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* A point representing a location in (x,y) coordinate space,
* specified in integer precision
*/
public class Point
{
/**
* The X coordinate of this Point
*/
private int x;
/**
* The Y coordinate of this Point
*/
private int y;
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Returns the location of this point
*
* @return location of this point
*/
public Point getLocation()
{
return new Point(x, y);
}
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
public void setLocation(int x, int y)
{
this.x = x;
this.y = y;
}
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
public void setLocation(Point p)
{
x = p.getX();
y = p.getY();
}
/**
* Returns the X coordinate of this Point
*
* @return the X coordinate of this Point
*/
public int getX()
{
return x;
}
/**
* Returns the Y coordinate of this Point
*
* @return the Y coordinate of this Point
*/
public int getY()
{
return y;
}
}
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
/**
* A point representing a location in (x,y) coordinate space,
* specified in integer precision
*/
class Point
{
private:
/**
* The X coordinate of this Point
*/
int x;
/**
* The Y coordinate of this Point
*/
int y;
public:
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
Point(int x, int y);
/**
* Returns the location of this point
*
* @return location of this point
*/
Point* getLocation();
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
void setLocation(int x, int y);
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
void setLocation(Point* p);
/**
* Returns the X coordinate of this Point
*
* @return the X coordinate of this Point
*/
int getX();
/**
* Returns the Y coordinate of this Point
*
* @return the Y coordinate of this Point
*/
int getY();
};
/**
* Copyright (C) 2009/2025 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
#include "Point.h"
/**
* Constructs and initializes a point at the specified (x,y)
* location in the coordinate space
*
* @param x the X coordinate of the newly constructed Point
* @param y the Y coordinate of the newly constructed Point
*/
Point::Point(int x, int y)
{
Point::x = x;
Point::y = y;
}
/**
* Returns the location of this point
*
* @return location of this point
*/
Point* Point::getLocation()
{
return new Point(x, y);
}
/**
* Changes the point to have the specified location
*
* @param x the X coordinate of the new location
* @param y the Y coordinate of the new location
*/
void Point::setLocation(int x, int y)
{
Point::x = x;
Point::y = y;
}
/**
* Sets the location of the point to the specified location
*
* @param p a point, the new location for this point
*/
void Point::setLocation(Point* p)
{
x = p->getX();
y = p->getY();
}
/**
* Returns the X coordinate of this Point
*
* @return the X coordinate of this Point
*/
int Point::getX()
{
return x;
}
/**
* Returns the Y coordinate of this Point
*
* @return the Y coordinate of this Point
*/
int Point::getY()
{
return y;
}